diff --git a/cmd/add.go b/cmd/add.go new file mode 100644 index 0000000000..314c24a720 --- /dev/null +++ b/cmd/add.go @@ -0,0 +1,26 @@ +package cmd + +import ( + "context" + + "github.com/gobuffalo/genny" + "github.com/spf13/cobra" + "github.com/tendermint/starport/templates/add" +) + +var addCmd = &cobra.Command{ + Use: "add [feature]", + Short: "Adds a feature to a project.", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + appName, _ := getAppAndModule(appPath) + g, _ := add.New(&add.Options{ + Feature: args[0], + AppName: appName, + }) + run := genny.WetRunner(context.Background()) + run.With(g) + run.Run() + // fmt.Printf("\nšŸŽ‰ Created a type `%[1]v`.\n\n", args[0]) + }, +} diff --git a/cmd/root.go b/cmd/root.go index 5d90df8a9a..02fe70f8c4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,6 +29,7 @@ func init() { rootCmd.AddCommand(appCmd) rootCmd.AddCommand(typedCmd) rootCmd.AddCommand(serveCmd) + rootCmd.AddCommand(addCmd) serveCmd.Flags().BoolP("verbose", "v", false, "Verbose output") appCmd.Flags().StringP("denom", "d", "token", "Token denomination") rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") diff --git a/templates/add/new.go b/templates/add/new.go new file mode 100644 index 0000000000..375ed177f9 --- /dev/null +++ b/templates/add/new.go @@ -0,0 +1,116 @@ +package add + +import ( + "fmt" + "strings" + + "github.com/gobuffalo/genny" + "github.com/gobuffalo/packr/v2" + "github.com/gobuffalo/plush" + "github.com/gobuffalo/plushgen" +) + +// New ... +func New(opts *Options) (*genny.Generator, error) { + g := genny.New() + g.RunFn(appModify(opts)) + g.RunFn(cmdMainModify(opts)) + if err := g.Box(packr.New("wasm", "./wasm")); err != nil { + return g, err + } + ctx := plush.NewContext() + ctx.Set("AppName", opts.AppName) + ctx.Set("title", strings.Title) + g.Transformer(plushgen.Transformer(ctx)) + g.Transformer(genny.Replace("{{appName}}", opts.AppName)) + return g, nil +} + +const placeholder = "// this line is used by starport scaffolding" +const placeholder2 = "// this line is used by starport scaffolding # 2" +const placeholder3 = "// this line is used by starport scaffolding # 3" +const placeholder4 = "// this line is used by starport scaffolding # 4" +const placeholder5 = "// this line is used by starport scaffolding # 5" +const placeholder6 = "// this line is used by starport scaffolding # 6" +const placeholder7 = "// this line is used by starport scaffolding # 7" + +func appModify(opts *Options) genny.RunFn { + return func(r *genny.Runner) error { + path := "app/app.go" + f, err := r.Disk.Find(path) + if err != nil { + return err + } + template := `%[1]v + "path/filepath" + "github.com/CosmWasm/wasmd/x/wasm" + "github.com/tendermint/tendermint/libs/cli" + "github.com/spf13/viper"` + replacement := fmt.Sprintf(template, placeholder) + content := strings.Replace(f.String(), placeholder, replacement, 1) + + template2 := `%[1]v + wasm.AppModuleBasic{},` + replacement2 := fmt.Sprintf(template2, placeholder2) + content = strings.Replace(content, placeholder2, replacement2, 1) + + template3 := `%[1]v + wasmKeeper wasm.Keeper` + replacement3 := fmt.Sprintf(template3, placeholder3) + content = strings.Replace(content, placeholder3, replacement3, 1) + + template4 := placeholder4 + "\n" + "type WasmWrapper struct { Wasm wasm.WasmConfig `mapstructure:\"wasm\"`}" + ` + var wasmRouter = bApp.Router() + homeDir := viper.GetString(cli.HomeFlag) + wasmDir := filepath.Join(homeDir, "wasm") + + wasmWrap := WasmWrapper{Wasm: wasm.DefaultWasmConfig()} + err := viper.Unmarshal(&wasmWrap) + if err != nil { + panic("error while reading wasm config: " + err.Error()) + } + wasmConfig := wasmWrap.Wasm + supportedFeatures := "staking" + app.wasmKeeper = wasm.NewKeeper(app.cdc, keys[wasm.StoreKey], app.accountKeeper, app.bankKeeper, app.stakingKeeper, wasmRouter, wasmDir, wasmConfig, supportedFeatures, nil, nil) ` + content = strings.Replace(content, placeholder4, template4, 1) + + template5 := `%[1]v + wasm.StoreKey,` + replacement5 := fmt.Sprintf(template5, placeholder5) + content = strings.Replace(content, placeholder5, replacement5, 1) + + template6 := `%[1]v + wasm.NewAppModule(app.wasmKeeper),` + replacement6 := fmt.Sprintf(template6, placeholder6) + content = strings.Replace(content, placeholder6, replacement6, 1) + + template7 := `%[1]v + wasm.ModuleName,` + replacement7 := fmt.Sprintf(template7, placeholder7) + content = strings.Replace(content, placeholder7, replacement7, 1) + + newFile := genny.NewFileS(path, content) + return r.File(newFile) + } +} + +func cmdMainModify(opts *Options) genny.RunFn { + return func(r *genny.Runner) error { + path := fmt.Sprintf("cmd/%[1]vcli/main.go", opts.AppName) + f, err := r.Disk.Find(path) + if err != nil { + return err + } + template := `%[1]v + wasmrest "github.com/CosmWasm/wasmd/x/wasm/client/rest"` + replacement := fmt.Sprintf(template, placeholder) + content := strings.Replace(f.String(), placeholder, replacement, 1) + + template2 := `%[1]v + wasmrest.RegisterRoutes(rs.CliCtx, rs.Mux)` + replacement2 := fmt.Sprintf(template2, placeholder2) + content = strings.Replace(content, placeholder2, replacement2, 1) + newFile := genny.NewFileS(path, content) + return r.File(newFile) + } +} diff --git a/templates/add/options.go b/templates/add/options.go new file mode 100644 index 0000000000..6f7ce53f8f --- /dev/null +++ b/templates/add/options.go @@ -0,0 +1,12 @@ +package add + +// Options ... +type Options struct { + AppName string + Feature string +} + +// Validate that options are usuable +func (opts *Options) Validate() error { + return nil +} diff --git a/templates/app/templates/app/app.go.plush b/templates/app/templates/app/app.go.plush index 30cede689a..3bb0534740 100644 --- a/templates/app/templates/app/app.go.plush +++ b/templates/app/templates/app/app.go.plush @@ -23,6 +23,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/supply" "<%= ModulePath + "/x/" + AppName %>" + // this line is used by starport scaffolding ) const appName = "app" @@ -38,6 +39,7 @@ var ( params.AppModuleBasic{}, supply.AppModuleBasic{}, <%= AppName %>.AppModuleBasic{}, + // this line is used by starport scaffolding # 2 ) maccPerms = map[string][]string{ @@ -74,7 +76,7 @@ type NewApp struct { supplyKeeper supply.Keeper paramsKeeper params.Keeper <%= AppName %>Keeper <%= AppName %>.Keeper - + // this line is used by starport scaffolding # 3 mm *module.Manager sm *module.SimulationManager @@ -92,8 +94,15 @@ func NewInitApp( bApp.SetCommitMultiStoreTracer(traceStore) bApp.SetAppVersion(version.Version) - keys := sdk.NewKVStoreKeys(bam.MainStoreKey, auth.StoreKey, staking.StoreKey, - supply.StoreKey, params.StoreKey, <%= AppName %>.StoreKey) + keys := sdk.NewKVStoreKeys( + bam.MainStoreKey, + auth.StoreKey, + staking.StoreKey, + supply.StoreKey, + params.StoreKey, + <%= AppName %>.StoreKey, + // this line is used by starport scaffolding # 5 + ) tKeys := sdk.NewTransientStoreKeys(staking.TStoreKey, params.TStoreKey) @@ -149,6 +158,8 @@ func NewInitApp( keys[<%= AppName %>.StoreKey], ) + // this line is used by starport scaffolding # 4 + app.mm = module.NewManager( genutil.NewAppModule(app.accountKeeper, app.stakingKeeper, app.BaseApp.DeliverTx), auth.NewAppModule(app.accountKeeper), @@ -156,6 +167,7 @@ func NewInitApp( supply.NewAppModule(app.supplyKeeper, app.accountKeeper), <%= AppName %>.NewAppModule(app.<%= AppName %>Keeper, app.bankKeeper), staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), + // this line is used by starport scaffolding # 6 ) app.mm.SetOrderEndBlockers(staking.ModuleName) @@ -167,6 +179,7 @@ func NewInitApp( <%= AppName %>.ModuleName, supply.ModuleName, genutil.ModuleName, + // this line is used by starport scaffolding # 7 ) app.mm.RegisterRoutes(app.Router(), app.QueryRouter()) diff --git a/templates/app/templates/cmd/{{binaryNamePrefix}}cli/main.go.plush b/templates/app/templates/cmd/{{binaryNamePrefix}}cli/main.go.plush index 9fb7695845..b688861acc 100644 --- a/templates/app/templates/cmd/{{binaryNamePrefix}}cli/main.go.plush +++ b/templates/app/templates/cmd/{{binaryNamePrefix}}cli/main.go.plush @@ -25,6 +25,7 @@ import ( "github.com/tendermint/tendermint/libs/cli" "<%= ModulePath %>/app" + // this line is used by starport scaffolding ) func main() { @@ -146,6 +147,7 @@ func registerRoutes(rs *lcd.RestServer) { client.RegisterRoutes(rs.CliCtx, rs.Mux) authrest.RegisterTxRoutes(rs.CliCtx, rs.Mux) app.ModuleBasics.RegisterRESTRoutes(rs.CliCtx, rs.Mux) + // this line is used by starport scaffolding # 2 } func initConfig(cmd *cobra.Command) error {