Skip to content

Commit

Permalink
feat: scaffold minimal chain (#3835)
Browse files Browse the repository at this point in the history
* feat: scaffold minimal chain

* add changelog

* updates

* updates

* update tests

* feedback

* lint, scope better pr, add regression test

* update changelog
  • Loading branch information
julienrbrt authored Jan 16, 2024
1 parent 3b49691 commit 1b76c2d
Show file tree
Hide file tree
Showing 20 changed files with 305 additions and 50 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- [#3835](https://github.com/ignite/cli/pull/3835) Add `--minimal` flag to `scaffold chain` to scaffold a chain with the least amount of sdk modules

## [`v28.1.1`](https://github.com/ignite/cli/releases/tag/v28.1.1)

### Fixes
Expand Down
4 changes: 4 additions & 0 deletions ignite/cmd/scaffold_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

const (
flagMinimal = "minimal"
flagNoDefaultModule = "no-module"
flagSkipGit = "skip-git"

Expand Down Expand Up @@ -80,6 +81,7 @@ about Cosmos SDK on https://docs.cosmos.network
c.Flags().Bool(flagNoDefaultModule, false, "create a project without a default module")
c.Flags().StringSlice(flagParams, []string{}, "add default module parameters")
c.Flags().Bool(flagSkipGit, false, "skip Git repository initialization")
c.Flags().Bool(flagMinimal, false, "create a minimal blockchain (with the minimum required Cosmos SDK modules)")

return c
}
Expand All @@ -94,6 +96,7 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error {
appPath = flagGetPath(cmd)
noDefaultModule, _ = cmd.Flags().GetBool(flagNoDefaultModule)
skipGit, _ = cmd.Flags().GetBool(flagSkipGit)
minimal, _ = cmd.Flags().GetBool(flagMinimal)
)

params, err := cmd.Flags().GetStringSlice(flagParams)
Expand All @@ -118,6 +121,7 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error {
addressPrefix,
noDefaultModule,
skipGit,
minimal,
params,
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/scaffold_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

// moduleNameKeeperAlias is a map of well known module names that have a different keeper name than the usual <module-name>Keeper.
var moduleNameKeeperAlias = map[string]string{
"auth": "account",
"auth": "account", // TODO(@julienrbrt) remove this when x/accounts is released
}

const (
Expand Down
6 changes: 6 additions & 0 deletions ignite/config/chain/base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,19 @@ type Config struct {
Faucet Faucet `yaml:"faucet,omitempty"`
Client Client `yaml:"client,omitempty"`
Genesis xyaml.Map `yaml:"genesis,omitempty"`
Minimal bool `yaml:"minimal,omitempty"`
}

// GetVersion returns the config version.
func (c Config) GetVersion() version.Version {
return c.Version
}

// IsChainMinimal returns true if the chain is minimally scaffolded.
func (c Config) IsChainMinimal() bool {
return c.Minimal
}

// SetDefaults assigns default values to empty config fields.
func (c *Config) SetDefaults() error {
return mergo.Merge(c, DefaultConfig())
Expand Down
5 changes: 5 additions & 0 deletions ignite/pkg/cosmosgen/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func DepTools() []string {
// grpc-gateway plugins.
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway",
"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2",

// goimports
"golang.org/x/tools/cmd/goimports",
}
}

Expand All @@ -32,10 +35,12 @@ func InstallDepTools(ctx context.Context, appPath string) error {
if err := gocmd.ModTidy(ctx, appPath); err != nil {
return err
}

err := gocmd.Install(ctx, appPath, DepTools())
if gocmd.IsInstallError(err) {
return errors.New("unable to install dependency tools, run `ignite doctor` and try again")
}

return err
}

Expand Down
5 changes: 5 additions & 0 deletions ignite/pkg/gocmd/gocmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ func PackageLiteral(path, version string) string {
return fmt.Sprintf("%s@%s", path, version)
}

// Imports runs goimports on path with options.
func GoImports(ctx context.Context, path string) error {
return exec.Exec(ctx, []string{"goimports", "-w", path})
}

// binaryPath determines the path where binary will be located at.
func binaryPath(output, binary string) (string, error) {
if output != "" {
Expand Down
7 changes: 4 additions & 3 deletions ignite/services/scaffolder/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Init(
cacheStorage cache.Storage,
tracer *placeholder.Tracer,
root, name, addressPrefix string,
noDefaultModule, skipGit bool,
noDefaultModule, skipGit, minimal bool,
params []string,
) (path string, err error) {
pathInfo, err := gomodulepath.Parse(name)
Expand All @@ -46,7 +46,7 @@ func Init(
path = filepath.Join(root, appFolder)

// create the project
err = generate(ctx, tracer, pathInfo, addressPrefix, path, noDefaultModule, params)
err = generate(ctx, tracer, pathInfo, addressPrefix, path, noDefaultModule, minimal, params)
if err != nil {
return "", err
}
Expand All @@ -73,7 +73,7 @@ func generate(
pathInfo gomodulepath.Path,
addressPrefix,
absRoot string,
noDefaultModule bool,
noDefaultModule, minimal bool,
params []string,
) error {
// Parse params with the associated type
Expand All @@ -96,6 +96,7 @@ func generate(
GitHubPath: githubPath,
BinaryNamePrefix: pathInfo.Root,
AddressPrefix: addressPrefix,
IsChainMinimal: minimal,
})
if err != nil {
return err
Expand Down
7 changes: 5 additions & 2 deletions ignite/services/scaffolder/scaffolder.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ func finish(ctx context.Context, cacheStorage cache.Storage, path, gomodPath str
return err
}

if err := gocmd.ModTidy(ctx, path); err != nil {
if err := gocmd.Fmt(ctx, path); err != nil {
return err
}
return gocmd.Fmt(ctx, path)

_ = gocmd.GoImports(ctx, path) // goimports installation could fail, so ignore the error

return gocmd.ModTidy(ctx, path)
}

func protoc(ctx context.Context, cacheStorage cache.Storage, projectPath, gomodPath string) error {
Expand Down
29 changes: 28 additions & 1 deletion ignite/templates/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"embed"
"fmt"
"io/fs"

"github.com/gobuffalo/genny/v2"
Expand All @@ -16,6 +17,12 @@ import (
//go:embed files/* files/**/*
var files embed.FS

var (
ibcConfig = "app/ibc.go"
minimalAppConfig = "app/app_config_minimal.go"
appConfig = "app/app_config.go"
)

// NewGenerator returns the generator to scaffold a new Cosmos SDK app.
func NewGenerator(opts *Options) (*genny.Generator, error) {
// Remove "files/" prefix
Expand All @@ -24,16 +31,36 @@ func NewGenerator(opts *Options) (*genny.Generator, error) {
return nil, errors.Errorf("generator sub: %w", err)
}
g := genny.New()
if err := g.OnlyFS(subfs, opts.IncludePrefixes, nil); err != nil {

// always exclude minimal app config it will be created later
// app_config_minimal is only used for the minimal app template
excludePrefix := []string{minimalAppConfig}
if opts.IsChainMinimal {
// minimal chain does not have ibc or classic app config
excludePrefix = append(excludePrefix, ibcConfig, appConfig)
}

if err := g.SelectiveFS(subfs, opts.IncludePrefixes, nil, excludePrefix, nil); err != nil {
return g, errors.Errorf("generator fs: %w", err)
}

if opts.IsChainMinimal {
file, err := subfs.Open(fmt.Sprintf("%s.plush", minimalAppConfig))
if err != nil {
return g, errors.Errorf("open minimal app config: %w", err)
}

g.File(genny.NewFile(appConfig, file))
}

ctx := plush.NewContext()
ctx.Set("ModulePath", opts.ModulePath)
ctx.Set("AppName", opts.AppName)
ctx.Set("GitHubPath", opts.GitHubPath)
ctx.Set("BinaryNamePrefix", opts.BinaryNamePrefix)
ctx.Set("AddressPrefix", opts.AddressPrefix)
ctx.Set("DepTools", cosmosgen.DepTools())
ctx.Set("IsChainMinimal", opts.IsChainMinimal)

plushhelpers.ExtendPlushContext(ctx)
g.Transformer(xgenny.Transformer(ctx))
Expand Down
Loading

0 comments on commit 1b76c2d

Please sign in to comment.