/
init.go
152 lines (136 loc) · 3.68 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package scaffolder
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/ignite/cli/v29/ignite/pkg/cosmosgen"
"github.com/ignite/cli/v29/ignite/pkg/gomodulepath"
"github.com/ignite/cli/v29/ignite/pkg/xgenny"
"github.com/ignite/cli/v29/ignite/templates/app"
"github.com/ignite/cli/v29/ignite/templates/field"
modulecreate "github.com/ignite/cli/v29/ignite/templates/module/create"
"github.com/ignite/cli/v29/ignite/templates/testutil"
)
// Init initializes a new app with name and given options.
func Init(
ctx context.Context,
runner *xgenny.Runner,
root, name, addressPrefix, protoDir string,
noDefaultModule, minimal, isConsumerChain bool,
params, moduleConfigs []string,
) (string, string, error) {
pathInfo, err := gomodulepath.Parse(name)
if err != nil {
return "", "", err
}
// Create a new folder named as the blockchain when a custom path is not specified
var appFolder string
if root == "" {
appFolder = pathInfo.Root
}
if root, err = filepath.Abs(root); err != nil {
return "", "", err
}
var (
path = filepath.Join(root, appFolder)
gomodule = pathInfo.RawPath
)
// create the project
_, err = generate(
ctx,
runner,
pathInfo,
addressPrefix,
protoDir,
path,
noDefaultModule,
minimal,
isConsumerChain,
params,
moduleConfigs,
)
return path, gomodule, err
}
//nolint:interfacer
func generate(
ctx context.Context,
runner *xgenny.Runner,
pathInfo gomodulepath.Path,
addressPrefix,
protoDir,
absRoot string,
noDefaultModule, minimal, isConsumerChain bool,
params, moduleConfigs []string,
) (xgenny.SourceModification, error) {
// Parse params with the associated type
paramsFields, err := field.ParseFields(params, checkForbiddenTypeIndex)
if err != nil {
return xgenny.SourceModification{}, err
}
// Parse configs with the associated type
configsFields, err := field.ParseFields(moduleConfigs, checkForbiddenTypeIndex)
if err != nil {
return xgenny.SourceModification{}, err
}
githubPath := gomodulepath.ExtractAppPath(pathInfo.RawPath)
if !strings.Contains(githubPath, "/") {
// A username must be added when the app module appPath has a single element
githubPath = fmt.Sprintf("username/%s", githubPath)
}
g, err := app.NewGenerator(&app.Options{
// generate application template
ModulePath: pathInfo.RawPath,
AppName: pathInfo.Package,
AppPath: absRoot,
ProtoDir: protoDir,
GitHubPath: githubPath,
BinaryNamePrefix: pathInfo.Root,
AddressPrefix: addressPrefix,
IsChainMinimal: minimal,
IsConsumerChain: isConsumerChain,
})
if err != nil {
return xgenny.SourceModification{}, err
}
// Create the 'testutil' package with the test helpers
if err := testutil.Register(g, absRoot); err != nil {
return xgenny.SourceModification{}, err
}
// generate module template
runner.Root = absRoot
smc, err := runner.RunAndApply(g)
if err != nil {
return smc, err
}
if err := cosmosgen.InstallDepTools(ctx, absRoot); err != nil {
return smc, err
}
if !noDefaultModule {
opts := &modulecreate.CreateOptions{
ModuleName: pathInfo.Package, // App name
ModulePath: pathInfo.RawPath,
AppName: pathInfo.Package,
AppPath: absRoot,
ProtoDir: protoDir,
Params: paramsFields,
Configs: configsFields,
IsIBC: false,
}
// Check if the module name is valid
if err := checkModuleName(opts.AppPath, opts.ModuleName); err != nil {
return smc, err
}
g, err = modulecreate.NewGenerator(opts)
if err != nil {
return smc, err
}
// generate module template
smm, err := runner.RunAndApply(g, modulecreate.NewAppModify(runner.Tracer(), opts))
if err != nil {
return smc, err
}
smc.Merge(smm)
}
return smc, err
}