Skip to content

Commit

Permalink
Merge pull request #80 from kcmvp/version
Browse files Browse the repository at this point in the history
add gob setup version
  • Loading branch information
kcmvp authored Jan 25, 2024
2 parents ded724d + 7d9f466 commit 70badb1
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 29 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ these tools and frameworks exist independently to solve specific problems.
Whenever a new Golang project is started, it requires a series of initialization;
What’s worse is that whenever your switch the development environment, same process have to be repeated!
This project is built to solve this problem by providing a method similar to [Maven](https://maven.apache.org/)
or [Gradle](https://gradle.com/) in the **Java** ecosystem. Please refer [Document](#commands) for details
or [Gradle](https://gradle.com/) in the **Java** ecosystem. Please refer [documents](#commands) for details

<span id="nav-3"></span>

## Features

1. **Everything is a plugin, simple yet powerful !**
2. Build a tool chain and workflow without a line.
2. Define a tool chain and workflow without a line code.
3. Better user experience

## How gob works
Expand Down Expand Up @@ -171,6 +171,22 @@ List all the installed plugins

### gob setup version


## FAQ

- [When install a plugin, how to find out the url?](#)

`gob plugin install` work the same way as `go install`, it take the same url as `go install`.


- [How to upgrade a plugin ?](#)

Just simply edit `gob.yaml` file and update the version you want.
```yaml
plugins:
golangci-lint:
alias: lint
args: run ./...
url: github.com/golangci/golangci-lint/cmd/[email protected]
```
`

30 changes: 25 additions & 5 deletions cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type (
Action lo.Tuple2[string, Execution]
)

func builtinActions() []Action {
func buildActions() []Action {
return []Action{
{A: "build", B: buildAction},
{A: "clean", B: cleanAction},
Expand All @@ -40,8 +40,14 @@ func builtinActions() []Action {
}
}

func setupActions() []Action {
return []Action{
{A: "version", B: setupVersion},
}
}

func beforeExecution(cmd *cobra.Command, arg string) error {
if action, ok := lo.Find(builtinActions(), func(action Action) bool {
if action, ok := lo.Find(buildActions(), func(action Action) bool {
return action.A == fmt.Sprintf("before_%s", arg)
}); ok {
return action.B(cmd, arg)
Expand All @@ -50,7 +56,7 @@ func beforeExecution(cmd *cobra.Command, arg string) error {
}

func afterExecution(cmd *cobra.Command, arg string) error {
if action, ok := lo.Find(builtinActions(), func(action Action) bool {
if action, ok := lo.Find(buildActions(), func(action Action) bool {
return action.A == fmt.Sprintf("after_%s", arg)
}); ok {
return action.B(cmd, arg)
Expand All @@ -67,7 +73,7 @@ func execute(cmd *cobra.Command, arg string) error {
return plugin.Alias == arg
}); ok {
err = plugin.Execute()
} else if action, ok := lo.Find(builtinActions(), func(action Action) bool {
} else if action, ok := lo.Find(buildActions(), func(action Action) bool {
return action.A == arg
}); ok {
err = action.B(cmd, arg)
Expand All @@ -81,7 +87,7 @@ func execute(cmd *cobra.Command, arg string) error {
}

func validBuilderArgs() []string {
builtIn := lo.Map(lo.Filter(builtinActions(), func(item Action, _ int) bool {
builtIn := lo.Map(lo.Filter(buildActions(), func(item Action, _ int) bool {
return !strings.Contains(item.A, "_")
}), func(action Action, _ int) string {
return action.A
Expand Down Expand Up @@ -157,3 +163,17 @@ func coverReport(_ *cobra.Command, _ ...string) error {
}
return fmt.Errorf(color.RedString("Failed to generate coverage report %s", err.Error()))
}

func setupVersion(_ *cobra.Command, _ ...string) error {
infra := filepath.Join(internal.CurProject().Root(), "infra")
if _, err := os.Stat(infra); err != nil {
os.Mkdir(infra, 0700) // nolint
}
ver := filepath.Join(infra, "version.go")
if _, err := os.Stat(ver); err != nil {
data, _ := resources.ReadFile(filepath.Join(resourceDir, "version.tmpl"))
os.WriteFile(ver, data, 0666) //nolint
}
color.GreenString("version file is generated at infra/version.go")
return nil
}
22 changes: 19 additions & 3 deletions cmd/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (suite *ActionTestSuite) TestActionBuild() {
}

func (suite *ActionTestSuite) TestBeforeExecution() {
actions := lo.Filter(builtinActions(), func(item Action, _ int) bool {
actions := lo.Filter(buildActions(), func(item Action, _ int) bool {
return !strings.Contains(item.A, "_")
})
args := lo.Map(actions, func(item Action, _ int) string {
Expand All @@ -47,8 +47,8 @@ func (suite *ActionTestSuite) TestBeforeExecution() {
}

func (suite *ActionTestSuite) TestBuiltInActions() {
assert.Equal(suite.T(), 4, len(builtinActions()))
assert.Equal(suite.T(), []string{"build", "clean", "test", "after_test"}, lo.Map(builtinActions(), func(item Action, index int) string {
assert.Equal(suite.T(), 4, len(buildActions()))
assert.Equal(suite.T(), []string{"build", "clean", "test", "after_test"}, lo.Map(buildActions(), func(item Action, index int) string {
return item.A
}))
}
Expand All @@ -66,5 +66,21 @@ func (suite *ActionTestSuite) TestCoverage() {
_, err1 := os.Stat(filepath.Join(internal.CurProject().Target(), "cover.out"))
err2 := coverReport(nil, "")
assert.True(suite.T(), (err1 == nil) == (err2 == nil))
}

func (suite *ActionTestSuite) TestSetupActions() {
assert.Equal(suite.T(), 1, len(setupActions()))
}

func (suite *ActionTestSuite) TestSetupVersion() {
err := setupVersion(nil, "")
assert.NoError(suite.T(), err)
version := filepath.Join(internal.CurProject().Root(), "infra", "version.go")
os.Remove(version)
_, err = os.Stat(version)
assert.Error(suite.T(), err)
err = setupVersion(nil, "")
assert.NoError(suite.T(), err)
_, err = os.Stat(version)
assert.NoError(suite.T(), err)
}
16 changes: 2 additions & 14 deletions cmd/initializer.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
Copyright © 2023 kcmvp <[email protected]>
*/
package cmd

Expand Down Expand Up @@ -28,21 +28,9 @@ func builtinPlugins() []internal.Plugin {
return plugins
}

func initBuildVersion() {
infra := filepath.Join(internal.CurProject().Root(), "infra")
if _, err := os.Stat(infra); err != nil {
os.Mkdir(infra, 0700) // nolint
}
ver := filepath.Join(infra, "version.go")
if _, err := os.Stat(ver); err != nil {
data, _ := resources.ReadFile(filepath.Join(resourceDir, "version.tmpl"))
os.WriteFile(ver, data, 0666) //nolint
}
}

func initializerFunc(_ *cobra.Command, _ []string) {
fmt.Println("Initialize configuration ......")
initBuildVersion()
// initBuildVersion()
lo.ForEach(builtinPlugins(), func(plugin internal.Plugin, index int) {
internal.CurProject().SetupPlugin(plugin)
if len(plugin.Config) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/resources/version.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package config This file is generated by gob
// This file is generated by gob
package infra

// buildVersion don't change this
Expand Down
33 changes: 33 additions & 0 deletions cmd/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/samber/lo"

"github.com/spf13/cobra"
)

// setupCmd represents the setup command
var setupCmd = &cobra.Command{
Use: "setup",
Short: "Setup scaffold of Go project",
Long: `Setup scaffold for most common used frameworks`,
ValidArgs: func() []string {
return lo.Map(setupActions(), func(action Action, _ int) string {
return action.A
})
}(),
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
action, _ := lo.Find(setupActions(), func(action Action) bool {
return action.A == args[0]
})
_ = action.B(cmd, args...)
},
}

func init() {
builderCmd.AddCommand(setupCmd)
}
25 changes: 25 additions & 0 deletions cmd/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"github.com/kcmvp/gob/internal"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)

func TestValidSetupArgs(t *testing.T) {
assert.Equal(t, setupCmd.ValidArgs, []string{"version"})
}

func TestSetupVersion(t *testing.T) {
version := filepath.Join(internal.CurProject().Root(), "infra", "version.go")
os.Remove(version)
_, err := os.Stat(version)
assert.Error(t, err)
builderCmd.SetArgs([]string{"setup", "version"})
err = builderCmd.Execute()
assert.NoError(t, err)
_, err = os.Stat(version)
assert.NoError(t, err)
}
6 changes: 3 additions & 3 deletions infra/version.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Package config This file is generated by gob
// This file is generated by gob
package infra

// buildVersion don't change this, `gob build` would set this according to git commit hash
var buildVersion = "unknown"
// buildVersion don't change this
var buildVersion string

func Version() string {
return buildVersion
Expand Down

0 comments on commit 70badb1

Please sign in to comment.