Skip to content

Commit

Permalink
feat: add 'gno mod tidy -v --recursive' flags (#2485)
Browse files Browse the repository at this point in the history
`cd examples; make tidy` is now instant and verbose.

Signed-off-by: moul <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed Jul 6, 2024
1 parent f28444a commit 1169658
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
}
var errs error
for _, pkg := range pkgs {
err := modTidyOnce(cfg, wd, pkg.Dir, io)
errs = multierr.Append(errs, err)
}
return errs
}

// 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
}
if cfg.verbose {
io.ErrPrintfln("%s", relpath)
}

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)
}
}

gm.Write(fname)
Expand Down

0 comments on commit 1169658

Please sign in to comment.