mirror of
https://github.com/mas-cli/mas
synced 2024-11-28 22:40:24 +00:00
Add a little more order to the download process
This commit is contained in:
parent
41ceab2c3a
commit
9205b94155
4 changed files with 51 additions and 20 deletions
|
@ -14,17 +14,30 @@ func download(adamId: UInt64) -> MASError? {
|
||||||
|
|
||||||
var purchaseError: MASError?
|
var purchaseError: MASError?
|
||||||
|
|
||||||
purchase.perform { purchase, completed, error, response in
|
purchase.perform { purchase, unused, error, response in
|
||||||
if completed {
|
if let error = error {
|
||||||
|
purchaseError = MASError(code: .PurchaseError, sourceError: error)
|
||||||
|
dispatch_group_leave(group)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if let downloads = response.downloads as? [SSDownload] where count(downloads) > 0 {
|
||||||
let observer = PurchaseDownloadObserver(purchase: purchase)
|
let observer = PurchaseDownloadObserver(purchase: purchase)
|
||||||
observer.onCompletion {
|
|
||||||
|
observer.errorHandler = { error in
|
||||||
|
purchaseError = error
|
||||||
|
dispatch_group_leave(group)
|
||||||
|
}
|
||||||
|
|
||||||
|
observer.completionHandler = {
|
||||||
dispatch_group_leave(group)
|
dispatch_group_leave(group)
|
||||||
}
|
}
|
||||||
|
|
||||||
CKDownloadQueue.sharedDownloadQueue().addObserver(observer)
|
CKDownloadQueue.sharedDownloadQueue().addObserver(observer)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
purchaseError = MASError(code: .PurchaseError, sourceError: error)
|
println("No downloads")
|
||||||
|
purchaseError = MASError(code: .NoDownloads)
|
||||||
dispatch_group_leave(group)
|
dispatch_group_leave(group)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,35 +11,45 @@ let csi = "\u{001B}["
|
||||||
@objc class PurchaseDownloadObserver: CKDownloadQueueObserver {
|
@objc class PurchaseDownloadObserver: CKDownloadQueueObserver {
|
||||||
let purchase: SSPurchase
|
let purchase: SSPurchase
|
||||||
var completionHandler: (() -> ())?
|
var completionHandler: (() -> ())?
|
||||||
var started = false
|
var errorHandler: ((MASError) -> ())?
|
||||||
|
|
||||||
init(purchase: SSPurchase) {
|
init(purchase: SSPurchase) {
|
||||||
self.purchase = purchase
|
self.purchase = purchase
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadQueue(queue: CKDownloadQueue, statusChangedForDownload download: SSDownload!) {
|
func downloadQueue(queue: CKDownloadQueue, statusChangedForDownload download: SSDownload!) {
|
||||||
if !started {
|
if download.metadata.itemIdentifier != purchase.itemIdentifier {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
progress(download.status.progressState)
|
let status = download.status
|
||||||
|
if status.failed || status.cancelled {
|
||||||
|
queue.removeDownloadWithItemIdentifier(download.metadata.itemIdentifier)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
progress(status.progressState)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadQueue(queue: CKDownloadQueue, changedWithAddition download: SSDownload!) {
|
func downloadQueue(queue: CKDownloadQueue, changedWithAddition download: SSDownload!) {
|
||||||
started = true
|
|
||||||
println("==> Downloading " + download.metadata.title)
|
println("==> Downloading " + download.metadata.title)
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadQueue(queue: CKDownloadQueue, changedWithRemoval download: SSDownload!) {
|
func downloadQueue(queue: CKDownloadQueue, changedWithRemoval download: SSDownload!) {
|
||||||
println("")
|
clearLine()
|
||||||
|
let status = download.status
|
||||||
|
if status.failed {
|
||||||
|
println("==> Download Failed: \(status.error.localizedDescription)")
|
||||||
|
errorHandler?(MASError(code: .DownloadFailed, sourceError: status.error))
|
||||||
|
}
|
||||||
|
else if status.cancelled {
|
||||||
|
println("==> Download Cancelled")
|
||||||
|
errorHandler?(MASError(code: .Cancelled))
|
||||||
|
}
|
||||||
|
else {
|
||||||
println("==> Installed " + download.metadata.title)
|
println("==> Installed " + download.metadata.title)
|
||||||
if let complete = self.completionHandler {
|
completionHandler?()
|
||||||
complete()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func onCompletion(complete: () -> ()) {
|
|
||||||
self.completionHandler = complete
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ProgressState {
|
struct ProgressState {
|
||||||
|
@ -69,10 +79,15 @@ func progress(state: ProgressState) {
|
||||||
bar += "-"
|
bar += "-"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print("\(csi)2K\(csi)0G\(bar) \(state.percentage) \(state.phase)")
|
clearLine()
|
||||||
|
print("\(bar) \(state.percentage) \(state.phase)")
|
||||||
fflush(stdout)
|
fflush(stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func clearLine() {
|
||||||
|
print("\(csi)2K\(csi)0G")
|
||||||
|
}
|
||||||
|
|
||||||
extension SSDownloadStatus {
|
extension SSDownloadStatus {
|
||||||
var progressState: ProgressState {
|
var progressState: ProgressState {
|
||||||
let phase = activePhase?.phaseDescription ?? "Waiting"
|
let phase = activePhase?.phaseDescription ?? "Waiting"
|
||||||
|
|
|
@ -14,6 +14,9 @@ public enum MASErrorCode: Int {
|
||||||
case NoError
|
case NoError
|
||||||
case NotSignedIn
|
case NotSignedIn
|
||||||
case PurchaseError
|
case PurchaseError
|
||||||
|
case NoDownloads
|
||||||
|
case Cancelled
|
||||||
|
case DownloadFailed
|
||||||
|
|
||||||
var exitCode: Int32 {
|
var exitCode: Int32 {
|
||||||
return Int32(self.rawValue)
|
return Int32(self.rawValue)
|
||||||
|
|
|
@ -17,7 +17,7 @@ registry.register(ListInstalledCommand())
|
||||||
registry.register(ListUpdatesCommand())
|
registry.register(ListUpdatesCommand())
|
||||||
registry.register(helpCommand)
|
registry.register(helpCommand)
|
||||||
|
|
||||||
registry.main(defaultVerb: helpCommand.verb, errorHandler: { error in
|
registry.main(defaultVerb: helpCommand.verb) { error in
|
||||||
fputs(error.description + "\n", stderr)
|
exit(Int32(error.code))
|
||||||
})
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue