From a66d2caa11f8bd72c02491530a5985db2858547b Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:40:17 +0200 Subject: [PATCH] feat: add 'gno mod tidy -v --recursive' flags Signed-off-by: moul <94029+moul@users.noreply.github.com> --- examples/Makefile | 2 +- gnovm/cmd/gno/mod.go | 75 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/examples/Makefile b/examples/Makefile index b9485ff7c01..578b4faf15b 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -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 diff --git a/gnovm/cmd/gno/mod.go b/gnovm/cmd/gno/mod.go index 19d7b121970..fec1b0ab2c1 100644 --- a/gnovm/cmd/gno/mod.go +++ b/gnovm/cmd/gno/mod.go @@ -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{ @@ -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) }, ) } @@ -124,6 +122,11 @@ For example: ) } +type modDownloadCfg struct { + remote string + verbose bool +} + func (c *modDownloadCfg) RegisterFlags(fs *flag.FlagSet) { fs.StringVar( &c.remote, @@ -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 } @@ -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 @@ -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 } @@ -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)