Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
appstore-distribution ▾

xtool.git / Sources / XToolSupport / LaunchCommand.swift

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