diff --git a/official/wasm/cmd/cmd.go b/official/wasm/cmd/cmd.go index 7b4233ff..c66b695d 100644 --- a/official/wasm/cmd/cmd.go +++ b/official/wasm/cmd/cmd.go @@ -1,13 +1,14 @@ package cmd import ( - "github.com/ignite/cli/v28/ignite/pkg/cliui/colors" - "github.com/ignite/cli/v28/ignite/pkg/xgenny" - "github.com/spf13/cobra" "os" "path/filepath" "sort" "strings" + + "github.com/ignite/cli/v28/ignite/pkg/cliui/colors" + "github.com/ignite/cli/v28/ignite/pkg/xgenny" + "github.com/spf13/cobra" ) const ( diff --git a/official/wasm/go.mod b/official/wasm/go.mod index 457c2992..1761f411 100644 --- a/official/wasm/go.mod +++ b/official/wasm/go.mod @@ -6,6 +6,7 @@ toolchain go1.21.6 require ( github.com/gobuffalo/genny/v2 v2.1.0 + github.com/gobuffalo/plush/v4 v4.1.19 github.com/hashicorp/go-plugin v1.5.2 github.com/ignite/cli/v28 v28.1.1 github.com/spf13/cobra v1.8.0 @@ -73,6 +74,8 @@ require ( github.com/dustin/go-humanize v1.0.1 // indirect github.com/dvsekhvalnov/jose2go v1.5.0 // indirect github.com/emicklei/dot v1.6.0 // indirect + github.com/emicklei/proto v1.12.2 // indirect + github.com/emicklei/proto-contrib v0.15.0 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/fatih/color v1.15.0 // indirect github.com/fatih/structs v1.1.0 // indirect @@ -90,7 +93,6 @@ require ( github.com/gobuffalo/helpers v0.6.7 // indirect github.com/gobuffalo/logger v1.0.7 // indirect github.com/gobuffalo/packd v1.0.2 // indirect - github.com/gobuffalo/plush/v4 v4.1.19 // indirect github.com/gobuffalo/tags/v3 v3.1.4 // indirect github.com/gobuffalo/validate/v3 v3.3.3 // indirect github.com/goccy/go-yaml v1.11.2 // indirect diff --git a/official/wasm/go.sum b/official/wasm/go.sum index 90ed7651..dc9777b0 100644 --- a/official/wasm/go.sum +++ b/official/wasm/go.sum @@ -257,6 +257,10 @@ github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcej github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emicklei/dot v1.6.0 h1:vUzuoVE8ipzS7QkES4UfxdpCwdU2U97m2Pb2tQCoYRY= github.com/emicklei/dot v1.6.0/go.mod h1:DeV7GvQtIw4h2u73RKBkkFdvVAz0D9fzeJrgPW6gy/s= +github.com/emicklei/proto v1.12.2 h1:ZDyDzrfMt7ncmyor/j07uoOCGLKtU5F87vTPwIzLe/o= +github.com/emicklei/proto v1.12.2/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A= +github.com/emicklei/proto-contrib v0.15.0 h1:5D8JKpV1qekMDFwEJp8NVJGY1We6t14dn9D4G05fpyo= +github.com/emicklei/proto-contrib v0.15.0/go.mod h1:p6zmoy14hFYiwUb35X7nJ4u4l1vfvjc1mWrIt8QB3kw= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g= diff --git a/official/wasm/pkg/goanalysis/goanalysis.go b/official/wasm/pkg/goanalysis/goanalysis.go index 97736770..e3631fe9 100644 --- a/official/wasm/pkg/goanalysis/goanalysis.go +++ b/official/wasm/pkg/goanalysis/goanalysis.go @@ -6,11 +6,12 @@ import ( "go/format" "go/parser" "go/token" + "strings" "github.com/ignite/cli/v28/ignite/pkg/errors" ) -// AppendImports inserts import statements into Go source code content. +// AppendImports appends import statements to the existing import block in Go source code content. func AppendImports(fileContent string, importStatements ...string) (modifiedContent string, err error) { fileSet := token.NewFileSet() @@ -20,56 +21,65 @@ func AppendImports(fileContent string, importStatements ...string) (modifiedCont return "", err } - // Check if the import already exists. - existImports := make(map[string]struct{}) + // Find the existing import declaration. + var importDecl *ast.GenDecl for _, decl := range f.Decls { genDecl, ok := decl.(*ast.GenDecl) if !ok || genDecl.Tok != token.IMPORT || len(genDecl.Specs) == 0 { continue } + importDecl = genDecl + break + } - for _, spec := range genDecl.Specs { - importSpec, ok := spec.(*ast.ImportSpec) - if !ok { - continue - } - existImports[importSpec.Path.Value] = struct{}{} + if importDecl == nil { + // If no existing import declaration found, create a new one. + importDecl = &ast.GenDecl{ + Tok: token.IMPORT, + Specs: make([]ast.Spec, 0), + } + f.Decls = append([]ast.Decl{importDecl}, f.Decls...) + } + + // Check existing imports to avoid duplicates. + existImports := make(map[string]struct{}) + for _, spec := range importDecl.Specs { + importSpec, ok := spec.(*ast.ImportSpec) + if !ok { + continue } + existImports[importSpec.Path.Value] = struct{}{} } - newSpecs := make([]ast.Spec, 0) + // Add new import statements. for _, importStatement := range importStatements { + impSplit := strings.Split(importStatement, " ") + var ( + importRepo = impSplit[len(impSplit)-1] + importname = "" + ) + if len(impSplit) > 1 { + importname = impSplit[0] + } + // Check if the import already exists. - if _, ok := existImports[`"`+importStatement+`"`]; ok { + if _, ok := existImports[`"`+importRepo+`"`]; ok { continue } // Create a new import spec. - newSpecs = append(newSpecs, &ast.ImportSpec{ + spec := &ast.ImportSpec{ + Name: &ast.Ident{ + Name: importname, + }, Path: &ast.BasicLit{ Kind: token.STRING, - Value: `"` + importStatement + `"`, + Value: `"` + importRepo + `"`, }, - }) - } - - if len(newSpecs) == 0 { - // No new imports to add. - return fileContent, nil - } - - // Create a new import declaration. - newImportDecl := &ast.GenDecl{ - Tok: token.IMPORT, - Specs: newSpecs, + } + importDecl.Specs = append(importDecl.Specs, spec) } - // Insert the new import declaration at the beginning of the file. - newDecls := append([]ast.Decl{newImportDecl}, f.Decls...) - - // Update the file's declarations. - f.Decls = newDecls - - // Write the modified AST to a buffer. + // Format the modified AST. var buf bytes.Buffer if err := format.Node(&buf, fileSet, f); err != nil { return "", err @@ -134,7 +144,7 @@ func AppendCode(fileContent, functionName, codeToInsert string) (modifiedContent } // ReplaceReturn replaces return statements in a Go function with a new return statement. -func ReplaceReturn(fileContent, functionName, newReturnStatement string) (modifiedContent string, err error) { +func ReplaceReturn(fileContent, functionName string, returnVars ...string) (string, error) { fileSet := token.NewFileSet() // Parse the Go source code content. @@ -143,6 +153,16 @@ func ReplaceReturn(fileContent, functionName, newReturnStatement string) (modifi return "", err } + returnStmts := make([]ast.Expr, 0) + for _, returnVar := range returnVars { + // Parse the new return var to expression. + newRetExpr, err := parser.ParseExpr(returnVar) + if err != nil { + return "", err + } + returnStmts = append(returnStmts, newRetExpr) + } + found := false ast.Inspect(f, func(n ast.Node) bool { if funcDecl, ok := n.(*ast.FuncDecl); ok { @@ -153,18 +173,8 @@ func ReplaceReturn(fileContent, functionName, newReturnStatement string) (modifi if retStmt, ok := stmt.(*ast.ReturnStmt); ok { // Remove existing return statements. retStmt.Results = nil - - // Parse the new return statement. - var buf bytes.Buffer - buf.WriteString(newReturnStatement) - returnExpr, err := parser.ParseExpr(buf.String()) - if err != nil { - return false - } // Add the new return statement. - retStmt.Results = []ast.Expr{returnExpr} - - //retStmt.Results = append(retStmt.Results, newRetExpr) + retStmt.Results = append(retStmt.Results, returnStmts...) } } found = true diff --git a/official/wasm/services/scaffolder/scaffolder.go b/official/wasm/services/scaffolder/scaffolder.go index 89619b41..6bae3719 100644 --- a/official/wasm/services/scaffolder/scaffolder.go +++ b/official/wasm/services/scaffolder/scaffolder.go @@ -1,5 +1,3 @@ -// Package scaffolder initializes Ignite CLI apps and modifies existing ones -// to add more features in a later time. package scaffolder import ( @@ -75,6 +73,15 @@ func AssertSupportedCosmosSDKVersion(v cosmosver.Version) error { } func finish(ctx context.Context, path string) error { + // Add wasmd to the go.mod + if err := gocmd.Get(ctx, path, []string{wasmRepo}); err != nil { + return err + } + + if err := gocmd.ModTidy(ctx, path); err != nil { + return err + } + if err := gocmd.Fmt(ctx, path); err != nil { return err }