Added ability to purchase free apps to mas tool.

This commit is contained in:
Jakob Rieck 2017-11-28 13:39:44 +01:00 committed by Maximilian Blochberger
parent 281dcf084c
commit c0bb85d6e6
No known key found for this signature in database
GPG key ID: A2A8F3CC6591A5FA
7 changed files with 76 additions and 7 deletions

View file

@ -16,6 +16,7 @@
30EA893640B02CCF679F9C57 /* Option.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AD7FE171F643805F7BC38A7 /* Option.swift */; };
693A98991CBFFA760004D3B4 /* Search.swift in Sources */ = {isa = PBXBuildFile; fileRef = 693A98981CBFFA760004D3B4 /* Search.swift */; };
693A989B1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift in Sources */ = {isa = PBXBuildFile; fileRef = 693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */; };
75FB3E761F9F7841005B6F20 /* Purchase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75FB3E751F9F7841005B6F20 /* Purchase.swift */; };
AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9D96DDBBCCCC5944160ABE /* ArgumentParser.swift */; };
ADE553C828AF4EAFF39ED3E1 /* ArgumentProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4237E5AA1A289D03D2A2FB8 /* ArgumentProtocol.swift */; };
EBD6B44FDF65E0253153629F /* HelpCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FDC2B8063EC231E28353D23 /* HelpCommand.swift */; };
@ -59,6 +60,7 @@
55E3BFBE58DFCE19A53A23D7 /* LinuxSupport.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = LinuxSupport.swift; path = Seeds/Commandant/Sources/Commandant/LinuxSupport.swift; sourceTree = "<group>"; };
693A98981CBFFA760004D3B4 /* Search.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Search.swift; sourceTree = "<group>"; };
693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSURLSession+Synchronous.swift"; sourceTree = "<group>"; };
75FB3E751F9F7841005B6F20 /* Purchase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Purchase.swift; sourceTree = "<group>"; };
8FDC2B8063EC231E28353D23 /* HelpCommand.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = HelpCommand.swift; path = Seeds/Commandant/Sources/Commandant/HelpCommand.swift; sourceTree = "<group>"; };
9257C5FABA335E5F060CB7F7 /* Result.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Result.swift; path = Seeds/Result/Result/Result.swift; sourceTree = "<group>"; };
AF1B6BEDF32AF3F8A575FB1F /* Switch.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Switch.swift; path = Seeds/Commandant/Sources/Commandant/Switch.swift; sourceTree = "<group>"; };
@ -206,6 +208,7 @@
EDE296521C700F4300554778 /* SignOut.swift */,
EDD3B3621C34709400B56B88 /* Upgrade.swift */,
EDB6CE8B1BAEC3D400648B4D /* Version.swift */,
75FB3E751F9F7841005B6F20 /* Purchase.swift */,
);
name = Commands;
path = "mas-cli/Commands";
@ -374,6 +377,7 @@
EDE296531C700F4300554778 /* SignOut.swift in Sources */,
EDA3BE521B8B84AF00C18D70 /* SSPurchase.swift in Sources */,
15E27926A580EABEB1B218EF /* Switch.swift in Sources */,
75FB3E761F9F7841005B6F20 /* Purchase.swift in Sources */,
EDD3B3631C34709400B56B88 /* Upgrade.swift in Sources */,
EDCBF9551D89CFC7000039C6 /* Utilities.swift in Sources */,
EDB6CE8C1BAEC3D400648B4D /* Version.swift in Sources */,

View file

