Skip to content

Commit

Permalink
feat: add gnoutil, centralizing pkg/file matching
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl committed Oct 26, 2023
1 parent c3866d4 commit 7f68232
Show file tree
Hide file tree
Showing 25 changed files with 752 additions and 920 deletions.
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 @@ import (
"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
verbose bool
}

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

func newBuildCmd(io *commands.IO) *commands.Command {
Expand All @@ -26,7 +25,7 @@ func newBuildCmd(io *commands.IO) *commands.Command {
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 @@ func (c *buildCfg) RegisterFlags(fs *flag.FlagSet) {
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 execBuild(cfg *buildCfg, args []string, io *commands.IO) error {

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 @@ import (
"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 @@ func execPrecompile(cfg *precompileCfg, args []string, io *commands.IO) error {
}

// 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 @@ func precompilePkg(pkgPath importPath, opts *precompileOptions) error {
}
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 @@ import (
"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 @@ func execRepl(cfg *replCfg, args []string) error {
}

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

0 comments on commit 7f68232

Please sign in to comment.