Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'gno mod tidy -v --recursive' flags #2485

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 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 @@
)
}

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 @@
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 @@
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 @@
gm.DropRequire(r.Mod.Path)
}

imports, err := getGnoPackageImports(wd)
imports, err := getGnoPackageImports(pkgdir)
if err != nil {
return err
}
Expand All @@ -241,6 +291,9 @@
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
Loading