Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
main ▾

xtool.git / Sources / XToolSupport / LaunchCommand.swift

LaunchCommand.swift49 lines · 1.5 KiBRaw
1import XCLIUtils
2import XUtils
3import Foundation
4import XKit
5import SwiftyMobileDevice
6import ArgumentParser
7
8struct LaunchCommand: AsyncParsableCommand {
9 static let configuration = CommandConfiguration(
10 commandName: "launch",
11 abstract: "Launch an installed app"
12 )
13
14 @OptionGroup var connectionOptions: ConnectionOptions
15
16 @Argument(
17 help: "The app to launch"
18 ) var bundleID: String
19
20 @Argument(
21 help: .init(
22 "Launch arguments to pass to the app",
23 valueName: "arg"
24 )
25 ) var args: [String] = []
26
27 func run() async throws {
28 let client = try await connectionOptions.client()
29
30 let installProxy = try InstallationProxyClient(device: client.device, label: "xtool-inst")
31 let executable: URL
32 do {
33 executable = try installProxy.executable(forBundleID: bundleID)
34 } catch {
35 throw Console.Error("Could not find an installed app with bundle ID '\(bundleID)'")
36 }
37
38 d.out.print("Launching \(executable.lastPathComponent)...")
39
40 let debugserver = try DebugserverClient(device: client.device, label: "xtool")
41 guard try debugserver.launch(executable: executable, arguments: args) == "OK" else {
42 throw Console.Error("Launch failed (!OK)")
43 }
44 guard try debugserver.send(command: "qLaunchSuccess", arguments: []) == Data("OK".utf8) else {
45 throw Console.Error("Launch failed (!qLaunchSuccess)")
46 }
47 try debugserver.send(command: "D", arguments: [])
48 }
49}