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