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(gno.land/cmd): auto load and sort pkgs from examples #763

Merged
merged 30 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
75d1ed5
feat: add gno.mod to each package
harry-hov Apr 19, 2023
42dafe6
feat: add logic to load and sort gno pkgs
harry-hov Apr 19, 2023
54b8ffa
feat(gno.land/cmd): auto load pkgs from examples
harry-hov Apr 21, 2023
20d4866
fix failing tests
harry-hov Apr 21, 2023
68fe2f9
Use more concise if statement
harry-hov Apr 29, 2023
10330b5
Move pkg loading and sorting logic to gnomod
harry-hov Apr 29, 2023
d296ad5
Merge branch 'master' into hariom/load-pkgs
harry-hov Apr 29, 2023
a3e978a
Fix review comments
harry-hov May 2, 2023
fb56382
Fix gno.mod files
harry-hov May 3, 2023
312236f
Extract visit() as named func
harry-hov May 4, 2023
2d222e0
Merge branch 'master' into hariom/load-pkgs
harry-hov May 4, 2023
527c78d
Skip draft or depends on draft
harry-hov May 4, 2023
df872c0
s/v0.0.0/v0.0.0-latest
harry-hov May 15, 2023
875bf62
Merge branch 'master' into hariom/load-pkgs
harry-hov May 15, 2023
4ca9e02
Merge branch 'master' into hariom/load-pkgs
harry-hov May 16, 2023
875da90
Fix `ListPkgs()`
harry-hov May 22, 2023
9282dba
Test `ListPkgs()`
harry-hov May 22, 2023
2a7391e
Merge branch 'master' into hariom/load-pkgs
harry-hov May 22, 2023
69ead49
oversight: remove print statement
harry-hov May 22, 2023
ee268b8
Export `Pkg` and add field `draft`
harry-hov May 24, 2023
6eb4e7a
Refactor and improvements
harry-hov May 24, 2023
72d7f2d
Merge branch 'master' into hariom/load-pkgs
harry-hov May 24, 2023
2fa5e09
make fmt
harry-hov May 24, 2023
c474363
Merge branch 'master' into hariom/load-pkgs
harry-hov May 25, 2023
ea7a6f6
Merge branch 'master' into hariom/load-pkgs
harry-hov May 26, 2023
84d13a3
Merge branch 'master' into hariom/load-pkgs
harry-hov Jun 14, 2023
2fb5408
ListPkgs(): don't skip nested pkgs
harry-hov Jun 16, 2023
4c39033
Add gno.mod files to `tests/subtests`
harry-hov Jun 16, 2023
a0bbcf9
No need to subtests manually
harry-hov Jun 16, 2023
5619816
Merge branch 'master' into hariom/load-pkgs
harry-hov Jun 16, 2023
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
6 changes: 6 additions & 0 deletions examples/gno.land/p/demo/acl/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/p/demo/acl

require (
"gno.land/p/demo/avl" v0.0.0
harry-hov marked this conversation as resolved.
Show resolved Hide resolved
"gno.land/p/demo/testutils" v0.0.0
)
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/avl/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/avl
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/bank/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/bank
6 changes: 6 additions & 0 deletions examples/gno.land/p/demo/blog/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/p/demo/blog

require (
"gno.land/p/demo/avl" v0.0.0
harry-hov marked this conversation as resolved.
Show resolved Hide resolved
"gno.land/p/demo/ufmt" v0.0.0
)
5 changes: 5 additions & 0 deletions examples/gno.land/p/demo/dom/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gno.land/p/demo/dom

require (
"gno.land/p/demo/avl" v0.0.0
)
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/flow/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module "gno.land/p/demo/flow"
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/gnode/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module "gno.land/p/demo/gnode"
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/grc/exts/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/grc/exts
7 changes: 7 additions & 0 deletions examples/gno.land/p/demo/grc/grc1155/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/p/demo/grc/grc1155

require (
"gno.land/p/demo/avl" v0.0.0
"gno.land/r/demo/users" v0.0.0
"gno.land/p/demo/ufmt" v0.0.0
)
7 changes: 7 additions & 0 deletions examples/gno.land/p/demo/grc/grc20/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/p/demo/grc/grc20

require (
"gno.land/p/demo/avl" v0.0.0
"gno.land/p/demo/ufmt" v0.0.0
"gno.land/p/demo/grc/exts" v0.0.0
)
7 changes: 7 additions & 0 deletions examples/gno.land/p/demo/grc/grc721/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/p/demo/grc/grc721

