-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
136 lines (115 loc) · 3.89 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
package main
import (
"fmt"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/command/git"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"net/http"
"os"
"path/filepath"
"runtime"
)
func main() {
var config Config
if err := stepconf.Parse(&config); err != nil {
log.Errorf("Configuration error: %s\n", err)
os.Exit(7)
}
stepconf.Print(config)
if err := ensureAndroidSdkSetup(); err != nil {
log.Errorf("Could not setup Android SDK, error: %s", err)
os.Exit(6)
}
flutterSdkDir, err := getSdkDestinationDir()
if err != nil {
log.Errorf("Could not calculate Flutter SDK destination directory path, error: %s", err)
os.Exit(5)
}
flutterSdkExists, err := pathutil.IsDirExists(flutterSdkDir)
if err != nil {
log.Errorf("Could not check if Flutter SDK is installed, error: %s", err)
os.Exit(1)
}
if !flutterSdkExists {
log.Infof("Extracting Flutter SDK to %s", flutterSdkDir)
if err := downloadAndExtractReleaseSdk(config.Version, flutterSdkDir); err != nil {
log.Infof("Version %s not found in releases, trying snapshot.", config.Version)
if err := downloadAndExtractSnapshotSdk(config.Version, flutterSdkDir); err != nil {
log.Errorf("Could not extract Flutter SDK, error: %s", err)
os.Exit(2)
}
}
} else {
log.Infof("Flutter SDK directory already exists, skipping installation.")
flutterVersion, err := fileutil.ReadStringFromFile(flutterSdkDir + "/version")
if err != nil {
log.Warnf("Could not determine installed Flutter version, error: %s", err)
} else if flutterVersion != config.Version {
log.Warnf("Already installed Flutter version %s will be used instead of requested version %s ", flutterVersion, config.Version)
}
}
for _, flutterCommand := range config.Commands {
log.Infof("Executing Flutter command: %s", flutterCommand)
flutterExecutablePath := filepath.Join(flutterSdkDir, "bin/flutter")
bashCommand := fmt.Sprintf("%s %s", flutterExecutablePath, flutterCommand)
err := command.RunCommandInDir(config.WorkingDir, "bash", "-c", bashCommand)
if err != nil {
log.Errorf("Flutter invocation failed, error: %s", err)
os.Exit(3)
}
}
}
func downloadAndExtractReleaseSdk(flutterVersion, flutterSdkDestinationDir string) error {
flutterSdkSourceURL := getFlutterSdkWithChannelSourceURL(flutterVersion)
flutterSdkParentDir := filepath.Join(flutterSdkDestinationDir, "..")
if runtime.GOOS == "darwin" {
return command.DownloadAndUnZIP(flutterSdkSourceURL, flutterSdkParentDir)
} else if runtime.GOOS == "linux" {
return downloadAndUnTarXZ(flutterSdkSourceURL, flutterSdkParentDir)
} else {
return fmt.Errorf("unsupported OS: %s", runtime.GOOS)
}
}
func getFlutterSdkWithChannelSourceURL(flutterVersion string) string {
channels := [2]string{"stable", "beta"}
var url string
for _, channel := range channels {
url = getFlutterSdkSourceURL(flutterVersion, channel)
if sdkFileExists(url) {
break
}
}
return url
}
func sdkFileExists(url string) bool {
response, err := http.Head(url)
return err == nil && response.StatusCode == 200
}
func getFlutterSdkSourceURL(flutterVersion string, channel string) string {
return fmt.Sprintf(
"https://storage.googleapis.com/flutter_infra_release/releases/%s/%s/flutter_%s_%s%s-%s.%s",
channel,
getFlutterPlatform(),
getFlutterPlatform(),
getArchitecture(),
flutterVersion,
channel,
getArchiveExtension())
}
func getArchitecture() string {
if runtime.GOARCH == "arm64" {
return "arm64_"
}
return ""
}
func downloadAndExtractSnapshotSdk(flutterVersion, flutterSdkDestinationDir string) error {
flutterVersion = normalizeFlutterVersion(flutterVersion)
gitRepo, err := git.New(flutterSdkDestinationDir)
if err != nil {
return err
}
return gitRepo.CloneTagOrBranch("https://github.com/flutter/flutter.git", flutterVersion).Run()
}