Skip to content

Commit

Permalink
feat: gno mod graph (#3588)
Browse files Browse the repository at this point in the history
Basic initial version compatible with `go mod graph` in terms of output,
while allowing the specification of folders through an optional
argument.

- [x] implement
- [x] tests
- [x] share examples

Depends on #3587

---------

Signed-off-by: moul <94029+moul@users.noreply.github.com>
  • Loading branch information
moul authored Jan 29, 2025
1 parent 15d119f commit b392287
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
49 changes: 48 additions & 1 deletion gnovm/cmd/gno/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func newModCmd(io commands.IO) *commands.Command {
cmd.AddSubCommands(
newModDownloadCmd(io),
// edit
// graph
newModGraphCmd(io),
newModInitCmd(),
newModTidy(io),
// vendor
Expand All @@ -61,6 +61,21 @@ func newModDownloadCmd(io commands.IO) *commands.Command {
)
}

func newModGraphCmd(io commands.IO) *commands.Command {
cfg := &modGraphCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "graph",
ShortUsage: "graph [path]",
ShortHelp: "print module requirement graph",
},
cfg,
func(_ context.Context, args []string) error {
return execModGraph(cfg, args, io)
},
)
}

func newModInitCmd() *commands.Command {
return commands.NewCommand(
commands.Metadata{
Expand Down Expand Up @@ -144,6 +159,38 @@ func (c *modDownloadCfg) RegisterFlags(fs *flag.FlagSet) {
)
}

type modGraphCfg struct{}

func (c *modGraphCfg) RegisterFlags(fs *flag.FlagSet) {
// /out std
// /out remote
// /out _test processing
// ...
}

func execModGraph(cfg *modGraphCfg, args []string, io commands.IO) error {
// default to current directory if no args provided
if len(args) == 0 {
args = []string{"."}
}
if len(args) > 1 {
return flag.ErrHelp
}

stdout := io.Out()

pkgs, err := gnomod.ListPkgs(args[0])
if err != nil {
return err
}
for _, pkg := range pkgs {
for _, dep := range pkg.Imports {
fmt.Fprintf(stdout, "%s %s\n", pkg.Name, dep)
}
}
return nil
}

func execModDownload(cfg *modDownloadCfg, args []string, io commands.IO) error {
if len(args) > 0 {
return flag.ErrHelp
Expand Down
28 changes: 28 additions & 0 deletions gnovm/cmd/gno/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,34 @@ func TestModApp(t *testing.T) {
# gno.land/p/demo/avl
valid.gno
`,
},

// test `gno mod graph`
{
args: []string{"mod", "graph"},
testDir: "../../tests/integ/minimalist_gnomod",
simulateExternalRepo: true,
stdoutShouldBe: ``,
},
{
args: []string{"mod", "graph"},
testDir: "../../tests/integ/valid1",
simulateExternalRepo: true,
stdoutShouldBe: ``,
},
{
args: []string{"mod", "graph"},
testDir: "../../tests/integ/valid2",
simulateExternalRepo: true,
stdoutShouldBe: `gno.land/p/integ/valid gno.land/p/demo/avl
`,
},
{
args: []string{"mod", "graph"},
testDir: "../../tests/integ/require_remote_module",
simulateExternalRepo: true,
stdoutShouldBe: `gno.land/tests/importavl gno.land/p/demo/avl
`,
},
}
Expand Down

0 comments on commit b392287

Please sign in to comment.