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 gnoutil, centralizing pkg/file matching #1299

Closed
wants to merge 2 commits into from
Closed
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 gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func InitChainer(baseApp *sdk.BaseApp, acctKpr auth.AccountKeeperI, bankKpr bank

// NOTE: comment out to ignore.
if !skipFailingGenesisTxs {
panic(res.Error)
panic(res.Log)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slipping in by accident as most errors here, if we just get res.Error, tend to be internal error. if we feel like we need to discuss it, happy to do another PR.

}
} else {
ctx.Logger().Info("SUCCESS:", string(amino.MustMarshalJSON(tx)))
Expand Down
21 changes: 6 additions & 15 deletions gnovm/cmd/gno/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
"os"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnoutil"
"github.com/gnolang/gno/tm2/pkg/commands"
)

type buildCfg struct {
verbose bool
goBinary string
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the option to specify a gno file as:

  • this is inconsistent with the rest of guessRootDir, which just use "go" as an argument.
  • it is already fairly straightforward to change this in a script; just change your PATH.

verbose bool
}

var defaultBuildOptions = &buildCfg{
verbose: false,
goBinary: "go",
verbose: false,
}

func newBuildCmd(io *commands.IO) *commands.Command {
Expand All @@ -26,7 +25,7 @@
return commands.NewCommand(
commands.Metadata{
Name: "build",
ShortUsage: "build [flags] <package>",
ShortUsage: "build [flags] <file|pkg> [<file|pkg>]",
ShortHelp: "Builds the specified gno package",
},
cfg,
Expand All @@ -43,21 +42,14 @@
defaultBuildOptions.verbose,
"verbose output when building",
)

fs.StringVar(
&c.goBinary,
"go-binary",
defaultBuildOptions.goBinary,
"go binary to use for building",
)
}

func execBuild(cfg *buildCfg, args []string, io *commands.IO) error {
if len(args) < 1 {
return flag.ErrHelp
}

paths, err := gnoPackagesFromArgs(args)
paths, err := gnoutil.Match(args)

Check warning on line 52 in gnovm/cmd/gno/build.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/build.go#L52

Added line #L52 was not covered by tests
if err != nil {
return fmt.Errorf("list packages: %w", err)
}
Expand All @@ -82,11 +74,10 @@

func goBuildFileOrPkg(fileOrPkg string, cfg *buildCfg) error {
verbose := cfg.verbose
goBinary := cfg.goBinary

if verbose {
fmt.Fprintf(os.Stderr, "%s\n", fileOrPkg)
}

return gno.PrecompileBuildPackage(fileOrPkg, goBinary)
return gno.PrecompileBuildPackage(fileOrPkg)

Check warning on line 82 in gnovm/cmd/gno/build.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/build.go#L82

Added line #L82 was not covered by tests
}
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/gnolang/gno/gnovm/pkg/doc"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/gnovm/pkg/gnoutil"
"github.com/gnolang/gno/tm2/pkg/commands"
)

Expand Down Expand Up @@ -77,7 +78,7 @@ func (c *docCfg) RegisterFlags(fs *flag.FlagSet) {
func execDoc(cfg *docCfg, args []string, io *commands.IO) error {
// guess opts.RootDir
if cfg.rootDir == "" {
cfg.rootDir = guessRootDir()
cfg.rootDir = gnoutil.DefaultRootDir()
}

wd, err := os.Getwd()
Expand Down
7 changes: 4 additions & 3 deletions gnovm/cmd/gno/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"

"github.com/gnolang/gno/gnovm/pkg/gnoutil"
"github.com/gnolang/gno/tm2/pkg/commands"
osm "github.com/gnolang/gno/tm2/pkg/os"
)
Expand All @@ -25,7 +26,7 @@ func newLintCmd(io *commands.IO) *commands.Command {
return commands.NewCommand(
commands.Metadata{
Name: "lint",
ShortUsage: "lint [flags] <package> [<package>...]",
ShortUsage: "lint [flags] <file|pkg> [<file|pkg>...]",
ShortHelp: "Runs the linter for the specified packages",
},
cfg,
Expand All @@ -51,10 +52,10 @@ func execLint(cfg *lintCfg, args []string, io *commands.IO) error {
rootDir = cfg.rootDir
)
if rootDir == "" {
rootDir = guessRootDir()
rootDir = gnoutil.DefaultRootDir()
}

pkgPaths, err := gnoPackagesFromArgs(args)
pkgPaths, err := gnoutil.Match(args)
if err != nil {
return fmt.Errorf("list packages from args: %w", err)
}
Expand Down
9 changes: 3 additions & 6 deletions gnovm/cmd/gno/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ func TestLintApp(t *testing.T) {
errShouldBe: "flag: help requested",
}, {
args: []string{"lint", "--set_exit_status=0", "../../tests/integ/run-main/"},
stderrShouldContain: "./../../tests/integ/run-main: missing 'gno.mod' file (code=1).",
stderrShouldContain: "../../tests/integ/run-main: missing 'gno.mod' file (code=1).",
}, {
args: []string{"lint", "--set_exit_status=0", "../../tests/integ/run-main/"},
stderrShouldContain: "./../../tests/integ/run-main: missing 'gno.mod' file (code=1).",
}, {
args: []string{"lint", "--set_exit_status=0", "../../tests/integ/minimalist-gnomod/"},
// TODO: raise an error because there is a gno.mod, but no .gno files
args: []string{"lint", "--set_exit_status=0", "../../tests/integ/minimalist-gnomod/"},
errShouldContain: "no valid gno files",
}, {
args: []string{"lint", "--set_exit_status=0", "../../tests/integ/invalid-module-name/"},
// TODO: raise an error because gno.mod is invalid
Expand Down
5 changes: 3 additions & 2 deletions gnovm/cmd/gno/precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"path/filepath"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnoutil"
"github.com/gnolang/gno/tm2/pkg/commands"
)

Expand Down Expand Up @@ -113,7 +114,7 @@
}

// precompile .gno files.
paths, err := gnoFilesFromArgs(args)
paths, err := gnoutil.Match(args, gnoutil.MatchFiles())

Check warning on line 117 in gnovm/cmd/gno/precompile.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/precompile.go#L117

Added line #L117 was not covered by tests
if err != nil {
return fmt.Errorf("list paths: %w", err)
}
Expand Down Expand Up @@ -143,7 +144,7 @@
}
opts.markAsPrecompiled(pkgPath)

files, err := filepath.Glob(filepath.Join(string(pkgPath), "*.gno"))
files, err := gnoutil.Match([]string{string(pkgPath)}, gnoutil.MatchFiles())

Check warning on line 147 in gnovm/cmd/gno/precompile.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/precompile.go#L147

Added line #L147 was not covered by tests
if err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"os"
"strings"

"github.com/gnolang/gno/gnovm/pkg/gnoutil"
"github.com/gnolang/gno/gnovm/pkg/repl"
"github.com/gnolang/gno/tm2/pkg/commands"
)
Expand Down Expand Up @@ -80,7 +81,7 @@
}

