-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathAdb.swift
More file actions
178 lines (147 loc) · 4.02 KB
/
Adb.swift
File metadata and controls
178 lines (147 loc) · 4.02 KB
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
//
// Adb.swift
// AndroidDeviceKit
//
// Created by Jared Hendry on 2020-09-22.
// Copyright © 2020 Shopify. All rights reserved.
//
import Foundation
import RegexBuilder
import ShellKit
final class AdbError: Error {}
final class AdbBootTimedOutError: Error {}
struct Adb {
static func listDevices() -> [ConnectedDevice] {
guard let output = try? run(command: .adb(.devices), log: log) else {
return []
}
return output
.components(separatedBy: .newlines)
.dropFirst()
.compactMap { ConnectedDevice(from: $0) }
}
static func getVirtualDeviceName(for device: ConnectedDevice) throws -> String {
let serial = device.serial
let output = try firstLine(of: .adb(.getProp(serial: serial, property: "ro.boot.qemu.avd_name")))
?? firstLine(of: .adb(.getProp(serial: serial, property: "ro.kernel.qemu.avd_name")))
?? firstLine(of: .adb(.avdName(serial: serial)))
guard let output else {
throw AdbError()
}
return output
}
static func install(serial: String, apkUrl: URL) throws {
try run(command: .adb(.install(serial: serial, apkUrl: apkUrl)), log: log)
}
static func launch(serial: String, componentName: String, arguments: [String]) throws {
try run(command: .adb(.launch(serial: serial, componentName: componentName, arguments: arguments)), log: log)
}
static func resolveActivity(serial: String, packageName: String) throws -> String {
let output = try run(command: .adb(.resolveActivity(serial: serial, packageName: packageName)), log: log)
guard let value = output.components(separatedBy: .newlines).last else {
throw AdbError()
}
return value
}
static func wait(forSerial serial: String) async throws {
try run(command: .adb(.waitForDevice(serial: serial)), timeout: 120, log: log)
try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
while true {
if try firstLine(of: .adb(.getProp(serial: serial, property: "sys.boot_completed"))) == "1" {
return
}
try await Task.sleep(for: .seconds(1))
}
}
group.addTask {
try await Task.sleep(for: .seconds(300))
throw AdbBootTimedOutError()
}
try await group.next()
group.cancelAll()
}
}
private static func firstLine(of command: ShellCommand) throws -> String? {
let output = try run(command: command, log: log)
guard let firstLineString = output.components(separatedBy: .newlines).first else {
return nil
}
return firstLineString.isEmpty ? nil : firstLineString
}
}
private extension ConnectedDevice {
nonisolated(unsafe) private static let anyWhitespace = OneOrMore(.whitespace)
nonisolated(unsafe) private static let characterOrSymbolCapture = Capture {
OneOrMore(.any.subtracting(.whitespace))
}
nonisolated(unsafe) private static let search = Regex {
characterOrSymbolCapture
anyWhitespace
TryCapture {
ChoiceOf {
"device"
"no device"
"offline"
}
} transform: { ConnectedDevice.State(rawValue: String($0)) }
Optionally {
anyWhitespace
"usb:"
characterOrSymbolCapture
}
Optionally {
anyWhitespace
"product:"
characterOrSymbolCapture
}
Optionally {
anyWhitespace
"model:"
characterOrSymbolCapture
}
Optionally {
anyWhitespace
"device:"
characterOrSymbolCapture
}
Optionally {
anyWhitespace
"transport_id:"
characterOrSymbolCapture
}
}
init?(from shellLine: String) {
guard let output = shellLine.firstMatch(of: Self.search)?.output else {
return nil
}
let (_, serial, state, usb, product, model, device, transportId) = output
self.serial = String(serial)
self._state = state
if let usb = usb {
self.usb = String(usb)
} else {
self.usb = nil
}
if let product = product {
self.product = String(product)
} else {
self.product = nil
}
if let model = model {
self.model = String(model)
} else {
self.model = nil
}
if let device = device {
self.device = String(device)
} else {
self.device = nil
}
if let transportId = transportId {
self.transportId = String(transportId)
} else {
self.transportId = nil
}
}
}