Modern Apps git

xtool.git

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

xtool.git / Sources / XToolSupport / XToolInstallerDelegate.swift

XToolInstallerDelegate.swift93 lines · 2.9 KiBRaw
1import XCLIUtils
2import Foundation
3import XKit
4import ConcurrencyExtras
5import Dependencies
6import XUtils
7
8final class XToolAuthDelegate: TwoFactorAuthDelegate {
9 func fetchCode() async -> String? {
10 try? await Console.prompt("Code: ")
11 }
12}
13
14actor XToolInstallerDelegate: IntegratedInstallerDelegate {
15 private nonisolated let _output = Dependency(\.out)
16 private nonisolated var output: CommandOutput { _output.wrappedValue }
17
18 private let updateTask = LockIsolated<Task<Void, Never>?>(nil)
19
20 nonisolated func setPresentedMessage(_ message: IntegratedInstaller.Message?) {
21 let text: String
22 switch message {
23 case .pairDevice:
24 text = "Please tap 'trust' on your device..."
25 case .unlockDevice:
26 text = "Please unlock your device..."
27 case nil:
28 text = "Continuing..."
29 }
30 output.print("\n\(text)", terminator: "", flush: true)
31 }
32
33 var prevStage: String?
34 var prevProgress: String?
35
36 private func _installerDidUpdate(toStage stage: String, progress: Double?) {
37 let progString: String?
38 if let progress = progress {
39 let progInt = Int(progress * 100)
40 if progInt < 10 {
41 progString = " \(progInt)%"
42 } else if progInt < 100 {
43 progString = " \(progInt)%"
44 } else {
45 progString = "\(progInt)%"
46 }
47 } else {
48 progString = nil
49 }
50
51 defer {
52 prevStage = stage
53 prevProgress = progString
54 }
55
56 if stage != prevStage {
57 if let progString = progString {
58 output.print("\n[\(stage)] \(progString)", terminator: "", flush: true)
59 } else {
60 output.print("\n[\(stage)] ...", terminator: "", flush: true)
61 }
62 } else if progString != prevProgress {
63 if let progString = progString {
64 output.print("\r[\(stage)] \(progString)", terminator: "", flush: true)
65 } else {
66 output.print("\r[\(stage)]", terminator: "", flush: true)
67 }
68 }
69 }
70
71 nonisolated func installerDidUpdate(toStage stage: String, progress: Double?) {
72 updateTask.withValue { task in
73 task = Task { [prev = task] in
74 await prev?.value
75 await _installerDidUpdate(toStage: stage, progress: progress)
76 }
77 }
78 }
79
80 func confirmRevocation(of certificates: [DeveloperServicesCertificate]) async -> Bool {
81 output.print("\nThe following certificates must be revoked:")
82 output.print(
83 certificates.map {
84 "- \($0.attributes!.name!) (expires \($0.attributes!.expirationDate!.formatted(date: .abbreviated, time: .shortened)))"
85 }.joined(separator: "\n")
86 )
87 do {
88 return try await Console.confirm("Continue?")
89 } catch {
90 return false
91 }
92 }
93}