-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBrowserStackAccessibilityLint.swift
More file actions
625 lines (531 loc) · 23.5 KB
/
BrowserStackAccessibilityLint.swift
File metadata and controls
625 lines (531 loc) · 23.5 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import Foundation
import PackagePlugin
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(ucrt)
import ucrt
#endif
@main
struct BrowserStackAccessibilityLintPlugin: CommandPlugin {
func performCommand(context: PluginContext, arguments: [String]) async throws {
var extractor = ArgumentExtractor(arguments)
let overrideDownloadURLString = extractor.extractOption(named: "download-url").last
let forceDownloadFlag = extractor.extractFlag(named: "force-download") > 0
let passthrough = extractor.remainingArguments
let environment = ProcessInfo.processInfo.environment
let forceDownload = forceDownloadFlag || isTruthy(environment["BROWSERSTACK_A11Y_CLI_FORCE_DOWNLOAD"])
let overrideDownloadURL = try parseOverride(urlString: overrideDownloadURLString ?? environment["BROWSERSTACK_A11Y_CLI_DOWNLOAD_URL"])
let cacheRoot = packageCacheRoot()
let artifact = try await BrowserStackCLIArtifact.ensureLatestBinary(
overrideURL: overrideDownloadURL,
forceDownload: forceDownload,
cacheRoot: cacheRoot
)
Diagnostics.remark("BrowserStackAccessibilityLint: Using CLI \(artifact.version) at \(artifact.executableURL.path)")
let sanitizedArguments = sanitizeArguments(passthrough)
let finalArguments = ["a11y"] + sanitizedArguments
try await runCLI(
executableURL: artifact.executableURL,
arguments: finalArguments,
workingDirectory: context.package.directory
)
}
}
private func isTruthy(_ value: String?) -> Bool {
guard let value = value?.trimmingCharacters(in: .whitespacesAndNewlines).lowercased(), !value.isEmpty else {
return false
}
return ["1", "true", "yes"].contains(value)
}
private func packageCacheRoot() -> URL {
// NOTE: Ignoring the package directory for caching; using a global user cache folder.
// Order of precedence:
// 1. XDG_CACHE_HOME if set
// 2. HOME/.cache
// 3. NSHomeDirectory()/.cache (fallback)
let env = ProcessInfo.processInfo.environment
let baseCache: URL = {
if let xdg = env["XDG_CACHE_HOME"], !xdg.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return URL(fileURLWithPath: xdg, isDirectory: true)
}
if let home = env["HOME"], !home.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return URL(fileURLWithPath: home, isDirectory: true).appendingPathComponent(".cache", isDirectory: true)
}
return URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true).appendingPathComponent(".cache", isDirectory: true)
}()
let target = baseCache
.appendingPathComponent("browserstack", isDirectory: true)
.appendingPathComponent("devtools", isDirectory: true)
.appendingPathComponent("spm-plugin", isDirectory: true)
// Verify write access to cache directory (exit code 2 if not writable)
let fm = FileManager.default
do {
try fm.createDirectory(at: target, withIntermediateDirectories: true)
if !fm.isWritableFile(atPath: target.path) {
forwardExit(code: 2, message: "Unable to access cache directory. Please add \"--allow-writing-to-directory ~/.cache/\" directive in the linter’s build phase command.")
}
let probe = target.appendingPathComponent(".write-probe-\(UUID().uuidString)")
do {
try "probe".data(using: .utf8)?.write(to: probe, options: [.atomic, .completeFileProtection])
try? fm.removeItem(at: probe)
} catch {
forwardExit(code: 2, message: "Unable to access cache directory. Please include directive \"--allow-writing-to-directory ~/.cache/\" where you are invoking the Swift package")
}
} catch {
forwardExit(code: 2, message: "Unable to access cache directory. Please include directive \"--allow-writing-to-directory ~/.cache/\" where you are invoking the Swift package")
}
return target
}
// MARK: - URL / Argument helpers
private func parseOverride(urlString: String?) throws -> URL? {
guard let urlString = urlString, !urlString.isEmpty else {
return nil
}
if let url = URL(string: urlString), let scheme = url.scheme, ["http", "https", "file"].contains(scheme.lowercased()) {
return url
}
return URL(fileURLWithPath: urlString)
}
private func sanitizeArguments(_ arguments: [String]) -> [String] {
var result: [String] = []
var skipNext = false
var passthroughMode = false
for argument in arguments {
if passthroughMode {
result.append(argument)
continue
}
if skipNext {
skipNext = false
continue
}
if argument == "--" {
passthroughMode = true
result.append(argument)
continue
}
if argument == "--output-format" || argument == "-o" {
skipNext = true
continue
}
if argument.hasPrefix("--output-format=") {
continue
}
if argument.count > 2, argument.hasPrefix("-o"), argument != "-o" {
// Handle short-form like "-oxcode".
continue
}
result.append(argument)
}
return result
}
// MARK: - CLI artifact management
private struct BrowserStackCLIArtifact {
let version: String
let executableURL: URL
static func ensureLatestBinary(overrideURL: URL?, forceDownload: Bool, cacheRoot: URL) async throws -> BrowserStackCLIArtifact {
let downloader = BrowserStackCLIDownloader(overrideURL: overrideURL, forceDownload: forceDownload, cacheRoot: cacheRoot)
return try await downloader.ensureArtifact()
}
}
private struct BrowserStackCLIDownloader {
let overrideURL: URL?
let forceDownload: Bool
let cacheRoot: URL
private var fileManager: FileManager { .default }
func ensureArtifact() async throws -> BrowserStackCLIArtifact {
if let overrideURL {
let info = try await resolveOverrideArtifact(from: overrideURL)
return try await prepareArtifact(using: info)
}
let defaultURL = try defaultDownloadURL()
let info = try await resolveRemoteArtifact(from: defaultURL)
return try await prepareArtifact(using: info)
}
private func ensureCacheRootExists() throws -> URL {
do {
try fileManager.createDirectory(at: cacheRoot, withIntermediateDirectories: true)
} catch let error as NSError where error.domain == NSCocoaErrorDomain && error.code == CocoaError.fileWriteNoPermission.rawValue {
throw PluginError("Permission denied writing to cache directory at \(cacheRoot.path). Rerun the plugin with --allow-writing-to-package-directory.")
} catch {
throw error
}
return cacheRoot
}
private func prepareArtifact(using info: ArtifactInfo) async throws -> BrowserStackCLIArtifact {
let cacheRoot = try ensureCacheRootExists()
let versionDirectory = cacheRoot.appendingPathComponent(info.version, isDirectory: true)
let executableName = info.executableName
let expectedExecutableURL = versionDirectory.appendingPathComponent(executableName, isDirectory: false)
if !forceDownload, fileManager.isExecutableFile(atPath: expectedExecutableURL.path) {
return BrowserStackCLIArtifact(version: info.version, executableURL: expectedExecutableURL)
}
Diagnostics.remark("BrowserStackAccessibilityLint: Downloading CLI \(info.version)...")
// Download into a temporary directory to avoid TOCTOU races
let tempDirectory = cacheRoot.appendingPathComponent(".download-\(UUID().uuidString)", isDirectory: true)
try fileManager.createDirectory(at: tempDirectory, withIntermediateDirectories: true)
defer { try? fileManager.removeItem(at: tempDirectory) }
#if os(Windows)
let archiveURL = tempDirectory.appendingPathComponent("browserstack-cli.zip")
try await download(from: info.resolvedURL, to: archiveURL)
Diagnostics.remark("BrowserStackAccessibilityLint: Extracting CLI \(info.version)...")
try unzip(archive: archiveURL, into: tempDirectory)
try? fileManager.removeItem(at: archiveURL)
#else
try extractWithBsdtar(from: info.resolvedURL, into: tempDirectory)
#endif
let locatedBinary = try locateExecutable(in: tempDirectory, preferredName: executableName)
// Atomically swap: remove old version dir, move temp into place
if fileManager.fileExists(atPath: versionDirectory.path) {
try fileManager.removeItem(at: versionDirectory)
}
try fileManager.moveItem(at: tempDirectory, to: versionDirectory)
let finalBinaryURL = versionDirectory.appendingPathComponent(locatedBinary.lastPathComponent, isDirectory: false)
if locatedBinary.lastPathComponent != executableName {
let expectedURL = versionDirectory.appendingPathComponent(executableName, isDirectory: false)
if fileManager.fileExists(atPath: expectedURL.path) {
try fileManager.removeItem(at: expectedURL)
}
try fileManager.moveItem(at: finalBinaryURL, to: expectedURL)
try ensureExecutablePermissions(at: expectedURL)
return BrowserStackCLIArtifact(version: info.version, executableURL: expectedURL)
}
try ensureExecutablePermissions(at: finalBinaryURL)
return BrowserStackCLIArtifact(version: info.version, executableURL: finalBinaryURL)
}
#if !os(Windows)
private func extractWithBsdtar(from url: URL, into directory: URL) throws {
if url.isFileURL {
try extractLocalArchive(at: url, into: directory)
} else {
try extractRemoteArchive(from: url, into: directory)
}
}
private func extractRemoteArchive(from url: URL, into directory: URL) throws {
let pipe = Pipe()
let curl = Process()
curl.executableURL = URL(fileURLWithPath: "/usr/bin/env")
curl.arguments = ["curl", "-fsSL", url.absoluteString]
curl.standardOutput = pipe
let curlError = Pipe()
curl.standardError = curlError
let bsdtar = Process()
bsdtar.executableURL = URL(fileURLWithPath: "/usr/bin/env")
bsdtar.arguments = ["bsdtar", "-xpf", "-", "-C", directory.path]
bsdtar.standardInput = pipe
let tarError = Pipe()
bsdtar.standardError = tarError
do {
try bsdtar.run()
} catch {
throw PluginError("Unable to launch bsdtar: \(error.localizedDescription)")
}
do {
try curl.run()
} catch {
bsdtar.terminate()
bsdtar.waitUntilExit()
throw PluginError("Unable to launch curl: \(error.localizedDescription)")
}
curl.waitUntilExit()
pipe.fileHandleForWriting.closeFile()
bsdtar.waitUntilExit()
if curl.terminationStatus != 0 {
let message = String(data: curlError.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
forwardExit(code: curl.terminationStatus, message: message)
}
guard bsdtar.terminationReason == .exit, bsdtar.terminationStatus == 0 else {
let message = String(data: tarError.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
forwardExit(code: bsdtar.terminationStatus, message: message.isEmpty ? "bsdtar failed to extract BrowserStack CLI." : message)
}
}
private func extractLocalArchive(at archiveURL: URL, into directory: URL) throws {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
process.arguments = ["bsdtar", "-xpf", archiveURL.path, "-C", directory.path]
let errorPipe = Pipe()
process.standardError = errorPipe
do {
try process.run()
process.waitUntilExit()
} catch {
throw PluginError("Failed to launch bsdtar: \(error.localizedDescription)")
}
if process.terminationReason != .exit || process.terminationStatus != 0 {
// Fall back to copying the file directly if it's already an executable.
let message = String(data: errorPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
if fileManager.isExecutableFile(atPath: archiveURL.path) {
let destination = directory.appendingPathComponent(archiveURL.lastPathComponent)
if fileManager.fileExists(atPath: destination.path) {
try fileManager.removeItem(at: destination)
}
try fileManager.copyItem(at: archiveURL, to: destination)
} else {
forwardExit(code: process.terminationStatus, message: message.isEmpty ? "bsdtar failed to extract BrowserStack CLI." : message)
}
}
}
#endif
private func resolveOverrideArtifact(from url: URL) async throws -> ArtifactInfo {
let resolvedURL: URL
if url.isFileURL {
resolvedURL = url
} else {
resolvedURL = try await followRedirects(for: url)
}
let version = extractVersion(from: resolvedURL) ?? "override"
return ArtifactInfo(version: version, resolvedURL: resolvedURL, executableName: executableFileName())
}
private func resolveRemoteArtifact(from url: URL) async throws -> ArtifactInfo {
let resolvedURL = try await followRedirects(for: url)
guard let version = extractVersion(from: resolvedURL) else {
throw PluginError("Unable to determine BrowserStack CLI version from \(resolvedURL.absoluteString)")
}
return ArtifactInfo(version: version, resolvedURL: resolvedURL, executableName: executableFileName())
}
private func defaultDownloadURL() throws -> URL {
let os = try currentOSName()
let arch = try currentArchName()
guard let url = URL(string: "http://api.browserstack.com/sdk/v1/download_cli?os=\(os)&os_arch=\(arch)") else {
throw PluginError("Failed to create download URL for \(os) \(arch).")
}
return url
}
private func currentOSName() throws -> String {
#if os(macOS)
return "macos"
#elseif os(Linux)
return isAlpineLinux() ? "alpine" : "linux"
#elseif os(Windows)
return "windows"
#else
throw PluginError("Unsupported operating system for BrowserStack CLI.")
#endif
}
private func currentArchName() throws -> String {
let machine = try hardwareIdentifier()
switch machine.lowercased() {
case "arm64", "aarch64":
return "arm64"
case "x86_64", "amd64":
return "x64"
default:
throw PluginError("Unsupported architecture '\(machine)' for BrowserStack CLI.")
}
}
private func followRedirects(for url: URL) async throws -> URL {
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
request.timeoutInterval = 30
do {
let (_, response) = try await URLSession.shared.data(for: request)
if let http = response as? HTTPURLResponse {
if http.statusCode == 405 || http.statusCode == 501 {
return try await followWithGet(for: url)
}
if let location = http.value(forHTTPHeaderField: "Location"),
let redirectURL = URL(string: location, relativeTo: url)?.absoluteURL {
return redirectURL
}
}
if let finalURL = response.url {
return finalURL
}
} catch let error as URLError where error.code == .badServerResponse || error.code == .unsupportedURL {
return try await followWithGet(for: url)
} catch let error as URLError where error.code == .cannotConnectToHost {
throw PluginError("Network connection failed for \(url.absoluteString): \(error.localizedDescription)")
} catch {
throw error
}
throw PluginError("Failed to resolve redirect for \(url.absoluteString).")
}
private func followWithGet(for url: URL) async throws -> URL {
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.httpShouldHandleCookies = false
request.timeoutInterval = 60
let (_, response) = try await URLSession.shared.data(for: request)
if let http = response as? HTTPURLResponse,
let location = http.value(forHTTPHeaderField: "Location"),
let redirectURL = URL(string: location, relativeTo: url)?.absoluteURL {
return redirectURL
}
guard let finalURL = response.url else {
throw PluginError("Failed to resolve redirect for \(url.absoluteString).")
}
return finalURL
}
#if os(Windows)
private func download(from url: URL, to destination: URL) async throws {
if url.isFileURL {
if fileManager.fileExists(atPath: destination.path) {
try fileManager.removeItem(at: destination)
}
try fileManager.copyItem(at: url, to: destination)
return
}
let (tempURL, response) = try await URLSession.shared.download(from: url)
if let httpResponse = response as? HTTPURLResponse, !(200..<300).contains(httpResponse.statusCode) {
throw PluginError("Failed to download BrowserStack CLI (HTTP \(httpResponse.statusCode)).")
}
if fileManager.fileExists(atPath: destination.path) {
try fileManager.removeItem(at: destination)
}
try fileManager.moveItem(at: tempURL, to: destination)
}
private func unzip(archive: URL, into destination: URL) throws {
let powershell = Process()
powershell.executableURL = URL(fileURLWithPath: "powershell")
powershell.arguments = [
"-NoProfile",
"-Command",
"Expand-Archive -LiteralPath \"\(archive.path)\" -DestinationPath \"\(destination.path)\" -Force"
]
try run(process: powershell, errorDescription: "Unable to extract BrowserStack CLI archive.")
}
#endif
private func locateExecutable(in directory: URL, preferredName: String) throws -> URL {
let preferredURL = directory.appendingPathComponent(preferredName, isDirectory: false)
if fileManager.isExecutableFile(atPath: preferredURL.path) {
return preferredURL
}
let enumerator = fileManager.enumerator(
at: directory,
includingPropertiesForKeys: [.isRegularFileKey, .isExecutableKey],
options: [.skipsHiddenFiles]
)
var fallback: URL?
while let element = enumerator?.nextObject() as? URL {
var isDirectory: ObjCBool = false
guard fileManager.fileExists(atPath: element.path, isDirectory: &isDirectory), !isDirectory.boolValue else {
continue
}
if element.lastPathComponent == preferredName {
return element
}
if fileManager.isExecutableFile(atPath: element.path) {
return element
}
if fallback == nil {
fallback = element
}
}
if let fallback {
return fallback
}
throw PluginError("Extracted archive does not contain a binary payload.")
}
private func ensureExecutablePermissions(at url: URL) throws {
#if os(Windows)
_ = url
#else
var attributes = [FileAttributeKey: Any]()
attributes[.posixPermissions] = 0o755
try fileManager.setAttributes(attributes, ofItemAtPath: url.path)
#endif
}
private func run(process: Process, errorDescription: String) throws {
process.standardOutput = FileHandle.standardOutput
process.standardError = FileHandle.standardError
process.standardInput = FileHandle.standardInput
try process.run()
process.waitUntilExit()
guard process.terminationReason == .exit else {
forwardExit(code: 1, message: errorDescription)
}
let status = process.terminationStatus
guard status == 0 else {
forwardExit(code: status, message: errorDescription)
}
}
private func executableFileName() -> String {
#if os(Windows)
return "browserstack-cli.exe"
#else
return "browserstack-cli"
#endif
}
}
private struct ArtifactInfo {
let version: String
let resolvedURL: URL
let executableName: String
}
// MARK: - System helpers
private func hardwareIdentifier() throws -> String {
#if os(Windows)
if let arch = ProcessInfo.processInfo.environment["PROCESSOR_ARCHITECTURE"]?.lowercased() {
return arch
}
throw PluginError("Unable to detect CPU architecture.")
#else
var systemInfo = utsname()
guard uname(&systemInfo) == 0 else {
throw PluginError("uname() failed to determine CPU architecture.")
}
let capacity = MemoryLayout.size(ofValue: systemInfo.machine)
let identifier = withUnsafePointer(to: &systemInfo.machine) { ptr -> String in
return ptr.withMemoryRebound(to: CChar.self, capacity: capacity) {
String(cString: $0)
}
}.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines.union(.controlCharacters))
return identifier
#endif
}
private func extractVersion(from url: URL) -> String? {
let filename = url.deletingPathExtension().lastPathComponent
if let range = filename.range(of: "-", options: .backwards) {
let version = filename[range.upperBound...]
return version.isEmpty ? nil : String(version)
}
return nil
}
#if os(Linux)
private func isAlpineLinux() -> Bool {
guard let contents = try? String(contentsOfFile: "/etc/os-release") else {
return false
}
return contents.contains("ID=alpine")
}
#else
private func isAlpineLinux() -> Bool { false }
#endif
// MARK: - CLI invocation
private func runCLI(executableURL: URL, arguments: [String], workingDirectory: PackagePlugin.Path) async throws {
let process = Process()
process.executableURL = executableURL
process.arguments = arguments
process.currentDirectoryURL = URL(fileURLWithPath: workingDirectory.string, isDirectory: true)
process.standardInput = FileHandle.standardInput
process.standardOutput = FileHandle.standardOutput
process.standardError = FileHandle.standardError
try process.run()
process.waitUntilExit()
guard process.terminationReason == .exit else {
forwardExit(code: 1, message: "browserstack-cli terminated abnormally.")
}
let status = process.terminationStatus
guard status == 0 else {
forwardExit(code: status, message: "")
}
}
// MARK: - Error
private struct PluginError: Error, CustomStringConvertible {
let message: String
init(_ message: String) {
self.message = message
}
var description: String { message }
}
private func forwardExit(code: Int32, message: String) -> Never {
if !message.isEmpty, let data = (message + "\n").data(using: .utf8) {
FileHandle.standardError.write(data)
}
exit(code)
}