-
Notifications
You must be signed in to change notification settings - Fork 532
/
init.go
162 lines (143 loc) · 3.77 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
153
154
155
156
157
158
159
160
161
162
package scaffolder
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/gobuffalo/genny/v2"
"github.com/ignite/cli/v28/ignite/pkg/cache"
"github.com/ignite/cli/v28/ignite/pkg/cosmosgen"
"github.com/ignite/cli/v28/ignite/pkg/gomodulepath"
"github.com/ignite/cli/v28/ignite/pkg/placeholder"
"github.com/ignite/cli/v28/ignite/pkg/xgit"
"github.com/ignite/cli/v28/ignite/templates/app"
"github.com/ignite/cli/v28/ignite/templates/field"
modulecreate "github.com/ignite/cli/v28/ignite/templates/module/create"
"github.com/ignite/cli/v28/ignite/templates/testutil"
)
// Init initializes a new app with name and given options.
func Init(
ctx context.Context,
cacheStorage cache.Storage,
tracer *placeholder.Tracer,
root, name, addressPrefix string,
noDefaultModule, skipGit, skipProto, minimal, isConsumerChain bool,
params []string,
) (path string, err 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
}
path = filepath.Join(root, appFolder)
// create the project
if err := generate(
ctx,
tracer,
pathInfo,
addressPrefix,
path,
noDefaultModule,
minimal,
isConsumerChain,
params,
); err != nil {
return "", err
}
if err = finish(ctx, cacheStorage, path, pathInfo.RawPath, skipProto); err != nil {
return "", err
}
if !skipGit {
// Initialize git repository and perform the first commit
if err := xgit.InitAndCommit(path); err != nil {
return "", err
}
}
return path, nil
}
//nolint:interfacer
func generate(
ctx context.Context,
tracer *placeholder.Tracer,
pathInfo gomodulepath.Path,
addressPrefix,
absRoot string,
noDefaultModule, minimal, isConsumerChain bool,
params []string,
) error {
// Parse params with the associated type
paramsFields, err := field.ParseFields(params, checkForbiddenTypeIndex)
if err != nil {
return err
}
githubPath := gomodulepath.ExtractAppPath(pathInfo.RawPath)
if !strings.Contains(githubPath, "/") {
// A username must be added when the app module path 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,
GitHubPath: githubPath,
BinaryNamePrefix: pathInfo.Root,
AddressPrefix: addressPrefix,
IsChainMinimal: minimal,
IsConsumerChain: isConsumerChain,
})
if err != nil {
return err
}
// Create the 'testutil' package with the test helpers
if err := testutil.Register(g, absRoot); err != nil {
return err
}
run := func(runner *genny.Runner, gen *genny.Generator) error {
if err := runner.With(gen); err != nil {
return err
}
runner.Root = absRoot
return runner.Run()
}
if err := run(genny.WetRunner(ctx), g); err != nil {
return err
}
if err := cosmosgen.InstallDepTools(ctx, absRoot); err != nil {
return err
}
// generate module template
if !noDefaultModule {
opts := &modulecreate.CreateOptions{
ModuleName: pathInfo.Package, // App name
ModulePath: pathInfo.RawPath,
AppName: pathInfo.Package,
AppPath: absRoot,
Params: paramsFields,
IsIBC: false,
}
// Check if the module name is valid
if err := checkModuleName(opts.AppPath, opts.ModuleName); err != nil {
return err
}
g, err = modulecreate.NewGenerator(opts)
if err != nil {
return err
}
if err := run(genny.WetRunner(ctx), g); err != nil {
return err
}
g = modulecreate.NewAppModify(tracer, opts)
if err := run(genny.WetRunner(ctx), g); err != nil {
return err
}
}
return nil
}