@ -6,14 +6,14 @@
// Copyright (c) 2015 Andrew Naylor. All rights reserved.
//
func download(_ adamId: UInt64) -> MASError? {
func download(_ adamId: UInt64, isPurchase: Bool) -> MASError? {
guard let account = ISStoreAccount.primaryAccount else {
return .notSignedIn
}
let group = DispatchGroup()
let purchase = SSPurchase(adamId: adamId, account: account)
let purchase = SSPurchase(adamId: adamId, account: account, isPurchase: isPurchase)
var purchaseError: MASError?
var observerIdentifier: Any? = nil

View file

@ -9,13 +9,23 @@
typealias SSPurchaseCompletion = (_ purchase: SSPurchase?, _ completed: Bool, _ error: Error?, _ response: SSPurchaseResponse?) -> ()
extension SSPurchase {
convenience init(adamId: UInt64, account: ISStoreAccount) {
convenience init(adamId: UInt64, account: ISStoreAccount, isPurchase: Bool) {
self.init()
self.buyParameters = "productType=C&price=0&salableAdamId=\(adamId)&pricingParameters=STDRDL&pg=default&appExtVrsId=0"
if isPurchase {
self.buyParameters = "productType=C&price=0&salableAdamId=\(adamId)&pricingParameters=STDQ&pg=default&appExtVrsId=0&macappinstalledconfirmed=1"
} else {
// is redownload, use existing functionality
self.buyParameters = "productType=C&price=0&salableAdamId=\(adamId)&pricingParameters=STDRDL&pg=default&appExtVrsId=0"
}
self.itemIdentifier = adamId
self.accountIdentifier = account.dsID
self.appleID = account.identifier
// Not sure if this is needed, but lets use it here.
if isPurchase {
self.isRedownload = false
}
let downloadMetadata = SSDownloadMetadata()
downloadMetadata.kind = "software"
downloadMetadata.itemIdentifier = adamId

View file

@ -19,7 +19,7 @@ struct InstallCommand: CommandProtocol {
return nil
}
return download(appId)
return download(appId, isPurchase: false)
}
switch downloadResults.count {

View file

@ -0,0 +1,54 @@
//
// Purchase.swift
// mas-cli
//
// Created by Jakob Rieck on 24/10/2017.
// Copyright (c) 2017 Jakob Rieck. All rights reserved.
//
struct PurchaseCommand: CommandProtocol {
typealias Options = PurchaseOptions
let verb = "purchase"
let function = "purchase and download free apps from the Mac App Store"
func run(_ options: Options) -> Result<(), MASError> {
// Try to download applications with given identifiers and collect results
let downloadResults = options.appIds.flatMap { (appId) -> MASError? in
if let product = installedApp(appId) {
printWarning("\(product.appName) has already been purchased.")
return nil
}
return download(appId, isPurchase: true)
}
switch downloadResults.count {
case 0:
return .success()
case 1:
return .failure(downloadResults[0])
default:
return .failure(.downloadFailed(error: nil))
}
}
fileprivate func installedApp(_ appId: UInt64) -> CKSoftwareProduct? {
let appId = NSNumber(value: appId)
let softwareMap = CKSoftwareMap.shared()
return softwareMap.allProducts()?.first { $0.itemIdentifier == appId }
}
}
struct PurchaseOptions: OptionsProtocol {
let appIds: [UInt64]
static func create(_ appIds: [Int]) -> PurchaseOptions {
return PurchaseOptions(appIds: appIds.map{UInt64($0)})
}
static func evaluate(_ m: CommandMode) -> Result<PurchaseOptions, CommandantError<MASError>> {
return create
<*> m <| Argument(usage: "app ID(s) to install")
}
}

View file

@ -37,7 +37,7 @@ struct UpgradeCommand: CommandProtocol {
print(updates.map({ "\($0.title) (\($0.bundleVersion))" }).joined(separator: ", "))
let updateResults = updates.flatMap {
download($0.itemIdentifier.uint64Value)
download($0.itemIdentifier.uint64Value, isPurchase: false)
}
switch updateResults.count {

View file

@ -18,6 +18,7 @@ let registry = CommandRegistry<MASError>()
let helpCommand = HelpCommand(registry: registry)
registry.register(AccountCommand())
registry.register(InstallCommand())
registry.register(PurchaseCommand())
registry.register(ListCommand())
registry.register(OutdatedCommand())
registry.register(ResetCommand())