Skip to content

armgit / xtool

Public Imported from GitHub
0

Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, macOS.

Add `build --sign` option (#224)

Kabir Oberai4077e1a0b8d9
diff --git a/Sources/XKit/Integration/IntegratedInstaller.swift b/Sources/XKit/Integration/IntegratedInstaller.swift
index a8cb990..0ecbd19 100644
--- a/Sources/XKit/Integration/IntegratedInstaller.swift
+++ b/Sources/XKit/Integration/IntegratedInstaller.swift
@@ -45,10 +45,7 @@ public actor IntegratedInstaller {
case unlockDevice
}
- let udid: String
- let lookupMode: LookupMode
let auth: DeveloperAPIAuthData
- let configureDevice: Bool
public weak var delegate: IntegratedInstallerDelegate?
@Dependency(\.zipCompressor) private var compressor
@@ -102,16 +99,10 @@ public actor IntegratedInstaller {
}
public init(
- udid: String,
- lookupMode: LookupMode,
auth: DeveloperAPIAuthData,
- configureDevice: Bool,
delegate: IntegratedInstallerDelegate
) {
- self.udid = udid
- self.lookupMode = lookupMode
self.auth = auth
- self.configureDevice = configureDevice
self.delegate = delegate
}
@@ -144,7 +135,7 @@ public actor IntegratedInstaller {
}
}
- private func fetchPairingKeys(with lockdownClient: LockdownClient) async throws -> Data {
+ private func fetchPairingKeys(with lockdownClient: LockdownClient, udid: String) async throws -> Data {
try lockdownClient.setValue(udid, forDomain: "com.apple.mobile.wireless_lockdown", key: "WirelessBuddyID")
try lockdownClient.setValue(true, forDomain: "com.apple.mobile.wireless_lockdown", key: "EnableWifiConnections")
@@ -201,7 +192,12 @@ public actor IntegratedInstaller {
}
@discardableResult
- public func install(app: URL) async throws -> String {
+ public func install(
+ app: URL,
+ udid: String,
+ lookupMode: LookupMode,
+ configureDevice: Bool,
+ ) async throws -> String {
try await self.updateStage(to: "Unpacking app", initialProgress: nil)
let _tempDir = try TemporaryDirectory(name: "staging")
@@ -254,7 +250,7 @@ public actor IntegratedInstaller {
let deviceName = try lockdownClient.deviceName()
let pairingKeys: Data? = if configureDevice {
- try await fetchPairingKeys(with: lockdownClient)
+ try await fetchPairingKeys(with: lockdownClient, udid: udid)
} else {
nil
}
@@ -262,30 +258,9 @@ public actor IntegratedInstaller {
try await updateProgress(to: 1)
- let context = try SigningContext(
- auth: auth,
- targetDevice: .init(udid: udid, name: deviceName)
- )
-
- let signer = AutoSigner(context: context) { certs in
- await self.delegate?.confirmRevocation(of: certs) ?? false
- }
- let bundleID = try await signer.sign(
+ let bundleID = try await signInPlace(
app: appDir,
- status: { @Sendable status in
- self.queueUpdateTask {
- $0.updateStageIgnoringCancellation(to: status)
- }
- },
- progress: { progress in
- self.queueUpdateTask {
- $0.updateProgressIgnoringCancellation(to: progress)
- }
- },
- didProvision: { @Sendable [self] in
- // TODO: reintroduce Superconfig
- _ = self
- }
+ targetDevice: .init(udid: udid, name: deviceName),
)
try await self.updateStage(to: "Packaging", initialProgress: nil)
@@ -314,6 +289,39 @@ public actor IntegratedInstaller {
return bundleID
}
+ @discardableResult
+ public func signInPlace(
+ app: URL,
+ targetDevice: SigningContext.TargetDevice? = nil,
+ ) async throws -> String {
+ let context = try SigningContext(
+ auth: auth,
+ targetDevice: targetDevice,
+ )
+
+ let signer = AutoSigner(context: context) { certs in
+ await self.delegate?.confirmRevocation(of: certs) ?? false
+ }
+
+ return try await signer.sign(
+ app: app,
+ status: { @Sendable status in
+ self.queueUpdateTask {
+ $0.updateStageIgnoringCancellation(to: status)
+ }
+ },
+ progress: { progress in
+ self.queueUpdateTask {
+ $0.updateProgressIgnoringCancellation(to: progress)
+ }
+ },
+ didProvision: { @Sendable [self] in
+ // TODO: reintroduce Superconfig
+ _ = self
+ }
+ )
+ }
+
}
#endif
diff --git a/Sources/XToolSupport/AuthToken.swift b/Sources/XToolSupport/AuthToken.swift
index 82e7b0f..eef02e8 100644
--- a/Sources/XToolSupport/AuthToken.swift
+++ b/Sources/XToolSupport/AuthToken.swift
@@ -48,9 +48,16 @@ extension AuthToken {
private static let decoder = JSONDecoder()
static func saved() throws -> Self {
- guard let data = try storage.data(forKey: "XTLAuthToken") else {
+ guard let token = try savedIfPresent() else {
throw Console.Error("Please log in with `xtool auth` before running this command.")
}
+ return token
+ }
+
+ static func savedIfPresent() throws -> Self? {
+ guard let data = try storage.data(forKey: "XTLAuthToken") else {
+ return nil
+ }
return try decoder.decode(AuthToken.self, from: data)
}
@@ -63,7 +70,7 @@ extension AuthToken {
try Self.storage.setData(data, forKey: "XTLAuthToken")
}
- func authData() throws -> DeveloperAPIAuthData {
+ func authData() -> DeveloperAPIAuthData {
switch self {
case .appStoreConnect(let data):
return .appStoreConnect(.init(id: data.id, issuerID: data.issuerID, pem: data.pem))
diff --git a/Sources/XToolSupport/DevCommand.swift b/Sources/XToolSupport/DevCommand.swift
index 83f8ac6..64d3327 100644
--- a/Sources/XToolSupport/DevCommand.swift
+++ b/Sources/XToolSupport/DevCommand.swift
@@ -80,7 +80,7 @@ struct PackOperation {
}
return try await group.reduce(into: [:]) { $0[$1.0] = $1.1 }
}
- print("Pseudo-signing...")
+ print("Applying entitlements...")
try await Signer.first().sign(
app: bundle,
identity: .adhoc,
@@ -117,6 +117,12 @@ struct DevBuildCommand: AsyncParsableCommand {
@OptionGroup var packOptions: PackOperation.BuildOptions
@Flag(
+ name: .shortAndLong,
+ help: "Codesign the built app",
+ ) var sign = false
+
+ @Flag(
+ name: .shortAndLong,
help: "Output a .ipa file instead of a .app"
) var ipa = false
@@ -128,11 +134,38 @@ struct DevBuildCommand: AsyncParsableCommand {
) var triple: String?
func run() async throws {
+ let signingAuthToken: AuthToken?
+ if sign {
+ guard let token = try AuthToken.savedIfPresent() else {
+ throw Console.Error("`build --sign` requires logging in with `xtool auth`")
+ }
+ signingAuthToken = token
+ } else {
+ signingAuthToken = nil
+ }
+
let url = try await PackOperation(
triple: triple,
buildOptions: packOptions
).run()
+ if let signingAuthToken {
+ let installDelegate = XToolInstallerDelegate()
+ let installer = IntegratedInstaller(
+ auth: signingAuthToken.authData(),
+ delegate: installDelegate
+ )
+ do {
+ defer { print() }
+ try await installer.signInPlace(app: url)
+ } catch let error as CancellationError {
+ throw error
+ } catch {
+ print("Error: \(error)")
+ throw ExitCode.failure
+ }
+ }
+
let finalURL: URL
if ipa {
@Dependency(\.zipCompressor) var compressor
@@ -211,17 +244,19 @@ struct DevRunCommand: AsyncParsableCommand {
let installDelegate = XToolInstallerDelegate()
let installer = IntegratedInstaller(
- udid: client.udid,
- lookupMode: .only(client.connectionType),
- auth: try token.authData(),
- configureDevice: false,
+ auth: token.authData(),
delegate: installDelegate
)
defer { print() }
do {
- try await installer.install(app: output)
+ try await installer.install(
+ app: output,
+ udid: client.udid,
+ lookupMode: .only(client.connectionType),
+ configureDevice: false,
+ )
} catch let error as CancellationError {
throw error
} catch {
diff --git a/Sources/XToolSupport/InstallCommand.swift b/Sources/XToolSupport/InstallCommand.swift
index e805b58..6ac024e 100644
--- a/Sources/XToolSupport/InstallCommand.swift
+++ b/Sources/XToolSupport/InstallCommand.swift
@@ -24,15 +24,17 @@ struct InstallCommand: AsyncParsableCommand {
let installDelegate = XToolInstallerDelegate()
let installer = IntegratedInstaller(
- udid: client.udid,
- lookupMode: .only(client.connectionType),
- auth: try token.authData(),
- configureDevice: false,
+ auth: token.authData(),
delegate: installDelegate
)
do {
- try await installer.install(app: URL(fileURLWithPath: path))
+ try await installer.install(
+ app: URL(fileURLWithPath: path),
+ udid: client.udid,
+ lookupMode: .only(client.connectionType),
+ configureDevice: false,
+ )
print("\nSuccessfully installed!")
} catch let error as CancellationError {
throw error