2015-12-30 21:03:45 +00:00
|
|
|
//
|
|
|
|
// Upgrade.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Andrew Naylor on 30/12/2015.
|
|
|
|
// Copyright © 2015 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-07-04 20:56:10 +00:00
|
|
|
import Commandant
|
2018-10-14 19:41:19 +00:00
|
|
|
import CommerceKit
|
2018-07-04 20:56:10 +00:00
|
|
|
|
2018-12-28 22:08:31 +00:00
|
|
|
/// Command which upgrades apps with new versions available in the Mac App Store.
|
2018-10-14 21:35:48 +00:00
|
|
|
public struct UpgradeCommand: CommandProtocol {
|
|
|
|
public typealias Options = UpgradeOptions
|
|
|
|
public let verb = "upgrade"
|
|
|
|
public let function = "Upgrade outdated apps from the Mac App Store"
|
|
|
|
|
2018-12-28 22:08:31 +00:00
|
|
|
private let appLibrary: AppLibrary
|
|
|
|
|
2020-03-29 03:15:07 +00:00
|
|
|
/// Public initializer.
|
|
|
|
/// - Parameter appLibrary: AppLibrary manager.
|
|
|
|
public init() {
|
|
|
|
self.init(appLibrary: MasAppLibrary())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal initializer.
|
|
|
|
/// - Parameter appLibrary: AppLibrary manager.
|
|
|
|
init(appLibrary: AppLibrary = MasAppLibrary()) {
|
2018-12-28 22:08:31 +00:00
|
|
|
self.appLibrary = appLibrary
|
|
|
|
}
|
2018-10-14 21:35:48 +00:00
|
|
|
|
2019-01-12 01:28:03 +00:00
|
|
|
/// Runs the command.
|
2018-10-14 21:35:48 +00:00
|
|
|
public func run(_ options: Options) -> Result<(), MASError> {
|
2016-09-17 12:58:38 +00:00
|
|
|
let updateController = CKUpdateController.shared()
|
2016-09-14 21:55:04 +00:00
|
|
|
let updates: [CKUpdate]
|
2017-08-19 15:50:35 +00:00
|
|
|
let apps = options.apps
|
|
|
|
if apps.count > 0 {
|
|
|
|
// convert input into a list of appId's
|
|
|
|
let appIds: [UInt64]
|
|
|
|
|
2018-04-04 01:34:45 +00:00
|
|
|
appIds = apps.compactMap {
|
2018-02-06 10:48:06 +00:00
|
|
|
if let appId = UInt64($0) {
|
2017-08-19 15:50:35 +00:00
|
|
|
return appId
|
|
|
|
}
|
2018-12-28 22:08:31 +00:00
|
|
|
if let appId = appLibrary.appIdsByName[$0] {
|
2017-08-19 15:50:35 +00:00
|
|
|
return appId
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check each of those for updates
|
2018-04-04 01:34:45 +00:00
|
|
|
updates = appIds.compactMap {
|
2017-08-19 15:50:35 +00:00
|
|
|
updateController?.availableUpdate(withItemIdentifier: $0)
|
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
|
2016-09-14 21:58:46 +00:00
|
|
|
guard updates.count > 0 else {
|
2016-09-25 21:13:23 +00:00
|
|
|
printWarning("Nothing found to upgrade")
|
2016-09-17 12:58:38 +00:00
|
|
|
return .success(())
|
2016-09-14 21:58:46 +00:00
|
|
|
}
|
2016-09-14 21:55:04 +00:00
|
|
|
} else {
|
2016-09-25 20:18:18 +00:00
|
|
|
updates = updateController?.availableUpdates() ?? []
|
|
|
|
|
2016-09-14 21:55:04 +00:00
|
|
|
// Upgrade everything
|
2016-09-25 20:18:18 +00:00
|
|
|
guard updates.count > 0 else {
|
2016-09-14 21:55:04 +00:00
|
|
|
print("Everything is up-to-date")
|
2016-09-17 12:58:38 +00:00
|
|
|
return .success(())
|
2016-09-14 21:55:04 +00:00
|
|
|
}
|
2015-12-30 21:20:25 +00:00
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
|
2015-12-30 21:20:25 +00:00
|
|
|
print("Upgrading \(updates.count) outdated application\(updates.count > 1 ? "s" : ""):")
|
2016-09-17 12:58:38 +00:00
|
|
|
print(updates.map({ "\($0.title) (\($0.bundleVersion))" }).joined(separator: ", "))
|
2019-01-12 01:06:02 +00:00
|
|
|
|
2018-04-04 01:34:45 +00:00
|
|
|
let updateResults = updates.compactMap {
|
2016-09-17 12:58:38 +00:00
|
|
|
download($0.itemIdentifier.uint64Value)
|
2016-09-14 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch updateResults.count {
|
|
|
|
case 0:
|
2017-12-28 15:45:08 +00:00
|
|
|
return .success(())
|
2016-09-14 21:55:04 +00:00
|
|
|
case 1:
|
2016-09-17 12:58:38 +00:00
|
|
|
return .failure(updateResults[0])
|
2016-09-14 21:55:04 +00:00
|
|
|
default:
|
2016-09-25 21:13:23 +00:00
|
|
|
return .failure(.downloadFailed(error: nil))
|
2015-12-30 21:03:45 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-30 21:20:25 +00:00
|
|
|
}
|
2016-09-14 21:55:04 +00:00
|
|
|
|
2018-10-14 21:35:48 +00:00
|
|
|
public struct UpgradeOptions: OptionsProtocol {
|
2017-08-19 15:50:35 +00:00
|
|
|
let apps: [String]
|
2019-01-12 01:06:02 +00:00
|
|
|
|
2017-08-19 15:50:35 +00:00
|
|
|
static func create(_ apps: [String]) -> UpgradeOptions {
|
|
|
|
return UpgradeOptions(apps: apps)
|
2016-09-14 21:55:04 +00:00
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
|
|
|
|
public static func evaluate(_ mode: CommandMode) -> Result<UpgradeOptions, CommandantError<MASError>> {
|
2016-09-14 21:55:04 +00:00
|
|
|
return create
|
2019-01-12 01:06:02 +00:00
|
|
|
<*> mode <| Argument(defaultValue: [], usage: "app(s) to upgrade")
|
2016-09-14 21:55:04 +00:00
|
|
|
}
|
2016-09-17 12:58:38 +00:00
|
|
|
}
|