require (
"gno.land/p/demo/avl" v0.0.0
"gno.land/r/demo/users" v0.0.0
"gno.land/p/demo/ufmt" v0.0.0
)
5 changes: 5 additions & 0 deletions examples/gno.land/p/demo/grc/grc777/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gno.land/p/demo/grc/grc777

require (
"gno.land/p/demo/grc/exts" v0.0.0
)
7 changes: 7 additions & 0 deletions examples/gno.land/p/demo/groups/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/p/demo/groups

require (
"gno.land/r/demo/boards" v0.0.0
"gno.land/p/demo/maths" v0.0.0
"gno.land/p/demo/testutils" v0.0.0
)
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/maths/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/maths
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/releases/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/releases
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/stack/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/stack
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/tests/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/tests
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/testutils/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/testutils
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/ufmt/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/ufmt
5 changes: 5 additions & 0 deletions examples/gno.land/r/demo/art/millipede/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gno.land/r/demo/art/millipede

require (
"gno.land/p/demo/ufmt" v0.0.0
)
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/banktest/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/demo/banktest
6 changes: 6 additions & 0 deletions examples/gno.land/r/demo/boards/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/r/demo/boards

require (
"gno.land/p/demo/avl" v0.0.0
"gno.land/r/demo/users" v0.0.0
)
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/deep/very/deep/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/demo/deep/very/deep
7 changes: 7 additions & 0 deletions examples/gno.land/r/demo/foo1155/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/r/demo/foo1155

require (
"gno.land/p/demo/ufmt" v0.0.0
"gno.land/p/demo/grc/grc1155" v0.0.0
"gno.land/r/demo/users" v0.0.0
)
7 changes: 7 additions & 0 deletions examples/gno.land/r/demo/foo20/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/r/demo/foo20

require (
"gno.land/p/demo/ufmt" v0.0.0
"gno.land/p/demo/grc/grc20" v0.0.0
"gno.land/r/demo/users" v0.0.0
)
7 changes: 7 additions & 0 deletions examples/gno.land/r/demo/foo721/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/r/demo/foo721

require (
"gno.land/p/demo/ufmt" v0.0.0
"gno.land/p/demo/grc/grc721" v0.0.0
"gno.land/r/demo/users" v0.0.0
)
6 changes: 6 additions & 0 deletions examples/gno.land/r/demo/groups/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/r/demo/groups

require (
"gno.land/p/demo/avl" v0.0.0
"gno.land/r/demo/users" v0.0.0
)
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/markdown_test/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/demo/markdown_test
6 changes: 6 additions & 0 deletions examples/gno.land/r/demo/nft/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/r/demo/nft

require (
"gno.land/p/demo/avl" v0.0.0
"gno.land/p/demo/grc/grc721" v0.0.0
)
5 changes: 5 additions & 0 deletions examples/gno.land/r/demo/releases_example/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gno.land/r/demo/releases_example

require (
"gno.land/p/demo/releases" v0.0.0
)
5 changes: 5 additions & 0 deletions examples/gno.land/r/demo/tests/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gno.land/r/demo/tests

require (
"gno.land/p/demo/tests" v0.0.0
)
5 changes: 5 additions & 0 deletions examples/gno.land/r/demo/tests_foo/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gno.land/r/demo/tests_foo

require (
"gno.land/r/demo/tests" v0.0.0
)
5 changes: 5 additions & 0 deletions examples/gno.land/r/demo/types/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gno.land/r/demo/types

require (
"gno.land/p/demo/avl" v0.0.0
)
6 changes: 6 additions & 0 deletions examples/gno.land/r/demo/users/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/r/demo/users

require (
"gno.land/p/demo/avl" v0.0.0
"gno.land/p/demo/testutils" v0.0.0
)
6 changes: 6 additions & 0 deletions examples/gno.land/r/gnoland/blog/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/r/gnoland/blog

require (
"gno.land/p/demo/avl" v0.0.0
"gno.land/p/demo/blog" v0.0.0
)
7 changes: 7 additions & 0 deletions examples/gno.land/r/gnoland/faucet/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/r/gnoland/faucet

require (
"gno.land/p/demo/avl" v0.0.0
"gno.land/p/demo/testutils" v0.0.0
"gno.land/p/demo/ufmt" v0.0.0
)
5 changes: 5 additions & 0 deletions examples/gno.land/r/system/names/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gno.land/r/system/names

