-
Notifications
You must be signed in to change notification settings - Fork 531
/
chain_build.go
180 lines (145 loc) · 5.65 KB
/
chain_build.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package ignitecmd
import (
"path/filepath"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/ignite/cli/v29/ignite/pkg/chaincmd"
"github.com/ignite/cli/v29/ignite/pkg/cliui"
"github.com/ignite/cli/v29/ignite/pkg/cliui/colors"
"github.com/ignite/cli/v29/ignite/services/chain"
)
const (
flagCheckDependencies = "check-dependencies"
flagDebug = "debug"
flagOutput = "output"
flagRelease = "release"
flagBuildTags = "build.tags"
flagReleasePrefix = "release.prefix"
flagReleaseTargets = "release.targets"
)
// NewChainBuild returns a new build command to build a blockchain app.
func NewChainBuild() *cobra.Command {
c := &cobra.Command{
Use: "build",
Short: "Build a node binary",
Long: `
The build command compiles the source code of the project into a binary and
installs the binary in the $(go env GOPATH)/bin directory.
You can customize the output directory for the binary using a flag:
ignite chain build --output dist
To compile the binary Ignite first compiles protocol buffer (proto) files into
Go source code. Proto files contain required type and services definitions. If
you're using another program to compile proto files, you can use a flag to tell
Ignite to skip the proto compilation step:
ignite chain build --skip-proto
Afterwards, Ignite install dependencies specified in the go.mod file. By default
Ignite doesn't check that dependencies of the main module stored in the module
cache have not been modified since they were downloaded. To enforce dependency
checking (essentially, running "go mod verify") use a flag:
ignite chain build --check-dependencies
Next, Ignite identifies the "main" package of the project. By default the "main"
package is located in "cmd/{app}d" directory, where "{app}" is the name of the
scaffolded project and "d" stands for daemon. If your project contains more
than one "main" package, specify the path to the one that Ignite should compile
in config.yml:
build:
main: custom/path/to/main
By default the binary name will match the top-level module name (specified in
go.mod) with a suffix "d". This can be customized in config.yml:
build:
binary: mychaind
You can also specify custom linker flags:
build:
ldflags:
- "-X main.Version=development"
- "-X main.Date=01/05/2022T19:54"
To build binaries for a release, use the --release flag. The binaries for one or
more specified release targets are built in a "release/" directory in the
project's source directory. Specify the release targets with GOOS:GOARCH build
tags. If the optional --release.targets is not specified, a binary is created
for your current environment.
ignite chain build --release -t linux:amd64 -t darwin:amd64 -t darwin:arm64
`,
Args: cobra.NoArgs,
RunE: chainBuildHandler,
}
flagSetPath(c)
flagSetClearCache(c)
c.Flags().AddFlagSet(flagSetCheckDependencies())
c.Flags().AddFlagSet(flagSetSkipProto())
c.Flags().AddFlagSet(flagSetDebug())
c.Flags().Bool(flagRelease, false, "build for a release")
c.Flags().StringSliceP(flagReleaseTargets, "t", []string{}, "release targets. Available only with --release flag")
c.Flags().StringSlice(flagBuildTags, []string{}, "parameters to build the chain binary")
c.Flags().String(flagReleasePrefix, "", "tarball prefix for each release target. Available only with --release flag")
c.Flags().StringP(flagOutput, "o", "", "binary output path")
c.Flags().BoolP("verbose", "v", false, "verbose output")
return c
}
func chainBuildHandler(cmd *cobra.Command, _ []string) error {
var (
isRelease, _ = cmd.Flags().GetBool(flagRelease)
releaseTargets, _ = cmd.Flags().GetStringSlice(flagReleaseTargets)
releasePrefix, _ = cmd.Flags().GetString(flagReleasePrefix)
buildTags, _ = cmd.Flags().GetStringSlice(flagBuildTags)
output, _ = cmd.Flags().GetString(flagOutput)
session = cliui.New(
cliui.WithVerbosity(getVerbosity(cmd)),
cliui.StartSpinner(),
)
)
defer session.End()
chainOption := []chain.Option{
chain.KeyringBackend(chaincmd.KeyringBackendTest),
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
chain.CheckCosmosSDKVersion(),
}
if flagGetCheckDependencies(cmd) {
chainOption = append(chainOption, chain.CheckDependencies())
}
c, err := chain.NewWithHomeFlags(cmd, chainOption...)
if err != nil {
return err
}
cacheStorage, err := newCache(cmd)
if err != nil {
return err
}
ctx := cmd.Context()
if isRelease {
releasePath, err := c.BuildRelease(ctx, cacheStorage, buildTags, output, releasePrefix, releaseTargets...)
if err != nil {
return err
}
return session.Printf("🗃 Release created: %s\n", colors.Info(releasePath))
}
binaryName, err := c.Build(ctx, cacheStorage, buildTags, output, flagGetSkipProto(cmd), flagGetDebug(cmd))
if err != nil {
return err
}
if output == "" {
return session.Printf("🗃 Installed. Use with: %s\n", colors.Info(binaryName))
}
binaryPath := filepath.Join(output, binaryName)
return session.Printf("🗃 Binary built at the path: %s\n", colors.Info(binaryPath))
}
func flagSetCheckDependencies() *flag.FlagSet {
usage := "verify that cached dependencies have not been modified since they were downloaded"
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Bool(flagCheckDependencies, false, usage)
return fs
}
func flagGetCheckDependencies(cmd *cobra.Command) (check bool) {
check, _ = cmd.Flags().GetBool(flagCheckDependencies)
return
}
func flagSetDebug() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Bool(flagDebug, false, "build a debug binary")
return fs
}
func flagGetDebug(cmd *cobra.Command) (debug bool) {
debug, _ = cmd.Flags().GetBool(flagDebug)
return
}