Skip to content

Commit

Permalink
feat: add 'gno mod tidy -v --recursive' flags
Browse files Browse the repository at this point in the history
Signed-off-by: moul <[email protected]>
  • Loading branch information
moul committed Jul 2, 2024
1 parent 3b42391 commit a66d2ca
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ fmt:

.PHONY: tidy
tidy:
find . -name "gno.mod" -execdir go run github.com/gnolang/gno/gnovm/cmd/gno mod tidy \;
go run github.com/gnolang/gno/gnovm/cmd/gno mod tidy -v --recursive
75 changes: 64 additions & 11 deletions gnovm/cmd/gno/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ import (
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/errors"
"go.uber.org/multierr"
)

type modDownloadCfg struct {
remote string
verbose bool
}

func newModCmd(io commands.IO) *commands.Command {
cmd := commands.NewCommand(
commands.Metadata{
Expand Down Expand Up @@ -73,15 +69,17 @@ func newModInitCmd() *commands.Command {
}

func newModTidy(io commands.IO) *commands.Command {
cfg := &modTidyCfg{}

return commands.NewCommand(
commands.Metadata{
Name: "tidy",
ShortUsage: "tidy",
ShortUsage: "tidy [flags]",
ShortHelp: "add missing and remove unused modules",
},
commands.NewEmptyConfig(),
cfg,
func(_ context.Context, args []string) error {
return execModTidy(args, io)
return execModTidy(cfg, args, io)
},
)
}
Expand Down Expand Up @@ -124,6 +122,11 @@ For example:
)
}

type modDownloadCfg struct {
remote string
verbose bool
}

func (c *modDownloadCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(
&c.remote,
Expand Down Expand Up @@ -211,7 +214,27 @@ func execModInit(args []string) error {
return nil
}

func execModTidy(args []string, io commands.IO) error {
type modTidyCfg struct {
verbose bool
recursive bool
}

func (c *modTidyCfg) RegisterFlags(fs *flag.FlagSet) {
fs.BoolVar(
&c.verbose,
"v",
false,
"verbose output when running",
)
fs.BoolVar(
&c.recursive,
"recursive",
false,
"walk subdirs for gno.mod files",
)
}

func execModTidy(cfg *modTidyCfg, args []string, io commands.IO) error {
if len(args) > 0 {
return flag.ErrHelp
}
Expand All @@ -220,7 +243,34 @@ func execModTidy(args []string, io commands.IO) error {
if err != nil {
return err
}
fname := filepath.Join(wd, "gno.mod")

if cfg.recursive {
pkgs, err := gnomod.ListPkgs(wd)
if err != nil {
return err

Check warning on line 250 in gnovm/cmd/gno/mod.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/mod.go#L248-L250

Added lines #L248 - L250 were not covered by tests
}
var errs error
for _, pkg := range pkgs {
err := modTidyOnce(cfg, wd, pkg.Dir, io)
errs = multierr.Append(errs, err)

Check warning on line 255 in gnovm/cmd/gno/mod.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/mod.go#L252-L255

Added lines #L252 - L255 were not covered by tests
}
return errs

Check warning on line 257 in gnovm/cmd/gno/mod.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/mod.go#L257

Added line #L257 was not covered by tests
}

// XXX: recursively check parents if no $PWD/gno.mod
return modTidyOnce(cfg, wd, wd, io)
}

func modTidyOnce(cfg *modTidyCfg, wd, pkgdir string, io commands.IO) error {
fname := filepath.Join(pkgdir, "gno.mod")
relpath, err := filepath.Rel(wd, fname)
if err != nil {
return err

Check warning on line 268 in gnovm/cmd/gno/mod.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/mod.go#L268

Added line #L268 was not covered by tests
}
if cfg.verbose {
io.ErrPrintfln("%s", relpath)

Check warning on line 271 in gnovm/cmd/gno/mod.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/mod.go#L271

Added line #L271 was not covered by tests
}

gm, err := gnomod.ParseGnoMod(fname)
if err != nil {
return err
Expand All @@ -231,7 +281,7 @@ func execModTidy(args []string, io commands.IO) error {
gm.DropRequire(r.Mod.Path)
}

imports, err := getGnoPackageImports(wd)
imports, err := getGnoPackageImports(pkgdir)
if err != nil {
return err
}
Expand All @@ -241,6 +291,9 @@ func execModTidy(args []string, io commands.IO) error {
continue
}
gm.AddRequire(im, "v0.0.0-latest")
if cfg.verbose {
io.ErrPrintfln(" %s", im)

Check warning on line 295 in gnovm/cmd/gno/mod.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/mod.go#L295

Added line #L295 was not covered by tests
}
}

gm.Write(fname)
Expand Down

0 comments on commit a66d2ca

Please sign in to comment.