Modern Apps git

xtool.git

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

xtool.git / Sources / XKit / Signer / AutoSigner.swift

AutoSigner.swift102 lines · 3.6 KiBRaw
1import Foundation
2import Dependencies
3
4/// Provisions and codesigns.
5public struct AutoSigner {
6
7 @Dependency(\.signer) private var signer
8
9 public enum Error: LocalizedError {
10 case errorReading(String)
11 case errorWriting(String)
12
13 public var errorDescription: String? {
14 switch self {
15 case .errorReading(let file):
16 return "\(file)".withCString {
17 String.localizedStringWithFormat(
18 NSLocalizedString(
19 "signer.error.error_reading", value: "Error while reading %s", comment: ""
20 ), $0
21 )
22 }
23 case .errorWriting(let file):
24 return "\(file)".withCString {
25 String.localizedStringWithFormat(
26 NSLocalizedString(
27 "signer.error.error_writing", value: "Error while writing %s", comment: ""
28 ), $0
29 )
30 }
31 }
32 }
33 }
34
35 public let context: SigningContext
36 public let confirmRevocation: @Sendable ([DeveloperServicesCertificate]) async -> Bool
37 public init(
38 context: SigningContext,
39 confirmRevocation: @escaping @Sendable ([DeveloperServicesCertificate]) async -> Bool
40 ) {
41 self.context = context
42 self.confirmRevocation = confirmRevocation
43 }
44
45 public func sign(
46 app: URL,
47 status: @escaping (String) -> Void,
48 progress: @escaping @Sendable (Double?) -> Void,
49 didProvision: @escaping () throws -> Void = {}
50 ) async throws -> String {
51 status(NSLocalizedString("signer.provisioning", value: "Provisioning", comment: ""))
52 let response = try await DeveloperServicesProvisioningOperation(
53 context: context,
54 app: app,
55 confirmRevocation: confirmRevocation,
56 progress: progress
57 ).perform()
58
59 let provisioningDict = response.provisioningDict
60 let signingInfo = response.signingInfo
61 guard let mainInfo = provisioningDict[app] else {
62 throw Error.errorReading("app bundle ID")
63 }
64
65 try didProvision()
66
67 for (url, info) in provisioningDict {
68 let infoPlist = url.appendingPathComponent("Info.plist")
69 guard let data = try? Data(contentsOf: infoPlist),
70 let dict = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any]
71 else { throw Error.errorReading(infoPlist.lastPathComponent) }
72 let nsDict = NSMutableDictionary(dictionary: dict)
73 nsDict["CFBundleIdentifier"] = info.newBundleID
74 guard nsDict.write(to: infoPlist, atomically: true) else {
75 throw Error.errorWriting(infoPlist.lastPathComponent)
76 }
77
78 let profURL = url.appendingPathComponent("embedded.mobileprovision")
79 if profURL.exists {
80 try FileManager.default.removeItem(at: profURL)
81 }
82 try info.mobileprovision.data().write(to: profURL)
83 }
84
85 let encoder = PropertyListEncoder()
86 encoder.outputFormat = .xml
87 let entitlements = try provisioningDict.mapValues { try encoder.encode($0.entitlements) }
88
89 status(NSLocalizedString("signer.signing", value: "Signing", comment: ""))
90 try await signer.sign(
91 app: app,
92 identity: .real(signingInfo.certificate, signingInfo.privateKey),
93 entitlementMapping: entitlements,
94 deep: true,
95 progress: progress
96 )
97 progress(1)
98
99 return mainInfo.newBundleID
100 }
101
102}