if cfg.rootDir == "" {
cfg.rootDir = guessRootDir()
cfg.rootDir = gnoutil.DefaultRootDir()

Check warning on line 84 in gnovm/cmd/gno/repl.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/repl.go#L84

Added line #L84 was not covered by tests
}

if !cfg.skipUsage {
Expand Down
50 changes: 10 additions & 40 deletions gnovm/cmd/gno/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strings"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnoutil"
"github.com/gnolang/gno/gnovm/tests"
"github.com/gnolang/gno/tm2/pkg/commands"
)
Expand All @@ -26,8 +24,8 @@ func newRunCmd(io *commands.IO) *commands.Command {
return commands.NewCommand(
commands.Metadata{
Name: "run",
ShortUsage: "run [flags] <file> [<file>...]",
ShortHelp: "Runs the specified gno files",
ShortUsage: "run [flags] <file|pkg> [<file|pkg>...]",
ShortHelp: "Runs the specified gno files or packages",
},
cfg,
func(_ context.Context, args []string) error {
Expand Down Expand Up @@ -65,7 +63,7 @@ func execRun(cfg *runCfg, args []string, io *commands.IO) error {
}

if cfg.rootDir == "" {
cfg.rootDir = guessRootDir()
cfg.rootDir = gnoutil.DefaultRootDir()
}

stdin := io.In
Expand Down Expand Up @@ -110,44 +108,16 @@ func execRun(cfg *runCfg, args []string, io *commands.IO) error {
}

func parseFiles(fnames []string) ([]*gno.FileNode, error) {
files := make([]*gno.FileNode, 0, len(fnames))
for _, fname := range fnames {
if s, err := os.Stat(fname); err == nil && s.IsDir() {
subFns, err := listNonTestFiles(fname)
if err != nil {
return nil, err
}
subFiles, err := parseFiles(subFns)
if err != nil {
return nil, err
}
files = append(files, subFiles...)
continue
} else if err != nil {
// either not found or some other kind of error --
// in either case not a file we can parse.
return nil, err
}
files = append(files, gno.MustReadFile(fname))
}
return files, nil
}

func listNonTestFiles(dir string) ([]string, error) {
fs, err := os.ReadDir(dir)
gnoFnames, err := gnoutil.Match(fnames, gnoutil.MatchFiles("!*_test.gno", "!*_filetest.gno"))
if err != nil {
return nil, err
}
fn := make([]string, 0, len(fs))
for _, f := range fs {
n := f.Name()
if isGnoFile(f) &&
!strings.HasSuffix(n, "_test.gno") &&
!strings.HasPrefix(n, "_filetest.gno") {
fn = append(fn, filepath.Join(dir, n))
}

files := make([]*gno.FileNode, 0, len(gnoFnames))
for _, fname := range gnoFnames {
files = append(files, gno.MustReadFile(fname))
}
return fn, nil
return files, nil
}

func runExpr(m *gno.Machine, expr string) {
Expand Down
Loading
Loading