This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
forked from bitrise-steplib/steps-avd-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·214 lines (179 loc) · 5.86 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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-steputils/tools"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/log"
"github.com/kballard/go-shellquote"
)
// config ...
type config struct {
AndroidHome string `env:"ANDROID_HOME,required"`
APILevel int `env:"api_level,required"`
Tag string `env:"tag,opt[google_apis,google_apis_playstore,android-wear,android-tv,default]"`
DeviceProfile string `env:"profile,required"`
CreateCommandArgs string `env:"create_command_flags"`
StartCommandArgs string `env:"start_command_flags"`
ID string `env:"emulator_id,required"`
Abi string `env:"abi,opt[x86,armeabi-v7a,arm64-v8a,x86_64]"`
EmulatorChannel string `env:"emulator_channel,opt[0,1,2,3]"`
}
func runningDeviceInfos(androidHome string) (map[string]string, error) {
cmd := command.New(filepath.Join(androidHome, "platform-tools/adb"), "devices")
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
return map[string]string{}, fmt.Errorf("command failed, error: %s", err)
}
// List of devices attached
// emulator-5554 device
deviceListItemPattern := `^(?P<emulator>emulator-\d*)[\s+](?P<state>.*)`
deviceListItemRegexp := regexp.MustCompile(deviceListItemPattern)
deviceStateMap := map[string]string{}
scanner := bufio.NewScanner(strings.NewReader(out))
for scanner.Scan() {
line := scanner.Text()
matches := deviceListItemRegexp.FindStringSubmatch(line)
if len(matches) == 3 {
serial := matches[1]
state := matches[2]
deviceStateMap[serial] = state
}
}
if scanner.Err() != nil {
return map[string]string{}, fmt.Errorf("scanner failed, error: %s", err)
}
return deviceStateMap, nil
}
func failf(msg string, args ...interface{}) {
log.Errorf(msg, args...)
os.Exit(1)
}
func currentlyStartedDeviceSerial(alreadyRunningDeviceInfos, currentlyRunningDeviceInfos map[string]string) string {
startedSerial := ""
for serial := range currentlyRunningDeviceInfos {
_, found := alreadyRunningDeviceInfos[serial]
if !found {
startedSerial = serial
break
}
}
if len(startedSerial) > 0 {
state := currentlyRunningDeviceInfos[startedSerial]
if state == "device" {
return startedSerial
}
}
return ""
}
type phase struct {
name string
command *command.Model
customExecutor func(cmd *command.Model) func() (string, error)
}
func main() {
var cfg config
if err := stepconf.Parse(&cfg); err != nil {
failf("Issue with input: %s", err)
}
stepconf.Print(cfg)
fmt.Println()
runningDevices, err := runningDeviceInfos(cfg.AndroidHome)
if err != nil {
failf("Failed to check running devices, error: %s", err)
}
var (
sdkManagerPath = filepath.Join(cfg.AndroidHome, "tools/bin/sdkmanager")
avdManagerPath = filepath.Join(cfg.AndroidHome, "tools/bin/avdmanager")
emulatorPath = filepath.Join(cfg.AndroidHome, "emulator/emulator")
pkg = fmt.Sprintf("system-images;android-%d;%s;%s", cfg.APILevel, cfg.Tag, cfg.Abi)
yes, no = strings.Repeat("yes\n", 20), strings.Repeat("no\n", 20)
)
// parse custom flags
createCustomFlags, err := shellquote.Split(cfg.CreateCommandArgs)
if err != nil {
failf("Failed to parse create command args, error: %s", err)
}
startCustomFlags, err := shellquote.Split(cfg.StartCommandArgs)
if err != nil {
failf("Failed to parse start command args, error: %s", err)
}
for _, phase := range []phase{
{"Update emulator",
command.New(sdkManagerPath, "--verbose", "--channel="+cfg.EmulatorChannel, "emulator").
SetStdin(strings.NewReader(yes)), // hitting yes in case it waits for accepting license
nil,
},
{"Update system-image packages",
command.New(sdkManagerPath, "--verbose", pkg).
SetStdin(strings.NewReader(yes)), // hitting yes in case it waits for accepting license
nil,
},
{"Create device",
command.New(avdManagerPath, append([]string{
"--verbose", "create", "avd", "--force",
"--name", cfg.ID,
"--device", cfg.DeviceProfile,
"--package", pkg,
"--tag", cfg.Tag,
"--abi", cfg.Abi}, createCustomFlags...)...).
SetStdin(strings.NewReader(no)), // hitting no in case it asks for creating hw profile
nil,
},
{"Start device",
command.New(emulatorPath, append([]string{
"@" + cfg.ID,
"-verbose",
"-show-kernel",
"-no-audio",
"-no-boot-anim",
"-netdelay", "none",
"-no-snapshot",
"-wipe-data",
"-gpu", "swiftshader_indirect"}, startCustomFlags...)...),
func(cmd *command.Model) func() (string, error) { // need to start the emlator as a detached process
return func() (string, error) {
return "", cmd.GetCmd().Start()
}
},
},
} {
log.Infof(phase.name)
log.Donef("$ %s", phase.command.PrintableCommandArgs())
var exec = phase.command.RunAndReturnTrimmedCombinedOutput
if e := phase.customExecutor; e != nil {
exec = e(phase.command)
}
if out, err := exec(); err != nil {
failf("Failed to run phase, error: %s, output: %s", err, out)
}
fmt.Println()
}
deviceDetectionStarted := time.Now()
for true {
currentRunningDevices, err := runningDeviceInfos(cfg.AndroidHome)
if err != nil {
failf("Failed to check running devices, error: %s", err)
}
serial := currentlyStartedDeviceSerial(runningDevices, currentRunningDevices)
if serial != "" {
if err := tools.ExportEnvironmentWithEnvman("BITRISE_EMULATOR_SERIAL", serial); err != nil {
log.Warnf("Failed to export environment (BITRISE_EMULATOR_SERIAL), error: %s", err)
}
log.Printf("- Device with serial: %s started", serial)
break
}
bootWaitTime := time.Duration(300)
if time.Now().After(deviceDetectionStarted.Add(bootWaitTime * time.Second)) {
failf("Failed to boot emulator device within %d seconds.", bootWaitTime)
}
time.Sleep(5 * time.Second)
}
log.Donef("- Done")
}