forked from bitrise-steplib/bitrise-step-flutter-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildspec.go
235 lines (196 loc) · 7.04 KB
/
buildspec.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"bytes"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"github.com/bitrise-io/go-steputils/output"
"github.com/bitrise-io/go-steputils/tools"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/sliceutil"
"github.com/bitrise-io/go-utils/ziputil"
"github.com/kballard/go-shellquote"
"github.com/ryanuber/go-glob"
)
type buildSpecification struct {
displayName string
platformOutputType OutputType
platformSelectors []string
outputPathPatterns []string
additionalParameters string
projectLocation string
}
func (spec buildSpecification) exportArtifacts(artifacts []string) error {
deployDir := os.Getenv("BITRISE_DEPLOY_DIR")
switch spec.platformOutputType {
case OutputTypeAPK:
return spec.exportAndroidArtifacts(OutputTypeAPK, artifacts, deployDir)
case OutputTypeAppBundle:
return spec.exportAndroidArtifacts(OutputTypeAppBundle, artifacts, deployDir)
case OutputTypeIOSApp:
return spec.exportIOSApp(artifacts, deployDir)
case OutputTypeArchive:
return spec.exportIOSArchive(artifacts, deployDir)
default:
return fmt.Errorf("unsupported platform for exporting artifacts: %s. Supported platforms: apk, appbundle, app, archive", spec.platformOutputType)
}
}
func (spec buildSpecification) artifactPaths(outputPathPatterns []string, isDir bool) ([]string, error) {
var paths []string
for _, outputPathPattern := range outputPathPatterns {
pths, err := findPaths(spec.projectLocation, outputPathPattern, isDir)
if err != nil {
return nil, err
}
paths = append(paths, pths...)
}
return paths, nil
}
func (spec buildSpecification) exportIOSApp(artifacts []string, deployDir string) error {
artifact := artifacts[len(artifacts)-1]
fileName := filepath.Base(artifact)
if len(artifacts) > 1 {
log.Warnf("- Multiple artifacts found: %v, exporting %s", artifacts, artifact)
}
if err := ziputil.ZipDir(artifact, filepath.Join(deployDir, fileName+".zip"), false); err != nil {
return err
}
log.Donef("- $BITRISE_DEPLOY_DIR/" + fileName + ".zip")
if err := tools.ExportEnvironmentWithEnvman("BITRISE_APP_DIR_PATH", artifact); err != nil {
return err
}
log.Donef("- $BITRISE_APP_DIR_PATH: " + artifact)
return nil
}
func (spec buildSpecification) exportIOSArchive(artifacts []string, deployDir string) error {
artifact := artifacts[len(artifacts)-1]
fileName := filepath.Base(artifact)
if len(artifacts) > 1 {
log.Warnf("- Multiple artifacts found: %v, exporting %s", artifacts, artifact)
}
zipPath := filepath.Join(deployDir, fileName+".zip")
if err := ziputil.ZipDir(artifact, zipPath, false); err != nil {
return err
}
log.Donef("- $BITRISE_DEPLOY_DIR/" + fileName + ".zip")
if err := tools.ExportEnvironmentWithEnvman("BITRISE_XCARCHIVE_PATH", artifact); err != nil {
return err
}
log.Donef("- $BITRISE_XCARCHIVE_PATH: " + artifact)
if err := tools.ExportEnvironmentWithEnvman("BITRISE_XCARCHIVE_ZIP_PATH", zipPath); err != nil {
return err
}
log.Donef("- BITRISE_XCARCHIVE_ZIP_PATH: " + zipPath)
return nil
}
func (spec buildSpecification) exportAndroidArtifacts(androidOutputType OutputType, artifacts []string, deployDir string) error {
artifacts = filterAndroidArtifactsBy(androidOutputType, artifacts)
var singleFileOutputEnvName string
var multipleFileOutputEnvName string
switch spec.platformOutputType {
case "appbundle":
singleFileOutputEnvName = "BITRISE_AAB_PATH"
multipleFileOutputEnvName = "BITRISE_AAB_PATH_LIST"
default:
singleFileOutputEnvName = "BITRISE_APK_PATH"
multipleFileOutputEnvName = "BITRISE_APK_PATH_LIST"
}
var deployedFiles []string
for _, path := range artifacts {
deployedFilePath := filepath.Join(deployDir, filepath.Base(path))
if err := output.ExportOutputFile(path, deployedFilePath, singleFileOutputEnvName); err != nil {
return err
}
deployedFiles = append(deployedFiles, deployedFilePath)
}
if err := tools.ExportEnvironmentWithEnvman(multipleFileOutputEnvName, strings.Join(deployedFiles, "\n")); err != nil {
return fmt.Errorf("failed to export enviroment variable %s, error: %s", multipleFileOutputEnvName, err)
}
deployedSingleFile := ""
if len(deployedFiles) > 0 {
deployedSingleFile = deployedFiles[len(deployedFiles)-1]
}
log.Donef("- " + singleFileOutputEnvName + ": " + deployedSingleFile)
log.Donef("- " + multipleFileOutputEnvName + ": " + strings.Join(deployedFiles, "|"))
return nil
}
func filterAndroidArtifactsBy(androidOutputType OutputType, artifacts []string) []string {
var index int
for _, artifact := range artifacts {
switch androidOutputType {
case OutputTypeAPK:
if path.Ext(artifact) != ".apk" {
log.Debugf("Artifact (%s) found by output patterns, but it's not the selected output type (%s) - Skip", artifact, androidOutputType)
continue // drop artifact
}
case OutputTypeAppBundle:
if path.Ext(artifact) != ".aab" {
log.Debugf("Artifact (%s) found by output patterns, but it's not the selected output type (%s) - Skip", artifact, androidOutputType)
continue // drop artifact
}
}
artifacts[index] = artifact
index++
}
return artifacts[:index]
}
func (spec buildSpecification) buildable(platform string) bool {
return sliceutil.IsStringInSlice(platform, spec.platformSelectors)
}
func findPaths(location string, outputPathPattern string, dir bool) (out []string, err error) {
err = filepath.Walk(location, func(path string, info os.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
}
if !info.IsDir() == dir || !glob.Glob(outputPathPattern, path) {
return nil
}
out = append(out, path)
return nil
})
if len(out) == 0 && err == nil {
log.Debugf("couldn't find output artifact on path: " + filepath.Join(location, outputPathPattern))
}
return
}
func (spec buildSpecification) build(params string) error {
paramSlice, err := shellquote.Split(params)
if err != nil {
return err
}
var errorWriter io.Writer = os.Stderr
var errBuffer bytes.Buffer
var platformCmd string
switch spec.platformOutputType {
case OutputTypeIOSApp:
platformCmd = "ios" // $ flutter build ios -> .app output
case OutputTypeArchive:
platformCmd = "ipa" // $ flutter build ipa -> .xcarchive output
default:
platformCmd = string(spec.platformOutputType)
}
if spec.platformOutputType == OutputTypeIOSApp {
paramSlice = append(paramSlice, "--no-codesign")
}
buildCmd := command.New("flutter", append([]string{"build", platformCmd}, paramSlice...)...).SetStdout(os.Stdout)
if spec.platformOutputType == OutputTypeIOSApp || spec.platformOutputType == OutputTypeArchive {
buildCmd.SetStdin(strings.NewReader("a")) // if the CLI asks to input the selected identity we force it to be aborted
errorWriter = io.MultiWriter(os.Stderr, &errBuffer)
}
buildCmd.SetStderr(errorWriter)
fmt.Println()
log.Donef("$ %s", buildCmd.PrintableCommandArgs())
fmt.Println()
buildCmd.SetDir(spec.projectLocation)
err = buildCmd.Run()
if spec.platformOutputType == OutputTypeIOSApp {
if strings.Contains(strings.ToLower(errBuffer.String()), "code signing is required") {
return errCodeSign
}
}
return err
}