This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (126 loc) · 4.26 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
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
)
func Contains(vs []string, t string, caseSensitive bool) int {
for i, v := range vs {
if caseSensitive {
if strings.Contains(v, t) {
return i
}
} else if strings.Contains(strings.ToLower(v), strings.ToLower(t)) {
return i
}
}
return -1
}
func GetDeviceId(deviceNameEnv string) string {
devicesListLog, devicesListErr := exec.Command("xcrun", "simctl", "list", "devices").CombinedOutput()
if devicesListErr != nil {
fmt.Printf("XCode devices couldn't be called - error: %#v | output: %s", devicesListErr, devicesListLog)
os.Exit(1)
}
devicesListLogSplitted := strings.Split(string(devicesListLog), "\n")
// fmt.Print(devicesListLogSplitted)
indexOfSearchedDevice := Contains(devicesListLogSplitted, deviceNameEnv, false)
if indexOfSearchedDevice == -1 {
fmt.Printf("Device not found - devicesListLog: %s", devicesListLog)
os.Exit(1)
}
deviceUUIDLog := devicesListLogSplitted[indexOfSearchedDevice]
re := regexp.MustCompile("[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}")
deviceUUID := re.FindString(deviceUUIDLog)
if deviceUUID == "" {
fmt.Printf("Device not found - deviceUuidLog: %s", deviceUUIDLog)
os.Exit(1)
}
return deviceUUID
}
func SimctlExec(command string, validationLog string, deviceUUID string) {
for {
var tryCount = 0
commandSimLog, _ := exec.Command(
"xcrun", "simctl", command, deviceUUID).CombinedOutput()
commandSimLogList := strings.Split(string(commandSimLog), "\n")
// Wait for simulator to be booted
if Contains(commandSimLogList, validationLog, false) > -1 {
break
}
if tryCount == 10 {
break
}
tryCount++
}
}
func SimctlExecPermission(command string, deviceUUID string,
action string, service string, bundleId string) {
exec.Command("xcrun", "simctl", command, deviceUUID, action, service, bundleId).CombinedOutput()
}
func SetEnv(key string, value string) {
cmdLog, err := exec.Command("bitrise", "envman", "add", "--key", key, "--value", value).CombinedOutput()
if err != nil {
fmt.Printf("Failed to expose output with envman, error: %#v | output: %s", err, cmdLog)
// os.Exit(1)
}
}
func main() {
localTesting := false
commandEnv := os.Getenv("simctl_command")
deviceNameEnv := os.Getenv("simctl_device")
simctlAction := os.Getenv("BITRISE_SIMCTL_ACTION")
simctlService := os.Getenv("BITRISE_SIMCTL_SERVICE")
iosBundleId := os.Getenv("BITRISE_IOS_BUNDLE_ID")
prevDeviceIdEnv := os.Getenv("BITRISE_SIMCTL_PREVIOUS_DEVICE_ID")
prevDeviceNameEnv := os.Getenv("BITRISE_SIMCTL_PREVIOUS_DEVICE_NAME")
if localTesting == true {
// commandEnv = "boot"
// commandEnv = "shutdown"
// commandEnv = "erase"
commandEnv = "privacy"
deviceNameEnv = "iphone 11"
// deviceNameEnv := ""
simctlAction = "grant"
simctlService = "location-always"
iosBundleId = "de.dealog.mobile.pilot"
prevDeviceNameEnv = "FEF73DCA-1D03-4847-9D87-77CFCD977977"
}
if deviceNameEnv == "" {
deviceNameEnv = prevDeviceNameEnv
}
deviceUUID := prevDeviceIdEnv
if prevDeviceIdEnv == "" {
deviceUUID = GetDeviceId(deviceNameEnv)
}
if deviceUUID == "" {
fmt.Printf("DeviceUUID is empty")
os.Exit(1)
}
xcrunSimctlShutdownStateLog := "Unable to shutdown device in current state: Shutdown"
xcrunSimctlBootedStateLog := "Unable to boot device in current state: Booted"
// boot/shutdown simulator
if commandEnv == "boot" {
SimctlExec(commandEnv, xcrunSimctlBootedStateLog, deviceUUID)
SetEnv("BITRISE_SIMCTL_PREVIOUS_DEVICE_ID", deviceUUID)
SetEnv("BITRISE_SIMCTL_PREVIOUS_DEVICE_NAME", deviceNameEnv)
fmt.Printf("XCode device %s is booted", deviceUUID)
} else if commandEnv == "shutdown" {
SimctlExec(commandEnv, xcrunSimctlShutdownStateLog, deviceUUID)
fmt.Printf("XCode device %s is shutdown", deviceUUID)
} else if commandEnv == "erase" {
SimctlExec(commandEnv, xcrunSimctlShutdownStateLog, deviceUUID)
fmt.Printf("XCode device %s is shutdown", deviceUUID)
} else if commandEnv == "privacy" {
SimctlExecPermission(commandEnv, deviceUUID, simctlAction, simctlService, iosBundleId)
fmt.Printf("Privacy set for XCode device %s", deviceUUID)
} else {
fmt.Printf("Command %s is unknown", commandEnv)
os.Exit(1)
}
fmt.Printf("4")
// Exec simulator command successful
os.Exit(0)
}