Skip to content

Commit

Permalink
Add runall for running command in every module
Browse files Browse the repository at this point in the history
  • Loading branch information
chokoswitch committed Nov 5, 2024
1 parent 1e5382b commit c544dc5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions build/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/sys v0.14.0 // indirect
)

Expand Down
2 changes: 2 additions & 0 deletions build/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/curioswitch/go-build

go 1.20
go 1.21

require (
github.com/goyek/goyek/v2 v2.1.0
github.com/goyek/x v0.1.7
golang.org/x/mod v0.20.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
Expand Down
38 changes: 37 additions & 1 deletion standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/goyek/goyek/v2"
_ "github.com/goyek/x/boot" // define flags to override
"github.com/goyek/x/cmd"
"golang.org/x/mod/modfile"
)

// DefineTasks defines common tasks for Go projects.
Expand All @@ -19,6 +20,8 @@ func DefineTasks(opts ...Option) {
// -v=false can still be used to disable it.
_ = flag.Lookup("v").Value.Set("true")

command := flag.String("cmd", "", "Command to execute with runall.")

conf := config{
artifactsPath: "out",
}
Expand Down Expand Up @@ -145,6 +148,32 @@ func DefineTasks(opts ...Option) {
}))
}

if !conf.excluded(("runall")) && fileExists("go.work") {
RegisterGenerateTask(goyek.Define(goyek.Task{
Name: "runall",
Usage: "Runs a command in each module in the workspace.",
Action: func(a *goyek.A) {
if *command == "" {
a.Error("missing -cmd flag required for runall")
return
}
content, err := os.ReadFile("go.work")
if err != nil {
a.Errorf("failed to read go.work: %v", err)
return
}
wf, err := modfile.ParseWork("go.work", content, nil)
if err != nil {
a.Errorf("failed to parse go.work: %v", err)
return
}
for _, u := range wf.Use {
cmd.Exec(a, *command, cmd.Dir(filepath.Join(".", u.Path)))
}
},
}))
}

goyek.Define(goyek.Task{
Name: "format",
Usage: "Format code in various languages.",
Expand Down Expand Up @@ -241,9 +270,16 @@ func pathRelativeToRoot() (string, string) {

func anyFileExists(dir string, files ...string) bool {
for _, f := range files {
if _, err := os.Stat(filepath.Join(dir, f)); err == nil {
if e := fileExists(filepath.Join(dir, f)); e {
return true
}
}
return false
}

func fileExists(p string) bool {
if _, err := os.Stat(p); err == nil {
return true
}
return false
}

0 comments on commit c544dc5

Please sign in to comment.