diff --git a/Sources/mas/AppStore/Downloader.swift b/Sources/mas/AppStore/Downloader.swift index 6435ff6..e95bb1c 100644 --- a/Sources/mas/AppStore/Downloader.swift +++ b/Sources/mas/AppStore/Downloader.swift @@ -14,17 +14,16 @@ import StoreFoundation /// /// - Parameters: /// - appIDs: The IDs of the apps to be downloaded -/// - purchase: Flag indicating whether the apps needs to be purchased. -/// Only works for free apps. Defaults to false. +/// - purchasing: Flag indicating if the apps will be purchased. Only works for free apps. Defaults to false. /// - Returns: A promise that completes when the downloads are complete. If any fail, /// the promise is rejected with the first error, after all remaining downloads are attempted. -func downloadAll(_ appIDs: [AppID], purchase: Bool = false) -> Promise { +func downloadApps(withAppIDs appIDs: [AppID], purchasing: Bool = false) -> Promise { var firstError: Error? return appIDs .reduce(Guarantee.value(())) { previous, appID in previous.then { - downloadWithRetries(appID, purchase: purchase) + downloadApp(withAppID: appID, purchasing: purchasing) .recover { error in if firstError == nil { firstError = error @@ -39,10 +38,15 @@ func downloadAll(_ appIDs: [AppID], purchase: Bool = false) -> Promise { } } -private func downloadWithRetries(_ appID: AppID, purchase: Bool = false, attempts: Int = 3) -> Promise { - SSPurchase().perform(appID: appID, purchase: purchase) +private func downloadApp( + withAppID appID: AppID, + purchasing: Bool = false, + withAttemptCount attemptCount: UInt32 = 3 +) -> Promise { + SSPurchase() + .perform(appID: appID, purchasing: purchasing) .recover { error in - guard attempts > 1 else { + guard attemptCount > 1 else { throw error } @@ -54,9 +58,9 @@ private func downloadWithRetries(_ appID: AppID, purchase: Bool = false, attempt throw error } - let attempts = attempts - 1 + let attemptCount = attemptCount - 1 printWarning((downloadError ?? error).localizedDescription) - printWarning("Trying again up to \(attempts) more \(attempts == 1 ? "time" : "times").") - return downloadWithRetries(appID, purchase: purchase, attempts: attempts) + printWarning("Trying again up to \(attemptCount) more \(attemptCount == 1 ? "time" : "times").") + return downloadApp(withAppID: appID, purchasing: purchasing, withAttemptCount: attemptCount) } } diff --git a/Sources/mas/AppStore/SSPurchase.swift b/Sources/mas/AppStore/SSPurchase.swift index cf712cd..a12a1c9 100644 --- a/Sources/mas/AppStore/SSPurchase.swift +++ b/Sources/mas/AppStore/SSPurchase.swift @@ -11,7 +11,7 @@ import PromiseKit import StoreFoundation extension SSPurchase { - func perform(appID: AppID, purchase: Bool) -> Promise { + func perform(appID: AppID, purchasing: Bool) -> Promise { var parameters: [String: Any] = [ "productType": "C", "price": 0, @@ -20,9 +20,11 @@ extension SSPurchase { "appExtVrsId": 0, ] - if purchase { + if purchasing { parameters["macappinstalledconfirmed"] = 1 parameters["pricingParameters"] = "STDQ" + // Possibly unnecessary… + isRedownload = false } else { parameters["pricingParameters"] = "STDRDL" } @@ -35,11 +37,6 @@ extension SSPurchase { itemIdentifier = appID - // Not sure if this is needed… - if purchase { - isRedownload = false - } - downloadMetadata = SSDownloadMetadata() downloadMetadata.kind = "software" downloadMetadata.itemIdentifier = appID diff --git a/Sources/mas/Commands/Install.swift b/Sources/mas/Commands/Install.swift index 7247172..32f03fd 100644 --- a/Sources/mas/Commands/Install.swift +++ b/Sources/mas/Commands/Install.swift @@ -38,7 +38,7 @@ extension MAS { } do { - try downloadAll(appIDs).wait() + try downloadApps(withAppIDs: appIDs).wait() } catch { throw error as? MASError ?? .downloadFailed(error: error as NSError) } diff --git a/Sources/mas/Commands/Lucky.swift b/Sources/mas/Commands/Lucky.swift index 400e5ef..dc57e41 100644 --- a/Sources/mas/Commands/Lucky.swift +++ b/Sources/mas/Commands/Lucky.swift @@ -66,7 +66,7 @@ extension MAS { printWarning("\(appName) is already installed") } else { do { - try downloadAll([appID]).wait() + try downloadApps(withAppIDs: [appID]).wait() } catch { throw error as? MASError ?? .downloadFailed(error: error as NSError) } diff --git a/Sources/mas/Commands/Purchase.swift b/Sources/mas/Commands/Purchase.swift index bdb8a58..dab6d24 100644 --- a/Sources/mas/Commands/Purchase.swift +++ b/Sources/mas/Commands/Purchase.swift @@ -35,7 +35,7 @@ extension MAS { } do { - try downloadAll(appIDs, purchase: true).wait() + try downloadApps(withAppIDs: appIDs, purchasing: true).wait() } catch { throw error as? MASError ?? .downloadFailed(error: error as NSError) } diff --git a/Sources/mas/Commands/Upgrade.swift b/Sources/mas/Commands/Upgrade.swift index 2770f39..94f8b50 100644 --- a/Sources/mas/Commands/Upgrade.swift +++ b/Sources/mas/Commands/Upgrade.swift @@ -46,7 +46,7 @@ extension MAS { ) do { - try downloadAll(apps.map(\.installedApp.itemIdentifier.appIDValue)).wait() + try downloadApps(withAppIDs: apps.map(\.installedApp.itemIdentifier.appIDValue)).wait() } catch { throw error as? MASError ?? .downloadFailed(error: error as NSError) }