Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ _ = Package(
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.6.1"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.3.0"),
.package(url: "https://github.com/apple/swift-collections.git", from: "1.3.0"),
.package(url: "https://github.com/mxcl/Version.git", from: "2.2.0"),
],
targets: [
.plugin(name: "MASBuildToolPlugin", capability: .buildTool()),
Expand All @@ -37,7 +36,6 @@ _ = Package(
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "Collections", package: "swift-collections"),
"Version",
],
swiftSettings: swiftSettings,
linkerSettings: [.unsafeFlags(["-F", "/System/Library/PrivateFrameworks"])],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// PurchaseDownloadObserver.swift
// DownloadQueueObserver.swift
// mas
//
// Copyright © 2015 mas-cli. All rights reserved.
Expand All @@ -15,17 +15,23 @@ private var installingPhaseType: Int64 { 1 }
private var initialPhaseType: Int64 { 4 }
private var downloadedPhaseType: Int64 { 5 }

final class PurchaseDownloadObserver: CKDownloadQueueObserver {
final class DownloadQueueObserver: CKDownloadQueueObserver {
private let adamID: ADAMID
private let printer: Printer
private let shouldCancel: (SSDownload, Bool) -> Bool

private var completionHandler: (() -> Void)?
private var errorHandler: ((Error) -> Void)?
private var prevPhaseType: Int64?

init(adamID: ADAMID, printer: Printer) {
init(
adamID: ADAMID,
printer: Printer,
shouldCancel: @Sendable @escaping (SSDownload, Bool) -> Bool = { _, _ in false }
) {
self.adamID = adamID
self.printer = printer
self.shouldCancel = shouldCancel
}

deinit {
Expand All @@ -40,6 +46,10 @@ final class PurchaseDownloadObserver: CKDownloadQueueObserver {
else {
return
}
guard status.isCancelled || !shouldCancel(download, true) else {
queue.cancelDownload(download, promptToConfirm: false, askToDelete: false)
return
}

if status.isFailed || status.isCancelled {
queue.removeDownload(withItemIdentifier: adamID)
Expand All @@ -65,12 +75,37 @@ final class PurchaseDownloadObserver: CKDownloadQueueObserver {
if status.isFailed {
errorHandler?(status.error ?? MASError.runtimeError("Failed to download \(metadata.appNameAndVersion)"))
} else if status.isCancelled {
errorHandler?(MASError.cancelled)
guard shouldCancel(download, false) else {
errorHandler?(MASError.cancelled)
return
}

completionHandler?()
} else {
printer.notice("Installed", metadata.appNameAndVersion)
completionHandler?()
}
}

func observeDownloadQueue(_ queue: CKDownloadQueue = .shared()) async throws {
let observerID = queue.add(self)
defer {
queue.removeObserver(observerID)
}

try await withCheckedThrowingContinuation { continuation in
completionHandler = {
self.completionHandler = nil
self.errorHandler = nil
continuation.resume()
}
errorHandler = { error in
self.completionHandler = nil
self.errorHandler = nil
continuation.resume(throwing: error)
}
}
}
}

private struct ProgressState { // swiftlint:disable:this one_declaration_per_file
Expand Down Expand Up @@ -158,25 +193,3 @@ private extension SSDownloadPhase {
}
}
}

extension PurchaseDownloadObserver {
func observeDownloadQueue(_ queue: CKDownloadQueue = .shared()) async throws {
let observerID = queue.add(self)
defer {
queue.removeObserver(observerID)
}

try await withCheckedThrowingContinuation { continuation in
completionHandler = {
self.completionHandler = nil
self.errorHandler = nil
continuation.resume()
}
errorHandler = { error in
self.completionHandler = nil
self.errorHandler = nil
continuation.resume(throwing: error)
}
}
}
}
56 changes: 27 additions & 29 deletions Sources/mas/AppStore/Downloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,35 @@

private import CommerceKit
private import Foundation
private import StoreFoundation
internal import StoreFoundation

struct Downloader {
let printer: Printer

func downloadApps(
withAppIDs appIDs: [AppID],
purchasing: Bool,
forceDownload: Bool,
installedApps: [InstalledApp],
searcher: AppStoreSearcher
) async {
for appID in appIDs.filter({ appID in
if let installedApp = installedApps.first(where: { appID.matches($0) }), !forceDownload {
printer.warning(
purchasing ? "Already purchased: " : "Already installed: ",
installedApp.name,
" (",
appID,
")",
separator: ""
)
return false
}
return true
}) {
do {
try await downloadApp(withADAMID: try await appID.adamID(searcher: searcher), purchasing: purchasing)
} catch {
printer.error(error: error)
}
func downloadApp(
withADAMID adamID: ADAMID,
purchasing: Bool = false,
forceDownload: Bool = false, // swiftlint:disable:this function_default_parameter_at_end
installedApps: [InstalledApp]
) async throws {
if !forceDownload, let installedApp = installedApps.first(where: { $0.adamID == adamID }) {
printer.warning(
purchasing ? "Already purchased: " : "Already installed: ",
installedApp.name,
" (",
adamID,
")",
separator: ""
)
return
}
try await downloadApp(withADAMID: adamID, purchasing: purchasing)
}

func downloadApp(
withADAMID adamID: ADAMID,
purchasing: Bool = false,
shouldCancel: @Sendable @escaping (SSDownload, Bool) -> Bool = { _, _ in false },
withAttemptCount attemptCount: UInt32 = 3
) async throws {
do {
Expand All @@ -55,7 +47,8 @@ struct Downloader {
} else if response?.downloads?.isEmpty == false {
Task {
do {
try await PurchaseDownloadObserver(adamID: adamID, printer: printer).observeDownloadQueue()
try await DownloadQueueObserver(adamID: adamID, printer: printer, shouldCancel: shouldCancel)
.observeDownloadQueue() // swiftformat:disable:this indent
continuation.resume()
} catch {
continuation.resume(throwing: error)
Expand Down Expand Up @@ -84,7 +77,12 @@ struct Downloader {
error,
separator: ""
)
try await downloadApp(withADAMID: adamID, purchasing: purchasing, withAttemptCount: attemptCount)
try await downloadApp(
withADAMID: adamID,
purchasing: purchasing,
shouldCancel: shouldCancel,
withAttemptCount: attemptCount
)
}
}
}
19 changes: 12 additions & 7 deletions Sources/mas/Commands/Install.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ extension MAS {

func run(installedApps: [InstalledApp], searcher: AppStoreSearcher) async throws {
try await MAS.run { printer in
await Downloader(printer: printer).downloadApps(
withAppIDs: requiredAppIDsOptionGroup.appIDs,
purchasing: false,
forceDownload: forceOptionGroup.force,
installedApps: installedApps,
searcher: searcher
)
let downloader = Downloader(printer: printer)
for appID in requiredAppIDsOptionGroup.appIDs {
do {
try await downloader.downloadApp(
withADAMID: try await appID.adamID(searcher: searcher),
forceDownload: forceOptionGroup.force,
installedApps: installedApps
)
} catch {
printer.error(error: error)
}
}
}
}
}
Expand Down
24 changes: 0 additions & 24 deletions Sources/mas/Commands/OptionGroups/VerboseOptionGroup.swift

This file was deleted.

43 changes: 22 additions & 21 deletions Sources/mas/Commands/Outdated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

internal import ArgumentParser
private import StoreFoundation

extension MAS {
/// Outputs a list of installed apps which have updates available to be
Expand All @@ -15,38 +16,38 @@ extension MAS {
abstract: "List pending app updates from the Mac App Store"
)

@OptionGroup
var verboseOptionGroup: VerboseOptionGroup
@OptionGroup
var optionalAppIDsOptionGroup: OptionalAppIDsOptionGroup

func run() async throws {
try await run(installedApps: await installedApps, searcher: ITunesSearchAppStoreSearcher())
try await run(installedApps: await installedApps)
}

func run(installedApps: [InstalledApp], searcher: AppStoreSearcher) async throws {
try await MAS.run { await run(printer: $0, installedApps: installedApps, searcher: searcher) }
func run(installedApps: [InstalledApp]) async throws {
try await MAS.run { await run(downloader: Downloader(printer: $0), installedApps: installedApps) }
}

private func run(printer: Printer, installedApps: [InstalledApp], searcher: AppStoreSearcher) async {
for installedApp in installedApps.filter(by: optionalAppIDsOptionGroup, printer: printer) {
private func run(downloader: Downloader, installedApps: [InstalledApp]) async {
for installedApp in installedApps.filter(by: optionalAppIDsOptionGroup, printer: downloader.printer) {
do {
let storeApp = try await searcher.lookup(appID: installedApp.id)
if installedApp.isOutdated(comparedTo: storeApp) {
printer.info(
installedApp.adamID,
" ",
installedApp.name,
" (",
installedApp.version,
" -> ",
storeApp.version,
")",
separator: ""
)
try await downloader.downloadApp(withADAMID: installedApp.adamID) { download, shouldOutput in
if shouldOutput, let metadata = download.metadata, installedApp.version != metadata.bundleVersion {
downloader.printer.info(
installedApp.adamID,
" ",
installedApp.name,
" (",
installedApp.version,
" -> ",
metadata.bundleVersion ?? "unknown",
")",
separator: ""
)
}
return true
}
} catch {
verboseOptionGroup.printProblem(forError: error, expectedAppName: installedApp.name, printer: printer)
downloader.printer.error(error: error)
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions Sources/mas/Commands/Purchase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ extension MAS {

func run(installedApps: [InstalledApp], searcher: AppStoreSearcher) async throws {
try await MAS.run { printer in
await Downloader(printer: printer).downloadApps(
withAppIDs: requiredAppIDsOptionGroup.appIDs,
purchasing: true,
forceDownload: false,
installedApps: installedApps,
searcher: searcher
)
let downloader = Downloader(printer: printer)
for appID in requiredAppIDsOptionGroup.appIDs {
do {
try await downloader.downloadApp(
withADAMID: try await appID.adamID(searcher: searcher),
purchasing: true,
installedApps: installedApps
)
} catch {
printer.error(error: error)
}
}
}
}
}
Expand Down
Loading