Skip to content

Commit

Permalink
starport add cosmwasm (ignite#40)
Browse files Browse the repository at this point in the history
* addCmd wasm

* Add cosmwasm
  • Loading branch information
fadeev authored Jul 20, 2020
1 parent 921b3f6 commit 887d17e
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 3 deletions.
26 changes: 26 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -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])
},
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
116 changes: 116 additions & 0 deletions templates/add/new.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
12 changes: 12 additions & 0 deletions templates/add/options.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 16 additions & 3 deletions templates/app/templates/app/app.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -38,6 +39,7 @@ var (
params.AppModuleBasic{},
supply.AppModuleBasic{},
<%= AppName %>.AppModuleBasic{},
// this line is used by starport scaffolding # 2
)

maccPerms = map[string][]string{
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -149,13 +158,16 @@ 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),
bank.NewAppModule(app.bankKeeper, app.accountKeeper),
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)
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/tendermint/tendermint/libs/cli"

"<%= ModulePath %>/app"
// this line is used by starport scaffolding
)

func main() {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 887d17e

Please sign in to comment.