forked from bitrise-steplib/steps-ionic-archive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
624 lines (505 loc) · 17.3 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"reflect"
"regexp"
"sort"
"strings"
"time"
"github.com/bitrise-io/go-utils/colorstring"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/sliceutil"
"github.com/bitrise-io/go-utils/ziputil"
"github.com/bitrise-tools/go-steputils/input"
"github.com/bitrise-tools/go-steputils/tools"
ver "github.com/hashicorp/go-version"
shellquote "github.com/kballard/go-shellquote"
"github.com/pkg/errors"
)
const (
ipaPathEnvKey = "BITRISE_IPA_PATH"
appZipPathEnvKey = "BITRISE_APP_PATH"
appDirPathEnvKey = "BITRISE_APP_DIR_PATH"
dsymDirPathEnvKey = "BITRISE_DSYM_DIR_PATH"
dsymZipPathEnvKey = "BITRISE_DSYM_PATH"
apkPathEnvKey = "BITRISE_APK_PATH"
apkPathListEnvKey = "BITRISE_APK_PATH_LIST"
)
// ConfigsModel ...
type ConfigsModel struct {
Remove string
Platform string
Configuration string
Target string
BuildConfig string
Options string
Username string
Password string
IonicVersion string
CordovaVersion string
CordovaIosVersion string
CordovaAndroidVersion string
WorkDir string
DeployDir string
}
func createConfigsModelFromEnvs() ConfigsModel {
return ConfigsModel{
Remove: os.Getenv("remove"),
Platform: os.Getenv("platform"),
Configuration: os.Getenv("configuration"),
Target: os.Getenv("target"),
BuildConfig: os.Getenv("build_config"),
Options: os.Getenv("options"),
Username: os.Getenv("ionic_username"),
Password: os.Getenv("ionic_password"),
IonicVersion: os.Getenv("ionic_version"),
CordovaVersion: os.Getenv("cordova_version"),
CordovaIosVersion: os.Getenv("cordova_ios_version"),
CordovaAndroidVersion: os.Getenv("cordova_android_version"),
WorkDir: os.Getenv("workdir"),
DeployDir: os.Getenv("BITRISE_DEPLOY_DIR"),
}
}
func (configs ConfigsModel) print() {
log.Infof("Configs:")
log.Printf("- Remove: %s", configs.Remove)
log.Printf("- Platform: %s", configs.Platform)
log.Printf("- Configuration: %s", configs.Configuration)
log.Printf("- Target: %s", configs.Target)
log.Printf("- BuildConfig: %s", configs.BuildConfig)
log.Printf("- Options: %s", configs.Options)
log.Printf("- Username: %s", input.SecureInput(configs.Username))
log.Printf("- Password: %s", input.SecureInput(configs.Password))
log.Printf("- IonicVersion: %s", configs.IonicVersion)
log.Printf("- CordovaVersion: %s", configs.CordovaVersion)
log.Printf("- CordovaIosVersion: %s", configs.CordovaIosVersion)
log.Printf("- CordovaAndroidVersion: %s", configs.CordovaAndroidVersion)
log.Printf("- WorkDir: %s", configs.WorkDir)
log.Printf("- DeployDir: %s", configs.DeployDir)
}
func (configs ConfigsModel) validate() error {
if err := input.ValidateIfDirExists(configs.WorkDir); err != nil {
return fmt.Errorf("WorkDir: %s", err)
}
if err := input.ValidateWithOptions(configs.Platform, "ios,android", "ios", "android"); err != nil {
return fmt.Errorf("Platform: %s", err)
}
if err := input.ValidateIfNotEmpty(configs.Configuration); err != nil {
return fmt.Errorf("Configuration: %s", err)
}
if err := input.ValidateIfNotEmpty(configs.Target); err != nil {
return fmt.Errorf("Target: %s", err)
}
return nil
}
func moveAndExportOutputs(outputs []string, deployDir, envKey string, envListKey string) (string, []string, error) {
outputToExport := ""
APKPaths := make([]string, len(outputs))
for x, output := range outputs {
info, err := os.Lstat(output)
if err != nil {
return "", []string{}, err
}
if info.Mode()&os.ModeSymlink != 0 {
resolvedPth, err := os.Readlink(output)
if err != nil {
return "", []string{}, err
}
log.Warnf("Output: %s is a symlink to: %s", output, resolvedPth)
if exist, err := pathutil.IsPathExists(resolvedPth); err != nil {
return "", []string{}, err
} else if !exist {
return "", []string{}, fmt.Errorf("resolved path: %s does not exist", resolvedPth)
}
resolvedInfo, err := os.Lstat(resolvedPth)
if err != nil {
return "", []string{}, err
}
if resolvedInfo.Mode()&os.ModeSymlink != 0 {
return "", []string{}, fmt.Errorf("resolved path: %s is still symlink", resolvedPth)
}
output = resolvedPth
info = resolvedInfo
}
fileName := filepath.Base(output)
destinationPth := filepath.Join(deployDir, fileName)
if info.IsDir() {
if err := command.CopyDir(output, destinationPth, false); err != nil {
return "", []string{}, err
}
} else {
if err := command.CopyFile(output, destinationPth); err != nil {
return "", []string{}, err
}
}
outputToExport = destinationPth
APKPaths[x] = destinationPth
}
if outputToExport == "" {
return "", []string{}, nil
}
if err := tools.ExportEnvironmentWithEnvman(envKey, outputToExport); err != nil {
return "", []string{}, err
}
if err := tools.ExportEnvironmentWithEnvman(envListKey, strings.Join(APKPaths, "|")); err != nil {
return "", []string{}, err
}
return outputToExport, APKPaths, nil
}
func ionicVersion() (*ver.Version, error) {
cmd := command.New("ionic", "-v")
cmd.SetStdin(strings.NewReader("Y"))
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
return nil, err
}
// fix for ionic-cli intercative version output: `[1000D[K3.2.0`
pattern := `(?P<version>\d+\.\d+\.\d+)`
reader := strings.NewReader(out)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
if match := regexp.MustCompile(pattern).FindStringSubmatch(line); len(match) == 2 {
versionStr := match[1]
version, err := ver.NewVersion(versionStr)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse version: %s", versionStr)
}
return version, nil
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return nil, fmt.Errorf("output: %s", out)
}
func cordovaVersion() (*ver.Version, error) {
cmd := command.New("cordova", "-v")
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
return nil, err
}
version, err := ver.NewVersion(out)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse version: %s", out)
}
return version, nil
}
func fail(format string, v ...interface{}) {
log.Errorf(format, v...)
os.Exit(1)
}
func getField(c ConfigsModel, field string) string {
r := reflect.ValueOf(c)
f := reflect.Indirect(r).FieldByName(field)
return string(f.String())
}
func findArtifact(dir, ext string, buildStart time.Time) ([]string, error) {
var matches []string
if walkErr := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
if fi.ModTime().Before(buildStart) {
return nil
}
if filepath.Ext(path) == "."+ext {
matches = append(matches, path)
}
return err
}); walkErr != nil {
return nil, walkErr
}
return matches, nil
}
func main() {
configs := createConfigsModelFromEnvs()
fmt.Println()
configs.print()
if err := configs.validate(); err != nil {
fail("Issue with input: %s", err)
}
// Change dir to working directory
workDir, err := pathutil.AbsPath(configs.WorkDir)
if err != nil {
fail("Failed to expand WorkDir (%s), error: %s", configs.WorkDir, err)
}
currentDir, err := pathutil.CurrentWorkingDirectoryAbsolutePath()
if err != nil {
fail("Failed to get current directory, error: %s", err)
}
if workDir != currentDir {
fmt.Println()
log.Infof("Switch working directory to: %s", workDir)
revokeFunc, err := pathutil.RevokableChangeDir(workDir)
if err != nil {
fail("Failed to change working directory, error: %s", err)
}
defer func() {
fmt.Println()
log.Infof("Reset working directory")
if err := revokeFunc(); err != nil {
fail("Failed to reset working directory, error: %s", err)
}
}()
}
// Update cordova and ionic version
if configs.CordovaVersion != "" {
fmt.Println()
log.Infof("Updating cordova version to: %s", configs.CordovaVersion)
if err := npmRemove(false, "cordova"); err != nil {
fail("Failed to remove cordova, error: %s", err)
}
if err := npmInstall(true, "cordova@"+configs.CordovaVersion); err != nil {
fail("Failed to install cordova, error: %s", err)
}
}
if configs.IonicVersion != "" {
fmt.Println()
log.Infof("Updating ionic version to: %s", configs.IonicVersion)
if err := npmRemove(false, "ionic"); err != nil {
fail("Failed to remove ionic, error: %s", err)
}
if err := npmInstall(true, "ionic@"+configs.IonicVersion); err != nil {
fail("Failed to install ionic, error: %s", err)
}
fmt.Println()
log.Infof("Installing local ionic cli")
if err := npmInstall(false, "ionic@"+configs.IonicVersion); err != nil {
fail("command failed, error: %s", err)
}
}
// Print cordova and ionic version
cordovaVer, err := cordovaVersion()
if err != nil {
fail("Failed to get cordova version, error: %s", err)
}
fmt.Println()
log.Printf("cordova version: %s", colorstring.Green(cordovaVer.String()))
ionicVer, err := ionicVersion()
if err != nil {
fail("Failed to get ionic version, error: %s", err)
}
log.Printf("ionic version: %s", colorstring.Green(ionicVer.String()))
// Ionic CLI plugins angular and cordova have been marked as deprecated for
// version 3.8.0 and above.
ionicVerConstraint, err := ver.NewConstraint("< 3.8.0")
if err != nil {
fail("Could not create version constraint for ionic: %s", err)
}
if ionicVerConstraint.Check(ionicVer) {
fmt.Println()
log.Infof("Installing cordova and angular plugins")
if err := npmInstall(false, "@ionic/cli-plugin-ionic-angular@latest", "@ionic/cli-plugin-cordova@latest"); err != nil {
fail("command failed, error: %s", err)
}
}
//
// ionic login
if configs.Username != "" && configs.Password != "" {
fmt.Println()
log.Infof("Ionic login")
cmdArgs := []string{"ionic", "login", configs.Username, configs.Password}
cmd := command.New(cmdArgs[0], cmdArgs[1:]...)
cmd.SetStdout(os.Stdout).SetStderr(os.Stderr).SetStdin(strings.NewReader("y"))
log.Donef("$ ionic login *** ***")
if err := cmd.Run(); err != nil {
fail("command failed, error: %s", err)
}
}
ionicMajorVersion := ionicVer.Segments()[0]
platforms := strings.Split(configs.Platform, ",")
for i, p := range platforms {
platforms[i] = strings.TrimSpace(p)
}
sort.Strings(platforms)
// ionic prepare
fmt.Println()
log.Infof("Building project")
if configs.Remove == "yes" {
// platform rm
for _, platform := range platforms {
cmdArgs := []string{"ionic"}
if ionicMajorVersion > 2 {
cmdArgs = append(cmdArgs, "cordova")
}
cmdArgs = append(cmdArgs, "platform", "rm")
cmdArgs = append(cmdArgs, platform)
cmd := command.New(cmdArgs[0], cmdArgs[1:]...)
cmd.SetStdout(os.Stdout).SetStderr(os.Stderr).SetStdin(strings.NewReader("y"))
log.Donef("$ %s", cmd.PrintableCommandArgs())
if err := cmd.Run(); err != nil {
fail("command failed, error: %s", err)
}
}
}
{
// platform add
for _, platform := range platforms {
cmdArgs := []string{"ionic"}
if ionicMajorVersion > 2 {
cmdArgs = append(cmdArgs, "cordova")
}
cmdArgs = append(cmdArgs, "platform", "add")
platformVersion := platform
pv := getField(configs, "Cordova"+strings.Title(platform)+"Version")
if pv == "master" {
platformVersion = "https://github.com/apache/cordova-" + platform + ".git"
} else if pv != "" {
platformVersion = platform + "@" + pv
}
cmdArgs = append(cmdArgs, platformVersion)
cmd := command.New(cmdArgs[0], cmdArgs[1:]...)
cmd.SetStdout(os.Stdout).SetStderr(os.Stderr).SetStdin(strings.NewReader("y"))
log.Donef("$ %s", cmd.PrintableCommandArgs())
if err := cmd.Run(); err != nil {
fail("command failed, error: %s", err)
}
}
}
buildStart := time.Now()
{
// build
options := []string{}
if configs.Options != "" {
opts, err := shellquote.Split(configs.Options)
if err != nil {
fail("Failed to shell split Options (%s), error: %s", configs.Options, err)
}
options = opts
}
for _, platform := range platforms {
cmdArgs := []string{"ionic"}
if ionicMajorVersion > 2 {
cmdArgs = append(cmdArgs, "cordova")
}
cmdArgs = append(cmdArgs, "build")
cmdArgs = append(cmdArgs, platform)
if configs.Configuration != "" {
cmdArgs = append(cmdArgs, "--"+configs.Configuration)
}
if configs.Target != "" {
cmdArgs = append(cmdArgs, "--"+configs.Target)
}
if configs.BuildConfig != "" {
cmdArgs = append(cmdArgs, "--buildConfig", configs.BuildConfig)
}
cmdArgs = append(cmdArgs, options...)
if "ios" == platform {
cmdArgs = append(cmdArgs, "--")
cmdArgs = append(cmdArgs, "--buildFlag='-UseModernBuildSystem=0'")
}
cmdArgs = append(cmdArgs)
cmd := command.New(cmdArgs[0], cmdArgs[1:]...)
cmd.SetStdout(os.Stdout).SetStderr(os.Stderr).SetStdin(strings.NewReader("y"))
log.Donef("$ %s", cmd.PrintableCommandArgs())
if err := cmd.Run(); err != nil {
fail("command failed, error: %s", err)
}
}
}
// collect outputs
var ipas, dsyms, apps []string
iosOutputDir := filepath.Join(workDir, "platforms", "ios", "build", configs.Target)
if exist, err := pathutil.IsDirExists(iosOutputDir); err != nil {
fail("Failed to check if dir (%s) exist, error: %s", iosOutputDir, err)
} else if exist {
log.Donef("\n\nIOS output dir exists!\n\n")
fmt.Println()
log.Infof("Collecting ios outputs")
// ipa
ipas, err = findArtifact(iosOutputDir, "ipa", buildStart)
if err != nil {
fail("Failed to find ipas in dir (%s), error: %s", iosOutputDir, err)
}
if len(ipas) > 0 {
if exportedPth, _, err := moveAndExportOutputs(ipas, configs.DeployDir, ipaPathEnvKey, apkPathListEnvKey); err != nil {
fail("Failed to export ipas, error: %s", err)
} else if exportedPth != "" {
log.Donef("The ipa path is now available in the Environment Variable: %s (value: %s)", ipaPathEnvKey, exportedPth)
}
}
// ---
// dsym
dsyms, err = findArtifact(iosOutputDir, "dSYM", buildStart)
if err != nil {
fail("Failed to find dSYMs in dir (%s), error: %s", iosOutputDir, err)
}
if len(dsyms) > 0 {
if exportedPth, _, err := moveAndExportOutputs(dsyms, configs.DeployDir, dsymDirPathEnvKey, apkPathListEnvKey); err != nil {
fail("Failed to export dsyms, error: %s", err)
} else if exportedPth != "" {
log.Donef("The dsym dir path is now available in the Environment Variable: %s (value: %s)", dsymDirPathEnvKey, exportedPth)
zippedExportedPth := exportedPth + ".zip"
if err := ziputil.ZipDir(exportedPth, zippedExportedPth, false); err != nil {
fail("Failed to zip dsym dir (%s), error: %s", exportedPth, err)
}
if err := tools.ExportEnvironmentWithEnvman(dsymZipPathEnvKey, zippedExportedPth); err != nil {
fail("Failed to export dsym.zip (%s), error: %s", zippedExportedPth, err)
}
log.Donef("The dsym.zip path is now available in the Environment Variable: %s (value: %s)", dsymZipPathEnvKey, zippedExportedPth)
}
}
// --
// app
apps, err = findArtifact(iosOutputDir, "app", buildStart)
if err != nil {
fail("Failed to find apps in dir (%s), error: %s", iosOutputDir, err)
}
if len(apps) > 0 {
if exportedPth, _, err := moveAndExportOutputs(apps, configs.DeployDir, appDirPathEnvKey, apkPathListEnvKey); err != nil {
log.Warnf("Failed to export apps, error: %s", err)
} else if exportedPth != "" {
log.Donef("The app dir path is now available in the Environment Variable: %s (value: %s)", appDirPathEnvKey, exportedPth)
zippedExportedPth := exportedPth + ".zip"
if err := ziputil.ZipDir(exportedPth, zippedExportedPth, false); err != nil {
fail("Failed to zip app dir (%s), error: %s", exportedPth, err)
}
if err := tools.ExportEnvironmentWithEnvman(appZipPathEnvKey, zippedExportedPth); err != nil {
fail("Failed to export app.zip (%s), error: %s", zippedExportedPth, err)
}
log.Donef("The app.zip path is now available in the Environment Variable: %s (value: %s)", appZipPathEnvKey, zippedExportedPth)
}
}
// ---
} else {
// ios output directory not exists and ios selected as platform
}
var apks []string
androidOutputDir := filepath.Join(workDir, "platforms", "android")
if exist, err := pathutil.IsDirExists(androidOutputDir); err != nil {
fail("Failed to check if dir (%s) exist, error: %s", androidOutputDir, err)
} else if exist {
fmt.Println()
log.Infof("Collecting android outputs")
apks, err = findArtifact(androidOutputDir, "apk", buildStart)
if err != nil {
fail("Failed to find apks in dir (%s), error: %s", androidOutputDir, err)
}
if len(apks) > 0 {
if exportedPth, exportedPaths, err := moveAndExportOutputs(apks, configs.DeployDir, apkPathEnvKey, apkPathListEnvKey); err != nil {
fail("Failed to export apks, error: %s", err)
} else if exportedPth != "" {
log.Donef("The apk path is now available in the Environment Variable: %s (value: %s)", apkPathEnvKey, exportedPth)
if len(exportedPaths) > 0 {
log.Donef("The apk paths are now available in the Environment Variable: %s (value: %s)", apkPathListEnvKey, strings.Join(exportedPaths, "|"))
}
}
}
}
// if android in platforms
if len(apks) == 0 && sliceutil.IsStringInSlice("android", platforms) {
fail("No apk generated")
}
// if ios in platforms
if sliceutil.IsStringInSlice("ios", platforms) {
if len(apps) == 0 && configs.Target == "emulator" {
fail("no apps generated")
}
if len(ipas) == 0 && configs.Target == "device" {
fail("no ipas generated")
}
}
}