2015-08-21 21:02:36 +08:00
|
|
|
//
|
|
|
|
// Install.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Andrew Naylor on 21/08/2015.
|
|
|
|
// Copyright (c) 2015 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-07-04 14:56:10 -06:00
|
|
|
import Commandant
|
2018-10-14 13:41:19 -06:00
|
|
|
import CommerceKit
|
2018-07-04 14:56:10 -06:00
|
|
|
|
2018-12-28 15:08:31 -07:00
|
|
|
/// Installs previously purchased apps from the Mac App Store.
|
2018-10-14 15:35:48 -06:00
|
|
|
public struct InstallCommand: CommandProtocol {
|
|
|
|
public typealias Options = InstallOptions
|
|
|
|
public let verb = "install"
|
|
|
|
public let function = "Install from the Mac App Store"
|
|
|
|
|
2018-12-28 15:08:31 -07:00
|
|
|
private let appLibrary: AppLibrary
|
|
|
|
|
2020-03-28 21:15:07 -06:00
|
|
|
/// Public initializer.
|
|
|
|
public init() {
|
|
|
|
self.init(appLibrary: MasAppLibrary())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal initializer.
|
|
|
|
/// - Parameter appLibrary: AppLibrary manager.
|
|
|
|
init(appLibrary: AppLibrary = MasAppLibrary()) {
|
2018-12-28 15:08:31 -07:00
|
|
|
self.appLibrary = appLibrary
|
|
|
|
}
|
2018-12-27 14:02:14 -07:00
|
|
|
|
2019-01-11 18:28:03 -07:00
|
|
|
/// Runs the command.
|
2021-03-21 22:46:17 -07:00
|
|
|
public func run(_ options: Options) -> Result<Void, MASError> {
|
2016-09-06 21:28:24 +07:00
|
|
|
// Try to download applications with given identifiers and collect results
|
2018-04-03 19:34:45 -06:00
|
|
|
let downloadResults = options.appIds.compactMap { (appId) -> MASError? in
|
2018-12-28 15:08:31 -07:00
|
|
|
if let product = appLibrary.installedApp(forId: appId), !options.forceInstall {
|
2016-09-25 22:13:23 +01:00
|
|
|
printWarning("\(product.appName) is already installed")
|
2016-09-14 22:28:18 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-12-27 14:02:14 -07:00
|
|
|
|
2020-05-14 21:24:27 -06:00
|
|
|
return download(appId)
|
2016-09-14 22:28:18 +01:00
|
|
|
}
|
2016-09-14 22:07:56 +01:00
|
|
|
|
2016-09-14 22:28:18 +01:00
|
|
|
switch downloadResults.count {
|
|
|
|
case 0:
|
2017-12-28 09:45:08 -06:00
|
|
|
return .success(())
|
2016-09-14 22:28:18 +01:00
|
|
|
case 1:
|
2016-09-17 13:58:38 +01:00
|
|
|
return .failure(downloadResults[0])
|
2016-09-14 22:28:18 +01:00
|
|
|
default:
|
2016-09-25 22:13:23 +01:00
|
|
|
return .failure(.downloadFailed(error: nil))
|
2016-09-06 21:28:24 +07:00
|
|
|
}
|
2016-09-14 22:28:18 +01:00
|
|
|
}
|
2015-08-21 21:02:36 +08:00
|
|
|
}
|
|
|
|
|
2018-10-14 15:35:48 -06:00
|
|
|
public struct InstallOptions: OptionsProtocol {
|
2016-09-06 20:37:12 +07:00
|
|
|
let appIds: [UInt64]
|
2016-09-14 23:04:53 +01:00
|
|
|
let forceInstall: Bool
|
2018-10-14 15:35:48 -06:00
|
|
|
|
|
|
|
public static func create(_ appIds: [Int]) -> (_ forceInstall: Bool) -> InstallOptions {
|
2021-03-21 22:46:17 -07:00
|
|
|
{ forceInstall in
|
2019-01-29 23:15:24 -07:00
|
|
|
InstallOptions(appIds: appIds.map { UInt64($0) }, forceInstall: forceInstall)
|
2016-09-17 13:58:38 +01:00
|
|
|
}
|
2015-08-21 21:02:36 +08:00
|
|
|
}
|
2018-10-14 15:35:48 -06:00
|
|
|
|
2019-01-11 18:06:02 -07:00
|
|
|
public static func evaluate(_ mode: CommandMode) -> Result<InstallOptions, CommandantError<MASError>> {
|
2021-03-21 22:46:17 -07:00
|
|
|
create
|
2019-01-11 18:06:02 -07:00
|
|
|
<*> mode <| Argument(usage: "app ID(s) to install")
|
|
|
|
<*> mode <| Switch(flag: nil, key: "force", usage: "force reinstall")
|
2015-08-21 21:02:36 +08:00
|
|
|
}
|
2016-09-14 22:28:18 +01:00
|
|
|
}
|