8000 feat: (codegen/vuex): add Vuex code generation & refactor by ilgooz · Pull Request #824 · ignite/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: (codegen/vuex): add Vuex code generation & refactor #824

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

Merged
merged 12 commits into from
Mar 16, 2021
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
14 changes: 0 additions & 14 deletions starport/pkg/cmdrunner/cmdrunner.go
8000
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package cmdrunner

import (
"bytes"
"context"
"io"
"os"
"os/exec"

"github.com/pkg/errors"
"github.com/tendermint/starport/starport/pkg/cmdrunner/step"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -189,15 +187,3 @@ func (r *Runner) newCommand(s *step.Step) Executor {
}
return &cmdSignal{c, w}
}

// Exec executes a command with args, it's a shortcut func for basic command executions.
func Exec(ctx context.Context, command string, args ...string) error {
errb := &bytes.Buffer{}

err := New(
DefaultStderr(errb)).
Run(ctx,
step.New(step.Exec(command, args...)))

return errors.Wrap(err, errb.String())
}
78 changes: 78 additions & 0 deletions starport/pkg/cmdrunner/exec/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Package exec provides easy access to command execution for basic uses.
package exec

import (
"bytes"
"context"
"fmt"
"strings"

"github.com/pkg/errors"
"github.com/tendermint/starport/starport/pkg/cmdrunner"
"github.com/tendermint/starport/starport/pkg/cmdrunner/step"
)

type execConfig struct {
stepOptions []step.Option
includeStdLogsToError bool
}

type Option func(*execConfig)

func StepOption(o step.Option) Option {
return func(c *execConfig) {
c.stepOptions = append(c.stepOptions, o)
}
}

func IncludeStdLogsToError() Option {
return func(c *execConfig) {
c.includeStdLogsToError = true
}
}

// Exec executes a command with args, it's a shortcut func for basic command executions.
func Exec(ctx context.Context, fullCommand []string, options ...Option) error {
errb := &bytes.Buffer{}
logs := &bytes.Buffer{}

c := &execConfig{
stepOptions: []step.Option{
step.Exec(fullCommand[0], fullCommand[1:]...),
step.Stdout(logs),
step.Stderr(errb),
},
}

for _, apply := range options {
apply(c)
}

err := cmdrunner.New().Run(ctx, step.New(c.stepOptions...))
if err != nil {
return &Error{
Err: errors.Wrap(err, errb.String()),
Command: fullCommand[0],
StdLogs: logs.String(),
includeStdLogsToError: c.includeStdLogsToError,
}
}

return nil
}

// Error provides detailed errors from the executed program.
type Error struct {
Err error
Command string
StdLogs string // collected logs from code generation tools.
includeStdLogsToError bool
}

func (e *Error) Error() string {
message := fmt.Sprintf("error while running command %s: %s", e.Command, e.Err.Error())
if e.includeStdLogsToError && strings.TrimSpace(e.StdLogs) != "" {
return fmt.Sprintf("%s\n\n%s", message, e.StdLogs)
}
return message
}
84 changes: 84 additions & 0 deletions starport/pkg/cosmosanalysis/module/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package module

import (
"go/ast"
"go/parser"
"go/token"
)

// DiscoverMessages discovers sdk messages defined in a module that resides under modulePath.
func DiscoverMessages(modulePath string) (msgs []string, err error) {
// parse go packages/files under modulePath.
fset := token.NewFileSet()

pkgs, err := parser.ParseDir(fset, modulePath, nil, 0)
if err != nil {
return nil, err
}

// collect all structs under modulePath to find out the ones that satisfy requirements.
structs := make(map[string]requirements)

for _, pkg := range pkgs {
for _, f := range pkg.Files {
ast.Inspect(f, func(n ast.Node) bool {
// look for struct methods.
fdecl, ok := n.(*ast.FuncDecl)
if !ok {
return true
}

// not a method.
if fdecl.Recv == nil {
return true
}

// fname is the name of method.
fname := fdecl.Name.Name

// find the struct name that method belongs to.
t := fdecl.Recv.List[0].Type
sident, ok := t.(*ast.Ident)
if !ok {
sexp, ok := t.(*ast.StarExpr)
if !ok {
return true
}
sident = sexp.X.(*ast.Ident)
}
sname := sident.Name

// mark the requirement that this struct satisfies.
if _, ok := structs[sname]; !ok {
structs[sname] = newRequirements()
}

structs[sname][fname] = true

return true
})
}
}

// checkRequirements checks if all requirements are satisfied.
checkRequirements := func(r requirements) bool {
for _, ok := range r {
if !ok {
return false
}
}
return true
}

for name, reqs := range structs {
if checkRequirements(reqs) {
msgs = append(msgs, name)
}
}

if len(msgs) == 0 {
return nil, ErrModuleNotFound
}

return msgs, nil
}
Loading
0