-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
357 lines (284 loc) · 12.4 KB
/
Rakefile
File metadata and controls
357 lines (284 loc) · 12.4 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
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
require "json"
require "open3"
require "shellwords"
require "timeout"
require "tmpdir"
def latest_path(glob)
Dir[File.expand_path(glob)].max_by { |path| File.mtime(path) }
end
def xcode_build_settings(project:, scheme:, configuration:, destination: nil)
command = [
"xcodebuild",
"-project", project,
"-scheme", scheme,
"-configuration", configuration,
"-showBuildSettings",
]
command += ["-destination", destination] if destination
output, status = Open3.capture2e(*command)
raise "Unable to resolve build settings for #{scheme}.\n#{output}" unless status.success?
output.each_line.each_with_object({}) do |line, settings|
match = line.match(/^\s*([A-Z0-9_]+)\s=\s(.*)$/)
next unless match
settings[match[1]] = match[2]
end
end
def xcode_built_product_path(project:, scheme:, configuration:, destination: nil)
settings = xcode_build_settings(
project: project,
scheme: scheme,
configuration: configuration,
destination: destination
)
target_build_dir = settings["TARGET_BUILD_DIR"]
product_name = settings["FULL_PRODUCT_NAME"]
raise "Unable to locate TARGET_BUILD_DIR for #{scheme}." if target_build_dir.nil? || target_build_dir.empty?
raise "Unable to locate FULL_PRODUCT_NAME for #{scheme}." if product_name.nil? || product_name.empty?
File.join(target_build_dir, product_name)
end
def devicectl_devices
output_path = File.join(Dir.tmpdir, "devicectl-devices-#{Process.pid}.json")
success = system(
"xcrun", "devicectl", "list", "devices", "--json-output", output_path,
out: File::NULL, err: File::NULL
)
raise "Unable to query devices with devicectl." unless success
JSON.parse(File.read(output_path)).fetch("result").fetch("devices")
ensure
File.delete(output_path) if output_path && File.exist?(output_path)
end
def watchos_demo_device
requested = ENV["WATCHOS_DEVICE"]
devices = devicectl_devices
device = if requested
devices.find do |entry|
[
entry["identifier"],
entry.dig("hardwareProperties", "udid"),
entry.dig("deviceProperties", "name"),
].compact.include?(requested)
end
else
devices.find do |entry|
entry.dig("hardwareProperties", "platform") == "watchOS" &&
entry.dig("hardwareProperties", "reality") == "physical" &&
entry.dig("connectionProperties", "tunnelState") == "connected"
end
end
if device.nil? && requested
raise "No watchOS device matching WATCHOS_DEVICE=#{requested.inspect} found."
end
raise "No connected physical watchOS device found. Set WATCHOS_DEVICE to a device name, identifier, or UDID to override." if device.nil?
{
name: device.dig("deviceProperties", "name") || device.fetch("identifier"),
selector: requested || device.dig("hardwareProperties", "udid") || device.fetch("identifier"),
udid: device.dig("hardwareProperties", "udid") || device.fetch("identifier"),
}
end
def ios_demo_device
requested = ENV["IOS_DEVICE"]
devices = devicectl_devices
device = if requested
devices.find do |entry|
[
entry["identifier"],
entry.dig("hardwareProperties", "udid"),
entry.dig("deviceProperties", "name"),
].compact.include?(requested)
end
else
devices.find do |entry|
entry.dig("hardwareProperties", "platform") == "iOS" &&
entry.dig("hardwareProperties", "reality") == "physical" &&
entry.dig("connectionProperties", "tunnelState") == "connected"
end
end
if device.nil? && requested
raise "No iOS device matching IOS_DEVICE=#{requested.inspect} found."
end
raise "No connected physical iOS device found. Set IOS_DEVICE to a device name, identifier, or UDID to override." if device.nil?
{
name: device.dig("deviceProperties", "name") || device.fetch("identifier"),
selector: requested || device.dig("hardwareProperties", "udid") || device.fetch("identifier"),
udid: device.dig("hardwareProperties", "udid") || device.fetch("identifier"),
}
end
def ios_simulator_destination(name_prefix)
devices = JSON.parse(`xcrun simctl list devices available --json`).fetch("devices").values.flatten
device = devices.find { |entry| entry["isAvailable"] && entry["name"].start_with?(name_prefix) }
raise "No available #{name_prefix} simulator found" unless device
"platform=iOS Simulator,id=#{device.fetch("udid")}"
end
def watchos_simulator_destination(name_prefix)
devices = JSON.parse(`xcrun simctl list devices available --json`).fetch("devices").values.flatten
device = devices.find { |entry| entry["isAvailable"] && entry["name"].start_with?(name_prefix) }
raise "No available #{name_prefix} simulator found" unless device
"platform=watchOS Simulator,id=#{device.fetch("udid")}"
end
def ios_pretext_test_destination
ios_simulator_destination("iPhone")
end
def watchos_pretext_test_destination
watchos_simulator_destination("Apple Watch")
end
desc "Debug build (all targets)"
task :build do
sh "swift build"
end
desc "Build the Pretext library for iOS Simulator"
task :build_ios_pretext do
sh "xcodebuild -scheme Pretext -destination 'generic/platform=iOS Simulator' build CODE_SIGNING_ALLOWED=NO"
end
desc "Build the Demo app for iPhone and iPad simulators"
task :build_ios_demo do
sh "xcodebuild build -scheme Demo -destination '#{ios_simulator_destination("iPhone")}' CODE_SIGNING_ALLOWED=NO"
sh "xcodebuild build -scheme Demo -destination '#{ios_simulator_destination("iPad")}' CODE_SIGNING_ALLOWED=NO"
end
desc "Build the Demo macOS app bundle"
task :build_macos_demo do
sh "xcodegen generate --spec Xcode/DemoMacRunner/project.yml"
sh "xcodebuild -project Xcode/DemoMacRunner/DemoMacRunner.xcodeproj -scheme DemoMacRunner -configuration Release build"
end
desc "Build the Demo watchOS app for Apple Watch simulators"
task :build_watchos_demo do
sh "xcodegen generate --spec Xcode/DemoWatchRunner/project.yml"
sh "xcodebuild -project Xcode/DemoWatchRunner/DemoWatchRunner.xcodeproj -scheme DemoWatchRunner -configuration Release -destination 'generic/platform=watchOS Simulator' build CODE_SIGNING_ALLOWED=NO"
end
desc "Build, install, and launch the Demo watchOS app on a connected Apple Watch"
task :run_watchos_demo do
device = watchos_demo_device
device_selector = Shellwords.escape(device.fetch(:selector))
sh "xcodegen generate --spec Xcode/DemoWatchRunner/project.yml"
sh "xcodebuild -project Xcode/DemoWatchRunner/DemoWatchRunner.xcodeproj -scheme DemoWatchRunner -configuration Release -destination 'id=#{device.fetch(:udid)}' build"
app_path = latest_path("~/Library/Developer/Xcode/DerivedData/DemoWatchRunner-*/Build/Products/Release-watchos/Demo.app")
raise "Built Demo.app not found in DerivedData." unless app_path
app_path_escaped = Shellwords.escape(app_path)
bundle_id = "com.liang.pretextswift.demowatchrunner"
sh "xcrun devicectl device install app --device #{device_selector} #{app_path_escaped}"
Dir.mktmpdir("watchos-demo-run") do |tmpdir|
launch_json = File.join(tmpdir, "launch.json")
processes_json = File.join(tmpdir, "processes.json")
launch_json_escaped = Shellwords.escape(launch_json)
processes_json_escaped = Shellwords.escape(processes_json)
sh "xcrun devicectl device process launch --device #{device_selector} #{bundle_id} --activate --terminate-existing --json-output #{launch_json_escaped}"
process_id = JSON.parse(File.read(launch_json)).dig("result", "process", "processIdentifier")
sleep 2
sh "xcrun devicectl device info processes --device #{device_selector} --json-output #{processes_json_escaped}"
running_processes = JSON.parse(File.read(processes_json)).dig("result", "runningProcesses").to_a
running = running_processes.any? { |entry| entry["processIdentifier"] == process_id }
raise "Demo.app exited immediately after launch on #{device.fetch(:name)}." unless running
end
puts "Launched #{bundle_id} on #{device.fetch(:name)} (#{device.fetch(:udid)})."
end
desc "Build, install, and launch the Demo iOS app on a connected iPhone or iPad"
task :run_ios_demo do
device = ios_demo_device
device_selector = Shellwords.escape(device.fetch(:selector))
destination = "id=#{device.fetch(:udid)}"
sh "xcodegen generate --spec Xcode/DemoDeviceRunner/project.yml"
sh "xcodebuild -project Xcode/DemoDeviceRunner/DemoDeviceRunner.xcodeproj -scheme DemoDeviceRunner -configuration Release -destination 'id=#{device.fetch(:udid)}' build"
app_path = xcode_built_product_path(
project: "Xcode/DemoDeviceRunner/DemoDeviceRunner.xcodeproj",
scheme: "DemoDeviceRunner",
configuration: "Release",
destination: destination
)
raise "Built Demo.app not found at #{app_path}." unless File.exist?(app_path)
app_path_escaped = Shellwords.escape(app_path)
bundle_id = "com.liang.pretextswift.demodevicerunner"
sh "xcrun devicectl device install app --device #{device_selector} #{app_path_escaped}"
Dir.mktmpdir("ios-demo-run") do |tmpdir|
launch_json = File.join(tmpdir, "launch.json")
processes_json = File.join(tmpdir, "processes.json")
launch_json_escaped = Shellwords.escape(launch_json)
processes_json_escaped = Shellwords.escape(processes_json)
sh "xcrun devicectl device process launch --device #{device_selector} #{bundle_id} --activate --terminate-existing --json-output #{launch_json_escaped}"
process_id = JSON.parse(File.read(launch_json)).dig("result", "process", "processIdentifier")
sleep 2
sh "xcrun devicectl device info processes --device #{device_selector} --json-output #{processes_json_escaped}"
running_processes = JSON.parse(File.read(processes_json)).dig("result", "runningProcesses").to_a
running = running_processes.any? { |entry| entry["processIdentifier"] == process_id }
raise "Demo.app exited immediately after launch on #{device.fetch(:name)}." unless running
end
puts "Launched #{bundle_id} on #{device.fetch(:name)} (#{device.fetch(:udid)})."
end
desc "Run PretextTests on an iOS Simulator"
task :test_ios_pretext do
sh "xcodebuild test -scheme PretextSwift-Package -destination '#{ios_pretext_test_destination}' -only-testing:PretextTests CODE_SIGNING_ALLOWED=NO"
end
desc "Run PretextTests on a watchOS Simulator"
task :test_watchos_pretext do
sh "xcodegen generate --spec Xcode/PretextWatchTestRunner/project.yml"
sh "xcodebuild test -project Xcode/PretextWatchTestRunner/PretextWatchTestRunner.xcodeproj -scheme PretextWatchTests -destination '#{watchos_pretext_test_destination}' CODE_SIGNING_ALLOWED=NO"
end
desc "Run DemoTests on an iOS Simulator"
task :test_ios_demo do
sh "xcodebuild test -scheme PretextSwift-Package -destination '#{ios_simulator_destination("iPhone")}' -only-testing:DemoTests CODE_SIGNING_ALLOWED=NO"
end
desc "Release build"
task :release do
sh "swift build -c release"
end
desc "Run all tests"
task :test do
sh "swift test"
end
desc "Run complete Pretext test cases on the host platform"
task :test_pretext_host do
sh "swift test --filter '^PretextTests\\.'"
end
desc "Run complete Pretext test cases"
task test_pretext: :test_pretext_host
desc "Run demo tests only"
task :test_demo do
sh "swift test --filter DemoTests"
end
desc "Launch the demo app"
task :demo do
if RUBY_PLATFORM.include?("darwin")
Rake::Task[:build_macos_demo].invoke
app_path = xcode_built_product_path(
project: "Xcode/DemoMacRunner/DemoMacRunner.xcodeproj",
scheme: "DemoMacRunner",
configuration: "Release"
)
raise "Built Demo.app not found at #{app_path}." unless File.exist?(app_path)
executable_path = File.join(
app_path,
"Contents",
"MacOS",
File.basename(app_path, ".app")
)
escaped_executable_path = Shellwords.escape(executable_path)
escaped_app_path = Shellwords.escape(app_path)
sh "pkill -TERM -f #{escaped_executable_path} || true"
begin
Timeout.timeout(5) do
loop do
running = system("pgrep -f #{escaped_executable_path} >/dev/null 2>&1")
break unless running
sleep 0.1
end
end
rescue Timeout::Error
raise "Timed out waiting for #{executable_path} to terminate before relaunch."
end
sh "open -n #{escaped_app_path}"
else
Rake::Task[:release].invoke
sh ".build/release/Demo"
end
end
desc "Launch the benchmark GUI (release mode)"
task :benchmark => :release do
sh ".build/release/Benchmark"
end
desc "Run CLI benchmark (release mode)"
task :bench => :release do
sh ".build/release/Benchmark --cli"
end
desc "Clean build artifacts"
task :clean do
sh "swift package clean"
end
task default: :build