From 27eeddcdc59656415ebdcc2e1fce2fb159cda9a1 Mon Sep 17 00:00:00 2001 From: kabiroberai Date: Fri, 17 Apr 2020 16:43:17 +0530 Subject: [PATCH] Implement supersign cancellation --- Supersign/Installation/AppInstaller.swift | 65 +++++++++++++++++------ 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/Supersign/Installation/AppInstaller.swift b/Supersign/Installation/AppInstaller.swift index 4415100c..c0ef14e4 100644 --- a/Supersign/Installation/AppInstaller.swift +++ b/Supersign/Installation/AppInstaller.swift @@ -12,10 +12,17 @@ import SwiftyMobileDevice public class AppInstaller { public enum Error: LocalizedError { + case userCancelled case invalidURL public var errorDescription: String? { switch self { + case .userCancelled: + return NSLocalizedString( + "app_installer.error.user_cancelled", + value: "The user cancelled the operation", + comment: "" + ) case .invalidURL: return NSLocalizedString( "app_installer.error.invalid_url", @@ -71,59 +78,85 @@ public class AppInstaller { label: "com.kabiroberai.Supersign.install-queue" ) - private init() {} - public static let shared = AppInstaller() + private var needsCancellation = false + // throws `Error.userCancelled` if the user cancelled this operation + private func cancelPoint() throws { + if needsCancellation { + throw Error.userCancelled + } + } - // synchronous - private func installSync( + public let ipa: URL + public let bundleID: String + public let udid: String + public let pairingKeys: Data + public init( ipa: URL, bundleID: String, udid: String, - pairingKeys: Data, + pairingKeys: Data + ) { + self.ipa = ipa + self.bundleID = bundleID + self.udid = udid + self.pairingKeys = pairingKeys + } + + #warning("We should have some sort of `static` queue/lock") + // because if two connections are attempted simultaneously, + // the heartbeat will fail. + + // synchronous + private func installSync( progress: @escaping (Stage) -> Void ) throws { + defer { needsCancellation = false } + try cancelPoint() + let connection = try Connection(udid: udid, pairingKeys: pairingKeys) { progress(.connecting($0 * 4/6)) } defer { connection.close() } + try cancelPoint() + let uploader = try IPAUploader(connection: connection) progress(.connecting(5/6)) + try cancelPoint() + // we need to start the installer quickly because sometimes it fails if we do it after // uploading the ipa to the device let installer = try IPAInstaller(connection: connection) progress(.connecting(6/6)) + try cancelPoint() + let uploaded = try uploader.upload(ipa: ipa, withBundleID: bundleID) { currentProgress in progress(.uploading(currentProgress)) } defer { uploaded.delete() } + try cancelPoint() + try installer.install(uploaded: uploaded, bundleID: bundleID) { currentProgress in progress(.installing(currentProgress.details, currentProgress.progress)) } } public func install( - ipa: URL, - bundleID: String, - udid: String, - pairingKeys: Data, progress: @escaping (Stage) -> Void, completion: @escaping (Result<(), Swift.Error>) -> Void ) { installQueue.async { completion(Result { - try self.installSync( - ipa: ipa, - bundleID: bundleID, - udid: udid, - pairingKeys: pairingKeys, - progress: progress - ) + try self.installSync(progress: progress) }) } } + public func cancel() { + needsCancellation = true + } + }