require (
"gno.land/p/demo/avl" v0.0.0
)
1 change: 1 addition & 0 deletions examples/gno.land/r/system/rewards/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/system/rewards
1 change: 1 addition & 0 deletions examples/gno.land/r/system/validators/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/system/validators
39 changes: 13 additions & 26 deletions gno.land/cmd/gnoland/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/gnolang/gno/gno.land/pkg/gnoland"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/tm2/pkg/amino"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/bft/config"
Expand Down Expand Up @@ -205,33 +206,19 @@ func makeGenesisDoc(
// Load initial packages from examples.
test1 := crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
txs := []std.Tx{}
for _, path := range []string{
"p/demo/ufmt",
"p/demo/avl",
"p/demo/grc/exts",
"p/demo/grc/grc20",
"p/demo/grc/grc721",
"p/demo/grc/grc1155",
"p/demo/maths",
"p/demo/blog",
"r/demo/users",
"r/demo/foo20",
"r/demo/foo1155",
"r/demo/boards",
"r/demo/banktest",
"r/demo/types",
"r/demo/markdown_test",
"r/gnoland/blog",
"r/gnoland/faucet",
"r/system/validators",
"r/system/names",
"r/system/rewards",
"r/demo/deep/very/deep",
} {

// List initial packages to load from examples.
pkgs, err := gnomod.ListPkgs(filepath.Join("..", "examples"))
if err != nil {
panic(fmt.Errorf("listing gno packages: %w", err))
}
if err := gnomod.SortPkgs(pkgs); err != nil {
panic(fmt.Errorf("sorting packages: %w", err))
}

for _, pkg := range pkgs {
// open files in directory as MemPackage.
fsPath := filepath.Join("..", "examples", "gno.land", path)
importPath := "gno.land/" + path
memPkg := gno.ReadMemPackage(fsPath, importPath)
memPkg := gno.ReadMemPackage(pkg.Path(), pkg.Name())
var tx std.Tx
tx.Msgs = []std.Msg{
vmm.MsgAddPackage{
Expand Down
127 changes: 127 additions & 0 deletions gnovm/pkg/gnomod/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package gnomod

import (
"fmt"
"io/fs"
"os"
"path/filepath"
)

type pkg struct {
harry-hov marked this conversation as resolved.
Show resolved Hide resolved
name string
path string
requires []string
}

// Name returns the name of the package.
func (p pkg) Name() string {
return p.name
}

// Path returns the path of the package.
func (p pkg) Path() string {
return p.path
}

// Requires returns the required packages of the package.
func (p pkg) Requires() []string {
return p.requires
}

// sortPkgs sorts the given packages by their dependencies.
func SortPkgs(pkgs []pkg) error {
visited := make(map[string]bool)
onStack := make(map[string]bool)
sortedPkgs := make([]pkg, 0, len(pkgs))

var visit func(pkg pkg) error
visit = func(pkg pkg) error {
if onStack[pkg.name] {
return fmt.Errorf("cycle detected: %s", pkg.name)
}
if visited[pkg.name] {
return nil
}

visited[pkg.name] = true
onStack[pkg.name] = true

// Visit package's dependencies
for _, req := range pkg.requires {
found := false
for _, p := range pkgs {
if p.name == req {
if err := visit(p); err != nil {
return err
}
found = true
break
}
}
if !found {
return fmt.Errorf("missing dependency '%s' for package '%s'", req, pkg.name)
}
}

onStack[pkg.name] = false
sortedPkgs = append(sortedPkgs, pkg)
return nil
}

// Visit all packages
for _, p := range pkgs {
if err := visit(p); err != nil {
return err
}
}

copy(pkgs, sortedPkgs)
return nil
}

// ListPkgs lists all gno packages in the given root directory.
func ListPkgs(root string) ([]pkg, error) {
var pkgs []pkg

err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
goModPath := filepath.Join(path, "gno.mod")
data, err := os.ReadFile(goModPath)
if err == nil {
gnoMod, err := Parse(goModPath, data)
if err != nil {
return fmt.Errorf("parse: %w", err)
}
gnoMod.Sanitize()
if err := gnoMod.Validate(); err != nil {
return fmt.Errorf("validate: %w", err)
}
pkgs = append(pkgs, pkg{
name: gnoMod.Module.Mod.Path,
path: path,
requires: func() []string {
var reqs []string
for _, req := range gnoMod.Require {
reqs = append(reqs, req.Mod.Path)
}
return reqs
}(),
})
return fs.SkipDir
} else if !os.IsNotExist(err) {
return err
}
}

return nil
})
if err != nil {
return nil, err
}

return pkgs, nil
}
Loading