From 71fbe2e444168c715f8a2e57923c0545fd5f2682 Mon Sep 17 00:00:00 2001 From: Ross Goldberg <484615+rgoldberg@users.noreply.github.com> Date: Mon, 21 Oct 2024 07:12:32 -0400 Subject: [PATCH] Improve spacing. Partial #592 Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com> --- Sources/mas/AppStore/Downloader.swift | 24 +++++++++++-------- Sources/mas/AppStore/ISStoreAccount.swift | 21 +++++++++------- .../AppStore/PurchaseDownloadObserver.swift | 3 ++- Sources/mas/AppStore/SSPurchase.swift | 24 +++++++++---------- Sources/mas/Commands/Open.swift | 6 ++--- Sources/mas/Commands/Outdated.swift | 3 ++- Sources/mas/Commands/Upgrade.swift | 6 +++-- Sources/mas/Commands/Vendor.swift | 8 +++---- Sources/mas/Controllers/MasAppLibrary.swift | 7 +++--- Sources/mas/Controllers/MasStoreSearch.swift | 20 +++++++++------- Sources/mas/Formatters/Utilities.swift | 4 +++- .../Controllers/StoreSearchMock.swift | 3 +-- .../Network/NetworkSessionMockFromFile.swift | 5 ++-- 13 files changed, 75 insertions(+), 59 deletions(-) diff --git a/Sources/mas/AppStore/Downloader.swift b/Sources/mas/AppStore/Downloader.swift index cb5bbc3..bb09f3d 100644 --- a/Sources/mas/AppStore/Downloader.swift +++ b/Sources/mas/AppStore/Downloader.swift @@ -20,19 +20,23 @@ import StoreFoundation /// the promise is rejected with the first error, after all remaining downloads are attempted. func downloadAll(_ appIDs: [AppID], purchase: Bool = false) -> Promise { var firstError: Error? - return appIDs.reduce(Guarantee.value(())) { previous, appID in - previous.then { - downloadWithRetries(appID, purchase: purchase).recover { error in - if firstError == nil { - firstError = error - } + return + appIDs + .reduce(Guarantee.value(())) { previous, appID in + previous.then { + downloadWithRetries(appID, purchase: purchase) + .recover { error in + if firstError == nil { + firstError = error + } + } } } - }.done { - if let error = firstError { - throw error + .done { + if let error = firstError { + throw error + } } - } } private func downloadWithRetries(_ appID: AppID, purchase: Bool = false, attempts: Int = 3) -> Promise { diff --git a/Sources/mas/AppStore/ISStoreAccount.swift b/Sources/mas/AppStore/ISStoreAccount.swift index 7758ea4..f1d4fe0 100644 --- a/Sources/mas/AppStore/ISStoreAccount.swift +++ b/Sources/mas/AppStore/ISStoreAccount.swift @@ -15,13 +15,15 @@ extension ISStoreAccount: StoreAccount { if #available(macOS 10.13, *) { return race( Promise { seal in - ISServiceProxy.genericShared().accountService.primaryAccount { storeAccount in - seal.fulfill(storeAccount) - } + ISServiceProxy.genericShared().accountService + .primaryAccount { storeAccount in + seal.fulfill(storeAccount) + } }, - after(seconds: 30).then { - Promise(error: MASError.notSignedIn) - } + after(seconds: 30) + .then { + Promise(error: MASError.notSignedIn) + } ) } else { return .value(CKAccountStore.shared().primaryAccount) @@ -76,9 +78,10 @@ extension ISStoreAccount: StoreAccount { return race( signInPromise, - after(seconds: 30).then { - Promise(error: MASError.signInFailed(error: nil)) - } + after(seconds: 30) + .then { + Promise(error: MASError.signInFailed(error: nil)) + } ) } } diff --git a/Sources/mas/AppStore/PurchaseDownloadObserver.swift b/Sources/mas/AppStore/PurchaseDownloadObserver.swift index 343b49c..fd4add4 100644 --- a/Sources/mas/AppStore/PurchaseDownloadObserver.swift +++ b/Sources/mas/AppStore/PurchaseDownloadObserver.swift @@ -9,7 +9,8 @@ import CommerceKit import StoreFoundation -@objc class PurchaseDownloadObserver: NSObject, CKDownloadQueueObserver { +@objc +class PurchaseDownloadObserver: NSObject, CKDownloadQueueObserver { let purchase: SSPurchase var completionHandler: (() -> Void)? var errorHandler: ((MASError) -> Void)? diff --git a/Sources/mas/AppStore/SSPurchase.swift b/Sources/mas/AppStore/SSPurchase.swift index cabfa51..cf712cd 100644 --- a/Sources/mas/AppStore/SSPurchase.swift +++ b/Sources/mas/AppStore/SSPurchase.swift @@ -23,7 +23,6 @@ extension SSPurchase { if purchase { parameters["macappinstalledconfirmed"] = 1 parameters["pricingParameters"] = "STDQ" - } else { parameters["pricingParameters"] = "STDRDL" } @@ -63,19 +62,20 @@ extension SSPurchase { private func perform() -> Promise { Promise { seal in - CKPurchaseController.shared().perform(self, withOptions: 0) { purchase, _, error, response in - if let error { - seal.reject(MASError.purchaseFailed(error: error as NSError?)) - return - } + CKPurchaseController.shared() + .perform(self, withOptions: 0) { purchase, _, error, response in + if let error { + seal.reject(MASError.purchaseFailed(error: error as NSError?)) + return + } - guard response?.downloads.isEmpty == false, let purchase else { - seal.reject(MASError.noDownloads) - return - } + guard response?.downloads.isEmpty == false, let purchase else { + seal.reject(MASError.noDownloads) + return + } - seal.fulfill(purchase) - } + seal.fulfill(purchase) + } } .then { purchase in let observer = PurchaseDownloadObserver(purchase: purchase) diff --git a/Sources/mas/Commands/Open.swift b/Sources/mas/Commands/Open.swift index 4cc0c99..4ae4247 100644 --- a/Sources/mas/Commands/Open.swift +++ b/Sources/mas/Commands/Open.swift @@ -35,13 +35,11 @@ extension Mas { return } - guard let result = try storeSearch.lookup(appID: appID).wait() - else { + guard let result = try storeSearch.lookup(appID: appID).wait() else { throw MASError.noSearchResultsFound } - guard var url = URLComponents(string: result.trackViewUrl) - else { + guard var url = URLComponents(string: result.trackViewUrl) else { throw MASError.searchFailed } url.scheme = masScheme diff --git a/Sources/mas/Commands/Outdated.swift b/Sources/mas/Commands/Outdated.swift index 9c0b1be..2164a4b 100644 --- a/Sources/mas/Commands/Outdated.swift +++ b/Sources/mas/Commands/Outdated.swift @@ -32,7 +32,8 @@ extension Mas { appLibrary.installedApps.map { installedApp in firstly { storeSearch.lookup(appID: installedApp.itemIdentifier.appIDValue) - }.done { storeApp in + } + .done { storeApp in guard let storeApp else { if verbose { printWarning( diff --git a/Sources/mas/Commands/Upgrade.swift b/Sources/mas/Commands/Upgrade.swift index cfc7b66..a588af2 100644 --- a/Sources/mas/Commands/Upgrade.swift +++ b/Sources/mas/Commands/Upgrade.swift @@ -41,7 +41,8 @@ extension Mas { print("Upgrading \(apps.count) outdated application\(apps.count > 1 ? "s" : ""):") print( apps.map { "\($0.installedApp.appName) (\($0.installedApp.bundleVersion)) -> (\($0.storeApp.version))" } - .joined(separator: "\n")) + .joined(separator: "\n") + ) do { try downloadAll(apps.map(\.installedApp.itemIdentifier.appIDValue)).wait() @@ -71,7 +72,8 @@ extension Mas { // only upgrade apps whose local version differs from the store version firstly { storeSearch.lookup(appID: installedApp.itemIdentifier.appIDValue) - }.map { result -> (SoftwareProduct, SearchResult)? in + } + .map { result -> (SoftwareProduct, SearchResult)? in guard let storeApp = result, installedApp.isOutdatedWhenComparedTo(storeApp) else { return nil } diff --git a/Sources/mas/Commands/Vendor.swift b/Sources/mas/Commands/Vendor.swift index bdf13b1..7380ca0 100644 --- a/Sources/mas/Commands/Vendor.swift +++ b/Sources/mas/Commands/Vendor.swift @@ -26,13 +26,13 @@ extension Mas { func run(storeSearch: StoreSearch, openCommand: ExternalCommand) throws { do { - guard let result = try storeSearch.lookup(appID: appID).wait() - else { + guard let result = try storeSearch.lookup(appID: appID).wait() else { throw MASError.noSearchResultsFound } - guard let vendorWebsite = result.sellerUrl - else { throw MASError.noVendorWebsite } + guard let vendorWebsite = result.sellerUrl else { + throw MASError.noVendorWebsite + } do { try openCommand.run(arguments: vendorWebsite) diff --git a/Sources/mas/Controllers/MasAppLibrary.swift b/Sources/mas/Controllers/MasAppLibrary.swift index d842194..9325af4 100644 --- a/Sources/mas/Controllers/MasAppLibrary.swift +++ b/Sources/mas/Controllers/MasAppLibrary.swift @@ -14,9 +14,10 @@ class MasAppLibrary: AppLibrary { private let softwareMap: SoftwareMap /// Array of installed software products. - lazy var installedApps: [SoftwareProduct] = softwareMap.allSoftwareProducts().filter { product in - product.bundlePath.starts(with: "/Applications/") - } + lazy var installedApps: [SoftwareProduct] = softwareMap.allSoftwareProducts() + .filter { product in + product.bundlePath.starts(with: "/Applications/") + } /// Internal initializer for providing a mock software map. /// - Parameter softwareMap: SoftwareMap to use diff --git a/Sources/mas/Controllers/MasStoreSearch.swift b/Sources/mas/Controllers/MasStoreSearch.swift index 9e64b72..9680070 100644 --- a/Sources/mas/Controllers/MasStoreSearch.swift +++ b/Sources/mas/Controllers/MasStoreSearch.swift @@ -53,9 +53,11 @@ class MasStoreSearch: StoreSearch { // Combine the results, removing any duplicates. var seenAppIDs = Set() - return when(fulfilled: results).flatMapValues { $0 }.filterValues { result in - seenAppIDs.insert(result.trackId).inserted - } + return when(fulfilled: results) + .flatMapValues { $0 } + .filterValues { result in + seenAppIDs.insert(result.trackId).inserted + } } /// Looks up app details. @@ -75,14 +77,14 @@ class MasStoreSearch: StoreSearch { return .value(nil) } - guard let pageUrl = URL(string: result.trackViewUrl) - else { + guard let pageUrl = URL(string: result.trackViewUrl) else { return .value(result) } return firstly { self.scrapeAppStoreVersion(pageUrl) - }.map { pageVersion in + } + .map { pageVersion in guard let pageVersion, let searchVersion = Version(tolerant: result.version), pageVersion > searchVersion @@ -94,7 +96,8 @@ class MasStoreSearch: StoreSearch { var result = result result.version = pageVersion.description return result - }.recover { _ in + } + .recover { _ in // If we were unable to scrape the App Store page, assume compatibility. .value(result) } @@ -120,7 +123,8 @@ class MasStoreSearch: StoreSearch { private func scrapeAppStoreVersion(_ pageUrl: URL) -> Promise { firstly { networkManager.loadData(from: pageUrl) - }.map { data in + } + .map { data in guard let html = String(data: data, encoding: .utf8), let capture = MasStoreSearch.appVersionExpression.firstMatch(in: html)?.captures[0], let version = Version(tolerant: capture) diff --git a/Sources/mas/Formatters/Utilities.swift b/Sources/mas/Formatters/Utilities.swift index 3f41341..63e436c 100644 --- a/Sources/mas/Formatters/Utilities.swift +++ b/Sources/mas/Formatters/Utilities.swift @@ -18,7 +18,9 @@ private var standardError = FileHandle.standardError extension FileHandle: TextOutputStream { /// Appends the given string to the stream. public func write(_ string: String) { - guard let data = string.data(using: .utf8) else { return } + guard let data = string.data(using: .utf8) else { + return + } write(data) } } diff --git a/Tests/masTests/Controllers/StoreSearchMock.swift b/Tests/masTests/Controllers/StoreSearchMock.swift index f97a1d3..938c2b0 100644 --- a/Tests/masTests/Controllers/StoreSearchMock.swift +++ b/Tests/masTests/Controllers/StoreSearchMock.swift @@ -18,8 +18,7 @@ class StoreSearchMock: StoreSearch { } func lookup(appID: AppID) -> Promise { - guard let result = apps[appID] - else { + guard let result = apps[appID] else { return Promise(error: MASError.noSearchResultsFound) } diff --git a/Tests/masTests/Network/NetworkSessionMockFromFile.swift b/Tests/masTests/Network/NetworkSessionMockFromFile.swift index 07b25aa..2ab1fa1 100644 --- a/Tests/masTests/Network/NetworkSessionMockFromFile.swift +++ b/Tests/masTests/Network/NetworkSessionMockFromFile.swift @@ -22,8 +22,9 @@ class NetworkSessionMockFromFile: NetworkSessionMock { } override func loadData(from _: URL) -> Promise { - guard let fileURL = Bundle.url(for: responseFile) - else { fatalError("Unable to load file \(responseFile)") } + guard let fileURL = Bundle.url(for: responseFile) else { + fatalError("Unable to load file \(responseFile)") + } do { return .value(try Data(contentsOf: fileURL, options: .mappedIfSafe))