mirror of
https://github.com/mas-cli/mas
synced 2024-11-21 19:23:01 +00:00
Improve download functions.
Partial #533 Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
This commit is contained in:
parent
0a05cd438f
commit
9eef8b6cb8
6 changed files with 22 additions and 21 deletions
|
@ -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<Void> {
|
||||
func downloadApps(withAppIDs appIDs: [AppID], purchasing: Bool = false) -> Promise<Void> {
|
||||
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<Void> {
|
|||
}
|
||||
}
|
||||
|
||||
private func downloadWithRetries(_ appID: AppID, purchase: Bool = false, attempts: Int = 3) -> Promise<Void> {
|
||||
SSPurchase().perform(appID: appID, purchase: purchase)
|
||||
private func downloadApp(
|
||||
withAppID appID: AppID,
|
||||
purchasing: Bool = false,
|
||||
withAttemptCount attemptCount: UInt32 = 3
|
||||
) -> Promise<Void> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ import PromiseKit
|
|||
import StoreFoundation
|
||||
|
||||
extension SSPurchase {
|
||||
func perform(appID: AppID, purchase: Bool) -> Promise<Void> {
|
||||
func perform(appID: AppID, purchasing: Bool) -> Promise<Void> {
|
||||
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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue