-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.go
382 lines (317 loc) · 11 KB
/
main.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/bitrise-io/go-android/cache"
utilscache "github.com/bitrise-io/go-steputils/cache"
"github.com/bitrise-io/go-steputils/commandhelper"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/errorutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/retry"
"github.com/kballard/go-shellquote"
)
const (
bitriseGradleResultsTextEnvKey = "BITRISE_GRADLE_RAW_RESULT_TEXT_PATH"
rawGradleResultFileName = "raw-gradle-output.log"
)
// Config ...
type Config struct {
// Gradle Inputs
GradleFile string `env:"gradle_file"`
GradleTasks string `env:"gradle_task,required"`
GradlewPath string `env:"gradlew_path,file"`
GradleOptions string `env:"gradle_options"`
// Export config
AppFileIncludeFilter string `env:"app_file_include_filter,required"`
AppFileExcludeFilter string `env:"app_file_exclude_filter"`
TestApkFileIncludeFilter string `env:"test_apk_file_include_filter"`
TestApkFileExcludeFilter string `env:"test_apk_file_exclude_filter"`
MappingFileIncludeFilter string `env:"mapping_file_include_filter"`
MappingFileExcludeFilter string `env:"mapping_file_exclude_filter"`
// Debug
CacheLevel string `env:"cache_level,opt['all','only_deps','none']"`
// Other configs
DeployDir string `env:"BITRISE_DEPLOY_DIR"`
}
func runGradleTask(gradleTool, buildFile, tasks, options string, destDir string) error {
optionSlice, err := shellquote.Split(options)
if err != nil {
return err
}
taskSlice, err := shellquote.Split(tasks)
if err != nil {
return err
}
cmdSlice := []string{gradleTool}
if buildFile != "" {
cmdSlice = append(cmdSlice, "--build-file", buildFile)
}
cmdSlice = append(cmdSlice, taskSlice...)
cmdSlice = append(cmdSlice, optionSlice...)
fmt.Println()
log.Donef("$ %s", command.PrintableCommandArgs(false, cmdSlice))
fmt.Println()
cmd := command.New(cmdSlice[0], cmdSlice[1:]...)
if shouldSaveOutputToLogFile(optionSlice) { // Do not write to stdout as debug log may contain sensitive information
rawOutputLogPath := filepath.Join(destDir, rawGradleResultFileName)
return commandhelper.RunAndExportOutput(*cmd, rawOutputLogPath, bitriseGradleResultsTextEnvKey, 20)
}
cmd.SetStdout(os.Stdout)
cmd.SetStderr(os.Stderr)
if err := cmd.Run(); err != nil {
if errorutil.IsExitStatusError(err) {
return err
}
return fmt.Errorf("could not run gradlew command: %v", err)
}
return nil
}
func shouldSaveOutputToLogFile(options []string) bool {
for _, option := range options {
if option == "--debug" || option == "-d" {
return true
}
}
return false
}
func filterEmpty(in []string) (out []string) {
for _, item := range in {
if strings.TrimSpace(item) != "" {
out = append(out, item)
}
}
return
}
func createDeployPth(deployDir, apkName string) (string, error) {
deployPth := filepath.Join(deployDir, apkName)
if exist, err := pathutil.IsPathExists(deployPth); err != nil {
return "", err
} else if exist {
return "", fmt.Errorf("file already exists at: %s", deployPth)
}
return deployPth, nil
}
func findDeployPth(deployDir, baseName, ext string) (string, error) {
deployPth := ""
retryApkName := baseName + ext
err := retry.Times(10).Wait(1 * time.Second).Try(func(attempt uint) error {
requestedPath := filepath.Join(deployDir, retryApkName)
if attempt > 0 {
log.Warnf("Trying %s instead", requestedPath)
}
pth, pathErr := createDeployPth(deployDir, retryApkName)
if pathErr != nil {
log.Warnf("Couldn't open %s for writing: %s", requestedPath, pathErr.Error())
}
t := time.Now()
retryApkName = baseName + t.Format("20060102150405") + ext
deployPth = pth
return pathErr
})
return deployPth, err
}
func validateAndMigrateConfig(config *Config) error {
if config.GradleFile != "" {
if exist, err := pathutil.IsPathExists(config.GradleFile); err != nil {
return fmt.Errorf("failed to check if GradleFile exists at: %s: %s", config.GradleFile, err)
} else if !exist {
return fmt.Errorf("GradleFile does not exist at: %s", config.GradleFile)
}
}
return nil
}
func exportEnvironmentWithEnvman(keyStr, valueStr string) error {
cmd := command.New("envman", "add", "--key", keyStr)
cmd.SetStdin(strings.NewReader(valueStr))
return cmd.Run()
}
func failf(message string, args ...interface{}) {
log.Errorf(message, args...)
os.Exit(1)
}
func main() {
var configs Config
if err := stepconf.Parse(&configs); err != nil {
failf("Issue with input: %s", err)
}
stepconf.Print(configs)
if err := validateAndMigrateConfig(&configs); err != nil {
failf("Issue with input: %s", err)
}
fmt.Println()
gradlewPath, err := filepath.Abs(configs.GradlewPath)
if err != nil {
failf("Can't get absolute path for gradlew file (%s): %s", configs.GradlewPath, err)
}
if err := os.Chmod(gradlewPath, 0770); err != nil {
failf("Failed to add executable permission on gradlew file (%s): %s", gradlewPath, err)
}
gradleStarted := time.Now()
log.Infof("Running gradle task...")
if err := runGradleTask(gradlewPath, configs.GradleFile, configs.GradleTasks, configs.GradleOptions, configs.DeployDir); err != nil {
failf("Gradle task failed: %s", err)
}
// Collecting caches
fmt.Println()
log.Infof("Collecting cache:")
if warning := cache.Collect(filepath.Dir(gradlewPath), utilscache.Level(configs.CacheLevel)); warning != nil {
log.Warnf("%s", warning)
}
// Move apk and aab files
fmt.Println()
log.Infof("Move APK and AAB files...")
appFiles, err := findArtifacts(".",
filePatterns{
include: filterEmpty(strings.Split(configs.AppFileIncludeFilter, "\n")),
exclude: filterEmpty(strings.Split(configs.AppFileExcludeFilter, "\n")),
})
if err != nil {
failf("Failed to find APK or AAB files: %s", err)
}
if len(appFiles) == 0 {
log.Warnf("No file name matched app filters")
}
var copiedApkFiles []string
var copiedAabFiles []string
for _, appFile := range appFiles {
fi, err := os.Lstat(appFile)
if err != nil {
failf("Failed to get file info: %s", err)
}
if fi.ModTime().Before(gradleStarted) {
log.Warnf("skipping: %s, modified before the gradle task has started", appFile)
continue
}
ext := filepath.Ext(appFile)
baseName := filepath.Base(appFile)
baseName = strings.TrimSuffix(baseName, ext)
fileName := baseName + ext
log.Printf("Copying %s --> %s", appFile, filepath.Join(configs.DeployDir, fileName))
deployPth, err := findDeployPth(configs.DeployDir, baseName, ext)
if err != nil {
failf("Failed to create deploy path for %s: %s", fileName, err)
}
if err := command.CopyFile(appFile, deployPth); err != nil {
failf("Failed to copy %s: %s", fileName, err)
}
switch strings.ToLower(ext) {
case ".apk":
copiedApkFiles = append(copiedApkFiles, deployPth)
case ".aab":
copiedAabFiles = append(copiedAabFiles, deployPth)
default:
}
}
for appEnv, appFiles := range map[string][]string{
"BITRISE_APK_PATH": copiedApkFiles,
"BITRISE_AAB_PATH": copiedAabFiles} {
if len(appFiles) != 0 {
lastCopiedFile := appFiles[len(appFiles)-1]
if err := exportEnvironmentWithEnvman(appEnv, lastCopiedFile); err != nil {
failf("Failed to export environment (%s): %s", appEnv, err)
}
log.Donef("The app path is now available in the Environment Variable: $%s (value: %s)", appEnv, lastCopiedFile)
}
}
for appListEnv, appFiles := range map[string][]string{
"BITRISE_APK_PATH_LIST": copiedApkFiles,
"BITRISE_AAB_PATH_LIST": copiedAabFiles} {
if len(appFiles) != 0 {
appList := strings.Join(appFiles, "|")
if err := exportEnvironmentWithEnvman(appListEnv, appList); err != nil {
failf("Failed to export environment (%s): %s", appListEnv, err)
}
log.Donef("The app paths list is now available in the Environment Variable: $%s (value: %s)", appListEnv, appList)
}
}
testApkFiles, err := findArtifacts(".",
filePatterns{
include: filterEmpty(strings.Split(configs.TestApkFileIncludeFilter, "\n")),
exclude: filterEmpty(strings.Split(configs.TestApkFileExcludeFilter, "\n")),
})
if err != nil {
failf("Failed to find test apk files: %s", err)
}
if len(testApkFiles) == 0 {
log.Warnf("No file name matched test apk filters")
}
lastCopiedTestApkFile := ""
for _, apkFile := range testApkFiles {
fi, err := os.Lstat(apkFile)
if err != nil {
failf("Failed to get file info: %s", err)
}
if fi.ModTime().Before(gradleStarted) {
log.Warnf("skipping: %s, modified before the gradle task has started", apkFile)
continue
}
ext := filepath.Ext(apkFile)
baseName := filepath.Base(apkFile)
baseName = strings.TrimSuffix(baseName, ext)
fileName := baseName + ext
log.Printf("Copying %s --> %s", apkFile, filepath.Join(configs.DeployDir, fileName))
deployPth, err := findDeployPth(configs.DeployDir, baseName, ext)
if err != nil {
failf("Failed to create deploy path for %s: %s", fileName, err)
}
if err := command.CopyFile(apkFile, deployPth); err != nil {
failf("Failed to copy %s: %s", fileName, err)
}
lastCopiedTestApkFile = deployPth
}
if lastCopiedTestApkFile != "" {
if err := exportEnvironmentWithEnvman("BITRISE_TEST_APK_PATH", lastCopiedTestApkFile); err != nil {
failf("Failed to export environment (BITRISE_TEST_APK_PATH): %s", err)
}
log.Donef("The apk path is now available in the Environment Variable: $BITRISE_TEST_APK_PATH (value: %s)", lastCopiedTestApkFile)
}
// Move mapping files
log.Infof("Move mapping files...")
mappingFiles, err := findArtifacts(".",
filePatterns{
include: filterEmpty(strings.Split(configs.MappingFileIncludeFilter, "\n")),
exclude: filterEmpty(strings.Split(configs.MappingFileExcludeFilter, "\n")),
})
if err != nil {
failf("Failed to find mapping files: %s", err)
}
if len(mappingFiles) == 0 {
log.Printf("No mapping file matched the filters")
}
lastCopiedMappingFile := ""
for _, mappingFile := range mappingFiles {
fi, err := os.Lstat(mappingFile)
if err != nil {
failf("Failed to get file info: %s", err)
}
if fi.ModTime().Before(gradleStarted) {
log.Warnf("skipping: %s, modified before the gradle task has started", mappingFile)
continue
}
ext := filepath.Ext(mappingFile)
baseName := filepath.Base(mappingFile)
baseName = strings.TrimSuffix(baseName, ext)
fileName := baseName + ext
log.Printf("Copying %s --> %s", mappingFile, filepath.Join(configs.DeployDir, fileName))
deployPth, err := findDeployPth(configs.DeployDir, baseName, ext)
if err != nil {
failf("Failed to create deploy path for %s: %s", fileName, err)
}
if err := command.CopyFile(mappingFile, deployPth); err != nil {
failf("Failed to copy %s: %s", fileName, err)
}
lastCopiedMappingFile = deployPth
}
if lastCopiedMappingFile != "" {
if err := exportEnvironmentWithEnvman("BITRISE_MAPPING_PATH", lastCopiedMappingFile); err != nil {
failf("Failed to export environment (BITRISE_MAPPING_PATH): %s", err)
}
log.Donef("The mapping path is now available in the Environment Variable: $BITRISE_MAPPING_PATH (value: %s)", lastCopiedMappingFile)
}
}