Skip to content

Commit

Permalink
feat: utilize ffcli (CLI refactor) (gnolang#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos authored and harry-hov committed Mar 7, 2023
1 parent d70f873 commit 3e44da4
Show file tree
Hide file tree
Showing 67 changed files with 2,923 additions and 2,456 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ jobs:
# TODO: implement --allow-all-imports
#- run: gnodev precompile ./stdlibs --verbose --allow-all-imports
#- run: gnodev precompile ./tests --verbose --allow-all-imports
- run: go run ./cmd/gnodev precompile ./examples --verbose
- run: go run ./cmd/gnodev build ./examples --verbose
- run: go run ./cmd/gnodev precompile --verbose ./examples
- run: go run ./cmd/gnodev build --verbose ./examples
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ clean:
rm -rf build

examples.precompile: install_gnodev
go run ./cmd/gnodev precompile ./examples --verbose
go run ./cmd/gnodev precompile --verbose ./examples

examples.build: install_gnodev examples.precompile
go run ./cmd/gnodev build ./examples --verbose
go run ./cmd/gnodev build --verbose ./examples

########################################
# Formatting, linting.
Expand Down Expand Up @@ -146,10 +146,10 @@ test.packages2:
go test tests/*.go -v -run "TestPackages/bytes" --timeout 30m

test.examples:
go run ./cmd/gnodev test ./examples --verbose
go run ./cmd/gnodev test --verbose ./examples

test.examples.sync:
go run ./cmd/gnodev test ./examples --verbose --update-golden-tests
go run ./cmd/gnodev test --verbose --update-golden-tests ./examples

# Code gen
stringer:
Expand Down
25 changes: 25 additions & 0 deletions cmd/genproto/genproto.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package main

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/pkgs/amino"
"github.com/gnolang/gno/pkgs/amino/genproto"
"github.com/gnolang/gno/pkgs/commands"

// TODO: move these out.
abci "github.com/gnolang/gno/pkgs/bft/abci/types"
Expand All @@ -24,6 +29,22 @@ import (
)

func main() {
cmd := commands.NewCommand(
commands.Metadata{
LongHelp: "Generates proto bindings for Amino packages",
},
commands.NewEmptyConfig(),
execGen,
)

if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v", err)

os.Exit(1)
}
}

func execGen(_ context.Context, _ []string) error {
pkgs := []*amino.Package{
bitarray.Package,
merkle.Package,
Expand All @@ -42,12 +63,16 @@ func main() {
vm.Package,
gno.Package,
}

for _, pkg := range pkgs {
genproto.WriteProto3Schema(pkg)
genproto.WriteProtoBindings(pkg)
genproto.MakeProtoFolder(pkg, "proto")
}

for _, pkg := range pkgs {
genproto.RunProtoc(pkg, "proto")
}

return nil
}
67 changes: 50 additions & 17 deletions cmd/gnodev/build.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,60 @@
package main

import (
"context"
"flag"
"fmt"
"os"

"github.com/gnolang/gno/pkgs/command"
"github.com/gnolang/gno/pkgs/errors"
"github.com/gnolang/gno/pkgs/commands"
gno "github.com/gnolang/gno/pkgs/gnolang"
)

type buildOptions struct {
Verbose bool `flag:"verbose" help:"verbose"`
GoBinary string `flag:"go-binary" help:"go binary to use for building"`
type buildCfg struct {
verbose bool
goBinary string
}

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

func buildApp(cmd *command.Command, args []string, iopts interface{}) error {
opts := iopts.(buildOptions)
func newBuildCmd(io *commands.IO) *commands.Command {
cfg := &buildCfg{}

return commands.NewCommand(
commands.Metadata{
Name: "build",
ShortUsage: "build [flags] <package>",
ShortHelp: "Builds the specified gno package",
},
cfg,
func(_ context.Context, args []string) error {
return execBuild(cfg, args, io)
},
)
}

func (c *buildCfg) RegisterFlags(fs *flag.FlagSet) {
fs.BoolVar(
&c.verbose,
"verbose",
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 {
cmd.ErrPrintfln("Usage: build [build flags] [packages]")
return errors.New("invalid args")
return flag.ErrHelp
}

paths, err := gnoPackagesFromArgs(args)
Expand All @@ -33,23 +64,25 @@ func buildApp(cmd *command.Command, args []string, iopts interface{}) error {

errCount := 0
for _, pkgPath := range paths {
err = goBuildFileOrPkg(pkgPath, opts)
err = goBuildFileOrPkg(pkgPath, cfg)
if err != nil {
err = fmt.Errorf("%s: build pkg: %w", pkgPath, err)
cmd.ErrPrintfln("%s", err.Error())
io.ErrPrintfln("%s\n", err.Error())

errCount++
}
}

if errCount > 0 {
return fmt.Errorf("%d go build errors", errCount)
}

return nil
}

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

if verbose {
fmt.Fprintf(os.Stderr, "%s\n", fileOrPkg)
Expand Down
8 changes: 2 additions & 6 deletions cmd/gnodev/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import "testing"
func TestBuildApp(t *testing.T) {
tc := []testMainCase{
{
args: []string{"build"},
errShouldBe: "invalid args",
stderrShouldBe: "Usage: build [build flags] [packages]\n",
}, {
args: []string{"build", "--help"},
stdoutShouldContain: "# buildOptions options\n-",
args: []string{"build"},
errShouldBe: "flag: help requested",
},

// {args: []string{"build", "..."}, stdoutShouldContain: "..."},
Expand Down
126 changes: 40 additions & 86 deletions cmd/gnodev/main.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,53 @@
package main

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/pkgs/command"
"github.com/gnolang/gno/pkgs/errors"
"github.com/gnolang/gno/pkgs/commands"
)

func main() {
cmd := command.NewStdCommand()
exec := os.Args[0]
args := os.Args[1:]
err := runMain(cmd, exec, args)
if err != nil {
cmd.ErrPrintfln("%s", err.Error())
// cmd.ErrPrintfln("%#v", err)
os.Exit(1)
}
}
cmd := newGnodevCmd(commands.NewDefaultIO())

type (
AppItem = command.AppItem
AppList = command.AppList
)

var mainApps AppList = []AppItem{
{
App: runApp,
Name: "run",
Desc: "run a Gno file",
Defaults: defaultRunOptions,
},
{
App: buildApp,
Name: "build",
Desc: "build a gno package",
Defaults: defaultBuildOptions,
},
{
App: modApp,
Name: "mod",
Desc: "manage gno.mod",
Defaults: defaultModFlags,
},
{
App: precompileApp,
Name: "precompile",
Desc: "precompile .gno to .go",
Defaults: defaultPrecompileFlags,
},
{
App: testApp,
Name: "test",
Desc: "test a gno package",
Defaults: defaultTestOptions,
},
{
App: replApp,
Name: "repl",
Desc: "start a GnoVM REPL",
Defaults: defaultReplOptions,
},
if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v", err)

// fmt -- gofmt
// clean
// graph
// vendor -- download deps from the chain in vendor/
// list -- list packages
// render -- call render()?
// publish/release
// generate
// doc -- godoc
// "vm" -- starts an in-memory chain that can be interacted with?
// bug -- start a bug report
// version -- show gnodev, golang versions
}

func runMain(cmd *command.Command, exec string, args []string) error {
// show help message.
if len(args) == 0 || args[0] == "help" || args[0] == "--help" {
cmd.Println("available subcommands:")
for _, appItem := range mainApps {
cmd.Printf(" %s - %s\n", appItem.Name, appItem.Desc)
}
return nil
}

// switch on first argument.
for _, appItem := range mainApps {
if appItem.Name == args[0] {
err := cmd.Run(appItem.App, args[1:], appItem.Defaults)
return err // done
}
os.Exit(1)
}
}

// unknown app command!
return errors.New("unknown command " + args[0])
func newGnodevCmd(io *commands.IO) *commands.Command {
cmd := commands.NewCommand(
commands.Metadata{
ShortUsage: "<subcommand> [flags] [<arg>...]",
LongHelp: "Runs the gno development toolkit",
},
commands.NewEmptyConfig(),
commands.HelpExec,
)

cmd.AddSubCommands(
newRunCmd(io),
newBuildCmd(io),
newPrecompileCmd(io),
newTestCmd(io),
newReplCmd(),
newModCmd(),
// fmt -- gofmt
// clean
// graph
// vendor -- download deps from the chain in vendor/
// list -- list packages
// render -- call render()?
// publish/release
// generate
// doc -- godoc
// "vm" -- starts an in-memory chain that can be interacted with?
// bug -- start a bug report
// version -- show gnodev, golang versions
)

return cmd
}
Loading

0 comments on commit 3e44da4

Please sign in to comment.