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.

Asyncify more Supersign code

Kabir Oberaib96ab46c71b1
diff --git a/Supersign/Sources/Supersign/DeveloperServices/App Groups/DeveloperServicesAssignAppGroupsOperation.swift b/Supersign/Sources/Supersign/DeveloperServices/App Groups/DeveloperServicesAssignAppGroupsOperation.swift
index 284c605..d52bcc8 100644
--- a/Supersign/Sources/Supersign/DeveloperServices/App Groups/DeveloperServicesAssignAppGroupsOperation.swift
+++ b/Supersign/Sources/Supersign/DeveloperServices/App Groups/DeveloperServicesAssignAppGroupsOperation.swift
@@ -19,58 +19,44 @@ public struct DeveloperServicesAssignAppGroupsOperation: DeveloperServicesOperat
self.appID = appID
}
- private func assignAppGroup(
- _ group: DeveloperServicesAppGroup,
- appID: DeveloperServicesAppID,
- completion: @escaping (Result<DeveloperServicesAppGroup.GroupID, Swift.Error>) -> Void
- ) {
- let request = DeveloperServicesAssignAppGroupRequest(
- platform: context.platform, teamID: context.teamID, appIDID: appID.id, groupID: group.id
- )
- context.client.send(request) { result in
- guard result.get(withErrorHandler: completion) != nil else { return }
- completion(.success(group.groupID))
- }
- }
-
- private func addOrAssignAppGroup(
+ private func upsertAppGroup(
_ groupID: DeveloperServicesAppGroup.GroupID,
- existingGroups: [DeveloperServicesAppGroup],
- appID: DeveloperServicesAppID,
- completion: @escaping (Result<DeveloperServicesAppGroup.GroupID, Swift.Error>) -> Void
- ) {
+ existingGroups: [String: DeveloperServicesAppGroup],
+ appID: DeveloperServicesAppID
+ ) async throws -> DeveloperServicesAppGroup.GroupID {
let sanitized = ProvisioningIdentifiers.sanitize(groupID: groupID)
- if let group = existingGroups.first(where: {
- ProvisioningIdentifiers.sanitize(groupID: $0.groupID) == sanitized
- }) {
- assignAppGroup(group, appID: appID, completion: completion)
+ let group: DeveloperServicesAppGroup
+ if let existingGroup = existingGroups[sanitized] {
+ group = existingGroup
} else {
let groupID = ProvisioningIdentifiers.groupID(fromSanitized: sanitized, context: context)
let name = ProvisioningIdentifiers.groupName(fromSanitized: sanitized)
let request = DeveloperServicesAddAppGroupRequest(
platform: context.platform, teamID: context.teamID, name: name, groupID: groupID
)
- context.client.send(request) { result in
- guard let group = result.get(withErrorHandler: completion) else { return }
- self.assignAppGroup(group, appID: appID, completion: completion)
- }
+ group = try await context.client.send(request)
}
- }
- public func perform(completion: @escaping (Result<[DeveloperServicesAppGroup.GroupID], Error>) -> Void) {
- let request = DeveloperServicesListAppGroupsRequest(platform: context.platform, teamID: context.teamID)
- context.client.send(request) { result in
- guard let existingGroups = result.get(withErrorHandler: completion) else { return }
+ _ = try await context.client.send(DeveloperServicesAssignAppGroupRequest(
+ platform: context.platform, teamID: context.teamID, appIDID: appID.id, groupID: group.id
+ ))
+
+ return group.groupID
+ }
- let grouper = RequestGrouper<DeveloperServicesAppGroup.GroupID, Error>()
- for groupID in self.groupIDs {
- grouper.add { completion in
- self.addOrAssignAppGroup(
- groupID, existingGroups: existingGroups, appID: self.appID, completion: completion
- )
+ public func perform() async throws -> [DeveloperServicesAppGroup.GroupID] {
+ let existing = try await context.client.send(DeveloperServicesListAppGroupsRequest(
+ platform: context.platform, teamID: context.teamID
+ ))
+ let sanitized = existing.map { (ProvisioningIdentifiers.sanitize(groupID: $0.groupID), $0) }
+ let dict = Dictionary(sanitized, uniquingKeysWith: { $1 })
+ return try await withThrowingTaskGroup(of: DeveloperServicesAppGroup.GroupID.self) { group in
+ for groupID in groupIDs {
+ group.addTask {
+ try await upsertAppGroup(groupID, existingGroups: dict, appID: appID)
}
}
- grouper.onComplete(completion)
+ return try await group.reduce(into: []) { $0.append($1) }
}
}
diff --git a/Supersign/Sources/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift b/Supersign/Sources/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift
index 78a1d96..07f4e6f 100644
--- a/Supersign/Sources/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift
+++ b/Supersign/Sources/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift
@@ -46,57 +46,52 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {
}
/// Registers the app with the given entitlements
- private func addOrFetchApp(
+ private func upsertApp(
bundleID: String,
entitlements: Entitlements,
team: DeveloperServicesTeam,
- completion: @escaping (Result<DeveloperServicesAppID, Swift.Error>) -> Void
- ) {
- let request = DeveloperServicesListAppIDsRequest(platform: context.platform, teamID: context.teamID)
- context.client.send(request) { result in
- guard let appIDs = result.get(withErrorHandler: completion) else { return }
- if let appID = appIDs.first(where: {
- bundleID == ProvisioningIdentifiers.sanitize(identifier: $0.bundleID)
- }) {
- let request = DeveloperServicesUpdateAppIDRequest(
- platform: self.context.platform,
- teamID: self.context.teamID,
- appIDID: appID.id,
- entitlements: entitlements,
- additionalFeatures: [],
- isFree: team.isFree
- )
- self.context.client.send(request, completion: completion)
- } else {
- let newBundleID = ProvisioningIdentifiers.identifier(fromSanitized: bundleID, context: self.context)
- let name = ProvisioningIdentifiers.appName(fromSanitized: bundleID)
- let request = DeveloperServicesAddAppIDRequest(
- platform: self.context.platform,
- teamID: self.context.teamID,
- bundleID: newBundleID,
- appName: name,
- entitlements: entitlements,
- additionalFeatures: [],
- isFree: team.isFree
- )
- self.context.client.send(request, completion: completion)
- }
+ appIDs: [String: DeveloperServicesAppID]
+ ) async throws -> DeveloperServicesAppID {
+ if let appID = appIDs[bundleID] {
+ let request = DeveloperServicesUpdateAppIDRequest(
+ platform: self.context.platform,
+ teamID: self.context.teamID,
+ appIDID: appID.id,
+ entitlements: entitlements,
+ additionalFeatures: [],
+ isFree: team.isFree
+ )
+ return try await context.client.send(request)
+ } else {
+ let newBundleID = ProvisioningIdentifiers.identifier(fromSanitized: bundleID, context: self.context)
+ let name = ProvisioningIdentifiers.appName(fromSanitized: bundleID)
+ let request = DeveloperServicesAddAppIDRequest(
+ platform: self.context.platform,
+ teamID: self.context.teamID,
+ bundleID: newBundleID,
+ appName: name,
+ entitlements: entitlements,
+ additionalFeatures: [],
+ isFree: team.isFree
+ )
+ return try await context.client.send(request)
}
}
- /// Registers the app and returns the resultant entitlements
- private func addAppAndGetEntitlements(
- app: URL,
+ /// Registers the app and creates a profile. Returns the resultant entitlements as well as
+ /// the profile (note that the profile does not necessarily include all of the entitlements
+ /// that the app has).
+ private func addApp(
+ _ app: URL,
team: DeveloperServicesTeam,
- // old bundle id, app id, ents
- completion: @escaping (Result<(DeveloperServicesAppID, Entitlements), Swift.Error>) -> Void
- ) {
+ appIDs: [String: DeveloperServicesAppID]
+ ) async throws -> ProvisioningInfo {
let infoURL = app.appendingPathComponent("Info.plist")
guard let data = try? Data(contentsOf: infoURL),
let dict = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any],
let bundleID = dict["CFBundleIdentifier"] as? String,
let executable = dict["CFBundleExecutable"] as? String else {
- return completion(.failure(Error.invalidApp(app)))
+ throw Error.invalidApp(app)
}
let executableURL = app.appendingPathComponent(executable)
@@ -108,115 +103,89 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {
entitlements = decodedEntitlements
if team.isFree {
- do {
- // re-assign rather than using updateEntitlements since the latter will
- // retain unrecognized entitlements
- let filtered = try entitlements.entitlements().filter { type(of: $0).isFree }
- entitlements = try Entitlements(entitlements: filtered)
- } catch {
- return completion(.failure(error))
- }
+ // re-assign rather than using updateEntitlements since the latter will
+ // retain unrecognized entitlements
+ let filtered = try entitlements.entitlements().filter { type(of: $0).isFree }
+ entitlements = try Entitlements(entitlements: filtered)
}
} else {
- do {
- entitlements = try Entitlements(entitlements: [])
- } catch {
- return completion(.failure(error))
- }
+ entitlements = try Entitlements(entitlements: [])
}
- addOrFetchApp(bundleID: bundleID, entitlements: entitlements, team: team) { result in
- guard let appID = result.get(withErrorHandler: completion) else { return }
-
- do {
- try entitlements.update(teamID: self.context.teamID, bundleID: appID.bundleID)
- // set get-task-allow to YES, required for dev certs
- try entitlements.updateEntitlements { ents in
- if let getTaskAllow = ents.firstIndex(where: { $0 is GetTaskAllowEntitlement }) {
- ents[getTaskAllow] = GetTaskAllowEntitlement(rawValue: true)
- } else {
- ents.append(GetTaskAllowEntitlement(rawValue: true))
- }
- }
- } catch {
- return completion(.failure(error))
- }
+ let appID = try await upsertApp(bundleID: bundleID, entitlements: entitlements, team: team, appIDs: appIDs)
- if var entitlementsArray = try? entitlements.entitlements(),
- let groupsIdx = entitlementsArray.firstIndex(where: { $0 is AppGroupEntitlement }),
- let groupsEntitlement = entitlementsArray[groupsIdx] as? AppGroupEntitlement {
- let groups = groupsEntitlement.rawValue
-
- DeveloperServicesAssignAppGroupsOperation(
- context: self.context,
- groupIDs: groups,
- appID: appID
- ).perform { result in
- guard let newGroups = result.get(withErrorHandler: completion) else { return }
- entitlementsArray[groupsIdx] = AppGroupEntitlement(rawValue: newGroups)
-
- do {
- try entitlements.setEntitlements(entitlementsArray)
- } catch {
- return completion(.failure(error))
- }
-
- completion(.success((appID, entitlements)))
- }
+ try entitlements.update(teamID: self.context.teamID, bundleID: appID.bundleID)
+ // set get-task-allow to YES, required for dev certs
+ try entitlements.updateEntitlements { ents in
+ if let getTaskAllow = ents.firstIndex(where: { $0 is GetTaskAllowEntitlement }) {
+ ents[getTaskAllow] = GetTaskAllowEntitlement(rawValue: true)
} else {
- completion(.success((appID, entitlements)))
+ ents.append(GetTaskAllowEntitlement(rawValue: true))
}
}
- }
- /// Registers the app and creates a profile. Returns the resultant entitlements as well as
- /// the profile (note that the profile does not necessarily include all of the entitlements
- /// that the app has).
- private func addApp(
- _ app: URL,
- with team: DeveloperServicesTeam,
- completion: @escaping (Result<ProvisioningInfo, Swift.Error>) -> Void
- ) {
- addAppAndGetEntitlements(app: app, team: team) { result in
- guard let (appID, entitlements) = result.get(withErrorHandler: completion) else { return }
- DeveloperServicesFetchProfileOperation(context: self.context, appID: appID).perform { result in
- guard let profile = result.get(withErrorHandler: completion) else { return }
- guard let mobileprovision = profile.mobileprovision
- else { return completion(.failure(Mobileprovision.Error.invalidProfile)) }
- completion(.success(ProvisioningInfo(
- newBundleID: appID.bundleID, entitlements: entitlements, mobileprovision: mobileprovision
- )))
- }
+ if var entitlementsArray = try? entitlements.entitlements(),
+ let groupsIdx = entitlementsArray.firstIndex(where: { $0 is AppGroupEntitlement }),
+ let groupsEntitlement = entitlementsArray[groupsIdx] as? AppGroupEntitlement {
+ let groups = groupsEntitlement.rawValue
+
+ let newGroups = try await DeveloperServicesAssignAppGroupsOperation(
+ context: self.context,
+ groupIDs: groups,
+ appID: appID
+ ).perform()
+
+ entitlementsArray[groupsIdx] = AppGroupEntitlement(rawValue: newGroups)
+ try entitlements.setEntitlements(entitlementsArray)
}
+
+ let profile = try await DeveloperServicesFetchProfileOperation(context: self.context, appID: appID).perform()
+ guard let mobileprovision = profile.mobileprovision
+ else { throw Mobileprovision.Error.invalidProfile }
+ return ProvisioningInfo(
+ newBundleID: appID.bundleID, entitlements: entitlements, mobileprovision: mobileprovision
+ )
+ }
+
+ private func getTeam() async throws -> DeveloperServicesTeam {
+ let request = DeveloperServicesListTeamsRequest()
+ let teams = try await context.client.send(request)
+ guard let team = teams.first(where: { $0.id == self.context.teamID })
+ else { throw Error.teamNotFound(self.context.teamID) }
+ return team
+ }
+
+ // keyed by sanitized bundle ID
+ private func getCurrentAppIDs() async throws -> [String: DeveloperServicesAppID] {
+ let request = DeveloperServicesListAppIDsRequest(platform: context.platform, teamID: context.teamID)
+ let appIDs = try await context.client.send(request)
+ let keyedIDs = appIDs.map { (ProvisioningIdentifiers.sanitize(identifier: $0.bundleID), $0) }
+ return Dictionary(keyedIDs, uniquingKeysWith: { $1 })
}
- private func perform(
- with team: DeveloperServicesTeam,
- completion: @escaping (Result<[URL: ProvisioningInfo], Swift.Error>) -> Void
- ) {
+ /// Registers the app + its extensions, returning the profile and entitlements of each
+ public func perform() async throws -> [URL : ProvisioningInfo] {
var apps: [URL] = [root]
let plugins = root.appendingPathComponent("PlugIns")
if plugins.dirExists {
apps += plugins.implicitContents.filter { $0.pathExtension.lowercased() == "appex" }
}
- let grouper = RequestGrouper<(URL, ProvisioningInfo), Swift.Error>()
- for app in apps {
- grouper.add { completion in
- addApp(app, with: team) { completion($0.map { (app, $0) }) }
+ async let teamTask = getTeam()
+ async let appIDsTask = getCurrentAppIDs()
+ let (team, appIDs) = try await (teamTask, appIDsTask)
+
+ return try await withThrowingTaskGroup(
+ of: (URL, ProvisioningInfo).self,
+ returning: [URL: ProvisioningInfo].self
+ ) { group in
+ for app in apps {
+ group.addTask {
+ let info = try await addApp(app, team: team, appIDs: appIDs)
+ return (app, info)
+ }
}
- }
- grouper.onComplete { completion($0.map(Dictionary.init(uniqueKeysWithValues:))) }
- }
-
- /// Registers the app + its extensions, returning the profile and entitlements of each
- public func perform(completion: @escaping (Result<[URL: ProvisioningInfo], Swift.Error>) -> Void) {
- let request = DeveloperServicesListTeamsRequest()
- context.client.send(request) { result in
- guard let teams = result.get(withErrorHandler: completion) else { return }
- guard let team = teams.first(where: { $0.id == self.context.teamID })
- else { return completion(.failure(Error.teamNotFound(self.context.teamID))) }
- self.perform(with: team, completion: completion)
+ return try await group.reduce(into: [:]) { $0[$1.0] = $1.1 }
}
}
diff --git a/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift b/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift
index 6591896..ad088f9 100644
--- a/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift
+++ b/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift
@@ -83,7 +83,7 @@ public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOpera
return signingInfo
}
- private func perform() async throws -> SigningInfo {
+ public func perform() async throws -> SigningInfo {
let request = DeveloperServicesListCertificatesRequest(
teamID: context.teamID, certificateKind: .init(platform: context.platform)
)
@@ -106,10 +106,4 @@ public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOpera
return signingInfo
}
- public func perform(completion: @escaping (Result<SigningInfo, Swift.Error>) -> Void) {
- Task {
- completion(await Result { try await perform() })
- }
- }
-
}
diff --git a/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesOperation.swift b/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesOperation.swift
index e091492..83299e7 100644
--- a/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesOperation.swift
+++ b/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesOperation.swift
@@ -13,5 +13,5 @@ public protocol DeveloperServicesOperation {
var context: SigningContext { get }
- func perform(completion: @escaping (Result<Response, Error>) -> Void)
+ func perform() async throws -> Response
}
diff --git a/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesProvisioningOperation.swift b/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesProvisioningOperation.swift
index 075bd8a..37301a3 100644
--- a/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesProvisioningOperation.swift
+++ b/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesProvisioningOperation.swift
@@ -37,24 +37,21 @@ public struct DeveloperServicesProvisioningOperation: DeveloperServicesOperation
self.progress = progress
}
- public func perform(completion: @escaping (Result<Response, Error>) -> Void) {
+ public func perform() async throws -> Response {
progress(0/3)
- DeveloperServicesFetchDeviceOperation(context: context).perform { result in
- guard result.get(withErrorHandler: completion) != nil else { return }
- self.progress(1/3)
- DeveloperServicesFetchCertificateOperation(
- context: self.context,
- confirmRevocation: confirmRevocation
- ).perform { result in
- guard let signingInfo = result.get(withErrorHandler: completion) else { return }
- self.progress(2/3)
- DeveloperServicesAddAppOperation(context: self.context, root: self.app).perform { result in
- guard let provisioningDict = result.get(withErrorHandler: completion) else { return }
- self.progress(3/3)
- completion(.success(.init(signingInfo: signingInfo, provisioningDict: provisioningDict)))
- }
- }
- }
+ _ = try await DeveloperServicesFetchDeviceOperation(context: context).perform()
+
+ progress(1/3)
+ let signingInfo = try await DeveloperServicesFetchCertificateOperation(
+ context: self.context,
+ confirmRevocation: confirmRevocation
+ ).perform()
+
+ progress(2/3)
+ let provisioningDict = try await DeveloperServicesAddAppOperation(context: context, root: app).perform()
+
+ progress(3/3)
+ return .init(signingInfo: signingInfo, provisioningDict: provisioningDict)
}
}
diff --git a/Supersign/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift b/Supersign/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift
index d3e6d61..5b21e99 100644
--- a/Supersign/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift
+++ b/Supersign/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift
@@ -9,28 +9,25 @@
import Foundation
public struct DeveloperServicesFetchDeviceOperation: DeveloperServicesOperation {
-
public let context: SigningContext
public init(context: SigningContext) {
self.context = context
}
- public func perform(completion: @escaping (Result<DeveloperServicesDevice, Error>) -> Void) {
- let request = DeveloperServicesListDevicesRequest(platform: context.platform, teamID: context.teamID)
- context.client.send(request) { result in
- guard let devices = result.get(withErrorHandler: completion) else { return }
- if let device = devices.first(where: { $0.udid == self.context.udid }) {
- return completion(.success(device))
- }
-
- let addRequest = DeveloperServicesAddDeviceRequest(
- platform: self.context.platform,
- teamID: self.context.teamID,
- udid: self.context.udid,
- name: self.context.deviceName
- )
- self.context.client.send(addRequest, completion: completion)
+ public func perform() async throws -> DeveloperServicesDevice {
+ let listRequest = DeveloperServicesListDevicesRequest(platform: context.platform, teamID: context.teamID)
+ let devices = try await context.client.send(listRequest)
+ if let device = devices.first(where: { $0.udid == self.context.udid }) {
+ return device
}
+
+ let addRequest = DeveloperServicesAddDeviceRequest(
+ platform: self.context.platform,
+ teamID: self.context.teamID,
+ udid: self.context.udid,
+ name: self.context.deviceName
+ )
+ return try await context.client.send(addRequest)
}
}
diff --git a/Supersign/Sources/Supersign/DeveloperServices/Profiles/DeveloperServicesFetchProfileOperation.swift b/Supersign/Sources/Supersign/DeveloperServices/Profiles/DeveloperServicesFetchProfileOperation.swift
index 7dbfec0..99de067 100644
--- a/Supersign/Sources/Supersign/DeveloperServices/Profiles/DeveloperServicesFetchProfileOperation.swift
+++ b/Supersign/Sources/Supersign/DeveloperServices/Profiles/DeveloperServicesFetchProfileOperation.swift
@@ -18,32 +18,18 @@ public struct DeveloperServicesFetchProfileOperation: DeveloperServicesOperation
self.appID = appID
}
- private func createProfile(
- appID: DeveloperServicesAppID,
- completion: @escaping (Result<DeveloperServicesProfile, Swift.Error>) -> Void
- ) {
- let request = DeveloperServicesGetProfileRequest(
- platform: context.platform, teamID: context.teamID, appIDID: appID.id
- )
- context.client.send(request, completion: completion)
- }
-
- public func perform(completion: @escaping (Result<DeveloperServicesProfile, Error>) -> Void) {
- let request = DeveloperServicesListProfilesRequest(platform: context.platform, teamID: context.teamID)
- context.client.send(request) { result in
- guard let profiles = result.get(withErrorHandler: completion) else { return }
- if let profile = profiles.first(where: { $0.appID.id == self.appID.id }) {
- let request = DeveloperServicesDeleteProfileRequest(
- platform: self.context.platform, teamID: self.context.teamID, profileID: profile.id
- )
- self.context.client.send(request) { result in
- guard result.get(withErrorHandler: completion) != nil else { return }
- self.createProfile(appID: self.appID, completion: completion)
- }
- } else {
- self.createProfile(appID: self.appID, completion: completion)
- }
+ public func perform() async throws -> DeveloperServicesProfile {
+ let profiles = try await context.client.send(DeveloperServicesListProfilesRequest(
+ platform: context.platform, teamID: context.teamID
+ ))
+ if let profile = profiles.first(where: { $0.appID.id == self.appID.id }) {
+ _ = try await context.client.send(DeveloperServicesDeleteProfileRequest(
+ platform: context.platform, teamID: context.teamID, profileID: profile.id
+ ))
}
+ return try await context.client.send(DeveloperServicesGetProfileRequest(
+ platform: context.platform, teamID: context.teamID, appIDID: appID.id
+ ))
}
}
diff --git a/Supersign/Sources/Supersign/Signer/Signer.swift b/Supersign/Sources/Supersign/Signer/Signer.swift
index 1eb5bec..2f30be2 100644
--- a/Supersign/Sources/Supersign/Signer/Signer.swift
+++ b/Supersign/Sources/Supersign/Signer/Signer.swift
@@ -49,54 +49,58 @@ public struct Signer {
self.confirmRevocation = confirmRevocation
}
- private func sign(
+ public func sign(
app: URL,
- signingInfo: SigningInfo,
- provisioningDict: [URL: ProvisioningInfo],
status: @escaping (String) -> Void,
progress: @escaping (Double?) -> Void,
- didProvision: @escaping () throws -> Void,
- completion: @escaping (Result<(), Swift.Error>) -> Void
- ) {
- do {
- try didProvision()
- } catch {
- return completion(.failure(error))
+ didProvision: @escaping () throws -> Void = {}
+ ) async throws -> String {
+ status(NSLocalizedString("signer.provisioning", value: "Provisioning", comment: ""))
+ let response = try await DeveloperServicesProvisioningOperation(
+ context: context,
+ app: app,
+ confirmRevocation: confirmRevocation,
+ progress: progress
+ ).perform()
+
+ let provisioningDict = response.provisioningDict
+ let signingInfo = response.signingInfo
+ guard let mainInfo = provisioningDict[app] else {
+ throw Error.errorReading("app bundle ID")
}
+ try didProvision()
+
for (url, info) in provisioningDict {
let infoPlist = url.appendingPathComponent("Info.plist")
guard let data = try? Data(contentsOf: infoPlist),
let dict = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any]
- else { return completion(.failure(Error.errorReading(infoPlist.lastPathComponent))) }
+ else { throw Error.errorReading(infoPlist.lastPathComponent) }
let nsDict = NSMutableDictionary(dictionary: dict)
nsDict["CFBundleIdentifier"] = info.newBundleID
guard nsDict.write(to: infoPlist, atomically: true) else {
- return completion(.failure(Error.errorWriting(infoPlist.lastPathComponent)))
+ throw Error.errorWriting(infoPlist.lastPathComponent)
}
- do {
- let profURL = url.appendingPathComponent("embedded.mobileprovision")
- if profURL.exists {
- try FileManager.default.removeItem(at: profURL)
- }
- try info.mobileprovision.data().write(to: profURL)
- } catch {
- return completion(.failure(error))
+ let profURL = url.appendingPathComponent("embedded.mobileprovision")
+ if profURL.exists {
+ try FileManager.default.removeItem(at: profURL)
}
+ try info.mobileprovision.data().write(to: profURL)
}
- let entitlements = provisioningDict.mapValues { $0.entitlements }
+ let entitlements = provisioningDict.mapValues(\.entitlements)
status(NSLocalizedString("signer.signing", value: "Signing", comment: ""))
- context.signerImpl.sign(
+ try await context.signerImpl.sign(
app: app,
certificate: signingInfo.certificate,
privateKey: signingInfo.privateKey,
entitlementMapping: entitlements,
- progress: progress,
- completion: completion
+ progress: progress
)
+
+ return mainInfo.newBundleID
}
public func sign(
@@ -106,27 +110,10 @@ public struct Signer {
didProvision: @escaping () throws -> Void = {},
completion: @escaping (Result<String, Swift.Error>) -> Void
) {
- status(NSLocalizedString("signer.provisioning", value: "Provisioning", comment: ""))
- DeveloperServicesProvisioningOperation(
- context: context,
- app: app,
- confirmRevocation: confirmRevocation,
- progress: progress
- ).perform { result in
- guard let response = result.get(withErrorHandler: completion) else { return }
- guard let mainInfo = response.provisioningDict[app] else {
- return completion(.failure(Error.errorReading("app bundle ID")))
- }
- self.sign(
- app: app,
- signingInfo: response.signingInfo,
- provisioningDict: response.provisioningDict,
- status: status,
- progress: progress,
- didProvision: didProvision
- ) {
- completion($0.map { mainInfo.newBundleID })
- }
+ Task {
+ completion(await Result { try await sign(
+ app: app, status: status, progress: progress, didProvision: didProvision)
+ })
}
}
diff --git a/Supersign/Sources/Supersign/Signer/SignerImpl.swift b/Supersign/Sources/Supersign/Signer/SignerImpl.swift
index f13cdb9..259cacf 100644
--- a/Supersign/Sources/Supersign/Signer/SignerImpl.swift
+++ b/Supersign/Sources/Supersign/Signer/SignerImpl.swift
@@ -135,21 +135,22 @@ public struct SignerImpl {
certificate: Certificate,
privateKey: PrivateKey,
entitlementMapping: [URL: Entitlements],
- progress: @escaping (Double?) -> Void,
- completion: @escaping (Result<(), Swift.Error>) -> Void
- ) {
- Self.signingQueue.async {
- do {
- try self._sign(
- app: app,
- certificate: certificate,
- privateKey: privateKey,
- entitlementMapping: entitlementMapping,
- progress: progress
- )
- completion(.success(()))
- } catch {
- completion(.failure(error))
+ progress: @escaping (Double?) -> Void
+ ) async throws {
+ try await withCheckedThrowingContinuation { cont in
+ Self.signingQueue.async {
+ do {
+ try self._sign(
+ app: app,
+ certificate: certificate,
+ privateKey: privateKey,
+ entitlementMapping: entitlementMapping,
+ progress: progress
+ )
+ cont.resume()
+ } catch {
+ cont.resume(throwing: error)
+ }
}
}
}