xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / Installation / AppInstaller.swift
AppInstaller.swift128 lines · 3.6 KiBRaw
| 1 | // |
| 2 | // AppInstaller.swift |
| 3 | // XKit |
| 4 | // |
| 5 | // Created by Kabir Oberai on 13/11/19. |
| 6 | // Copyright © 2019 Kabir Oberai. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import Foundation |
| 10 | import SwiftyMobileDevice |
| 11 | |
| 12 | public actor AppInstaller { |
| 13 | |
| 14 | public enum Error: LocalizedError { |
| 15 | case userCancelled |
| 16 | case invalidURL |
| 17 | |
| 18 | public var errorDescription: String? { |
| 19 | switch self { |
| 20 | case .userCancelled: |
| 21 | return NSLocalizedString( |
| 22 | "app_installer.error.user_cancelled", |
| 23 | value: "The user cancelled the operation", |
| 24 | comment: "" |
| 25 | ) |
| 26 | case .invalidURL: |
| 27 | return NSLocalizedString( |
| 28 | "app_installer.error.invalid_url", |
| 29 | value: "Invalid URL", |
| 30 | comment: "" |
| 31 | ) |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | public enum Stage: Sendable { |
| 37 | case connecting(Double) |
| 38 | case uploading(Double) |
| 39 | case installing(String, Double?) |
| 40 | |
| 41 | public var displayName: String { |
| 42 | switch self { |
| 43 | case .connecting: |
| 44 | return NSLocalizedString( |
| 45 | "app_installer.stage.connecting", |
| 46 | value: "Connecting", |
| 47 | comment: "" |
| 48 | ) |
| 49 | case .uploading: |
| 50 | // installing is a better name although this is |
| 51 | // technically uploading |
| 52 | return NSLocalizedString( |
| 53 | "app_installer.stage.installing", |
| 54 | value: "Installing", |
| 55 | comment: "" |
| 56 | ) |
| 57 | case .installing: |
| 58 | return NSLocalizedString( |
| 59 | "app_installer.stage.verifying", |
| 60 | value: "Verifying", |
| 61 | comment: "" |
| 62 | ) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public var displayProgress: Double { |
| 67 | switch self { |
| 68 | case .connecting(let progress): |
| 69 | return progress |
| 70 | case .uploading(let progress): |
| 71 | return progress |
| 72 | case .installing(_, let progress): |
| 73 | return progress ?? 0 |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public let ipa: URL |
| 79 | public let udid: String |
| 80 | public let connectionPreferences: Connection.Preferences |
| 81 | public init( |
| 82 | ipa: URL, |
| 83 | udid: String, |
| 84 | connectionPreferences: Connection.Preferences |
| 85 | ) { |
| 86 | self.ipa = ipa |
| 87 | self.udid = udid |
| 88 | self.connectionPreferences = connectionPreferences |
| 89 | } |
| 90 | |
| 91 | // synchronous |
| 92 | public func install(progress: @escaping @Sendable (Stage) -> Void) async throws { |
| 93 | let connection = try await Connection.connection( |
| 94 | forUDID: udid, |
| 95 | preferences: connectionPreferences |
| 96 | ) { |
| 97 | progress(.connecting($0 * 4/6)) |
| 98 | } |
| 99 | |
| 100 | try Task.checkCancellation() |
| 101 | |
| 102 | let uploader = try await IPAUploader(connection: connection) |
| 103 | progress(.connecting(5/6)) |
| 104 | |
| 105 | try Task.checkCancellation() |
| 106 | |
| 107 | // we need to start the installer quickly because sometimes it fails if we do it after |
| 108 | // uploading the ipa to the device |
| 109 | let installer = try await IPAInstaller(connection: connection) |
| 110 | progress(.connecting(6/6)) |
| 111 | |
| 112 | try Task.checkCancellation() |
| 113 | |
| 114 | let uploaded = try await uploader.upload(app: ipa) { currentProgress in |
| 115 | progress(.uploading(currentProgress)) |
| 116 | } |
| 117 | defer { uploaded.delete() } |
| 118 | |
| 119 | try Task.checkCancellation() |
| 120 | |
| 121 | try await installer.install(uploaded: uploaded) { currentProgress in |
| 122 | progress(.installing(currentProgress.details, currentProgress.progress)) |
| 123 | } |
| 124 | |
| 125 | _ = connection |
| 126 | } |
| 127 | |
| 128 | } |