Standardize names of variables & parameters relating to AppIDs.

Resolve #478

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
This commit is contained in:
Ross Goldberg 2024-10-14 01:50:10 -04:00
parent 39f77c01a9
commit 006273bb81
No known key found for this signature in database
20 changed files with 70 additions and 68 deletions

View file

@ -35,7 +35,7 @@ func downloadAll(_ appIDs: [AppID], purchase: Bool = false) -> Promise<Void> {
}
private func downloadWithRetries(_ appID: AppID, purchase: Bool = false, attempts: Int = 3) -> Promise<Void> {
SSPurchase().perform(adamId: appID, purchase: purchase)
SSPurchase().perform(appID: appID, purchase: purchase)
.recover { error -> Promise<Void> in
guard attempts > 1 else {
throw error

View file

@ -11,11 +11,11 @@ import PromiseKit
import StoreFoundation
extension SSPurchase {
func perform(adamId: AppID, purchase: Bool) -> Promise<Void> {
func perform(appID: AppID, purchase: Bool) -> Promise<Void> {
var parameters: [String: Any] = [
"productType": "C",
"price": 0,
"salableAdamId": adamId,
"salableAdamId": appID,
"pg": "default",
"appExtVrsId": 0,
]
@ -34,7 +34,7 @@ extension SSPurchase {
}
.joined(separator: "&")
itemIdentifier = adamId
itemIdentifier = appID
// Not sure if this is needed
if purchase {
@ -43,7 +43,7 @@ extension SSPurchase {
downloadMetadata = SSDownloadMetadata()
downloadMetadata.kind = "software"
downloadMetadata.itemIdentifier = adamId
downloadMetadata.itemIdentifier = appID
// Monterey obscures the user's App Store account, but allows
// redownloads without passing any account IDs to SSPurchase.

View file

@ -17,7 +17,7 @@ extension Mas {
)
@Argument(help: "ID of app to show on MAS Preview")
var appId: AppID
var appID: AppID
/// Runs the command.
func run() throws {
@ -26,7 +26,7 @@ extension Mas {
func run(storeSearch: StoreSearch, openCommand: ExternalCommand) throws {
do {
guard let result = try storeSearch.lookup(app: appId).wait() else {
guard let result = try storeSearch.lookup(appID: appID).wait() else {
throw MASError.noSearchResultsFound
}

View file

@ -18,7 +18,7 @@ extension Mas {
)
@Argument(help: "ID of app to show info")
var appId: AppID
var appID: AppID
/// Runs the command.
func run() throws {
@ -27,7 +27,7 @@ extension Mas {
func run(storeSearch: StoreSearch) throws {
do {
guard let result = try storeSearch.lookup(app: appId).wait() else {
guard let result = try storeSearch.lookup(appID: appID).wait() else {
throw MASError.noSearchResultsFound
}

View file

@ -19,7 +19,7 @@ extension Mas {
@Flag(help: "force reinstall")
var force = false
@Argument(help: "app ID(s) to install")
var appIds: [AppID]
var appIDs: [AppID]
/// Runs the command.
func run() throws {
@ -28,8 +28,8 @@ extension Mas {
func run(appLibrary: AppLibrary) throws {
// Try to download applications with given identifiers and collect results
let appIds = appIds.filter { appId in
if let product = appLibrary.installedApp(forId: appId), !force {
let appIDs = appIDs.filter { appID in
if let product = appLibrary.installedApp(withAppID: appID), !force {
printWarning("\(product.appName) is already installed")
return false
}
@ -38,7 +38,7 @@ extension Mas {
}
do {
try downloadAll(appIds).wait()
try downloadAll(appIDs).wait()
} catch {
throw error as? MASError ?? .downloadFailed(error: error as NSError)
}

View file

@ -28,7 +28,7 @@ extension Mas {
}
func run(appLibrary: AppLibrary, storeSearch: StoreSearch) throws {
var appId: AppID?
var appID: AppID?
do {
let results = try storeSearch.search(for: appName).wait()
@ -37,28 +37,30 @@ extension Mas {
throw MASError.noSearchResultsFound
}
appId = result.trackId
appID = result.trackId
} catch {
throw error as? MASError ?? .searchFailed
}
guard let identifier = appId else { fatalError() }
guard let appID else {
fatalError()
}
try install(identifier, appLibrary: appLibrary)
try install(appID: appID, appLibrary: appLibrary)
}
/// Installs an app.
///
/// - Parameters:
/// - appId: App identifier
/// - appID: App identifier
/// - appLibrary: Library of installed apps
fileprivate func install(_ appId: AppID, appLibrary: AppLibrary) throws {
fileprivate func install(appID: AppID, appLibrary: AppLibrary) throws {
// Try to download applications with given identifiers and collect results
if let product = appLibrary.installedApp(forId: appId), !force {
if let product = appLibrary.installedApp(withAppID: appID), !force {
printWarning("\(product.appName) is already installed")
} else {
do {
try downloadAll([appId]).wait()
try downloadAll([appID]).wait()
} catch {
throw error as? MASError ?? .downloadFailed(error: error as NSError)
}

View file

@ -20,7 +20,7 @@ extension Mas {
)
@Argument(help: "the app ID")
var appId: AppID?
var appID: AppID?
/// Runs the command.
func run() throws {
@ -29,13 +29,13 @@ extension Mas {
func run(storeSearch: StoreSearch, openCommand: ExternalCommand) throws {
do {
guard let appId else {
guard let appID else {
// If no app ID is given, just open the MAS GUI app
try openCommand.run(arguments: masScheme + "://")
return
}
guard let result = try storeSearch.lookup(app: appId).wait()
guard let result = try storeSearch.lookup(appID: appID).wait()
else {
throw MASError.noSearchResultsFound
}

View file

@ -33,7 +33,7 @@ extension Mas {
fulfilled:
appLibrary.installedApps.map { installedApp in
firstly {
storeSearch.lookup(app: installedApp.itemIdentifier.uint64Value)
storeSearch.lookup(appID: installedApp.itemIdentifier.uint64Value)
}.done { storeApp in
guard let storeApp else {
if verbose {

View file

@ -16,7 +16,7 @@ extension Mas {
)
@Argument(help: "app ID(s) to install")
var appIds: [AppID]
var appIDs: [AppID]
/// Runs the command.
func run() throws {
@ -25,8 +25,8 @@ extension Mas {
func run(appLibrary: AppLibrary) throws {
// Try to download applications with given identifiers and collect results
let appIds = appIds.filter { appId in
if let product = appLibrary.installedApp(forId: appId) {
let appIDs = appIDs.filter { appID in
if let product = appLibrary.installedApp(withAppID: appID) {
printWarning("\(product.appName) has already been purchased.")
return false
}
@ -35,7 +35,7 @@ extension Mas {
}
do {
try downloadAll(appIds, purchase: true).wait()
try downloadAll(appIDs, purchase: true).wait()
} catch {
throw error as? MASError ?? .downloadFailed(error: error as NSError)
}

View file

@ -21,7 +21,7 @@ extension Mas {
@Flag(help: "dry run")
var dryRun = false
@Argument(help: "ID of app to uninstall")
var appId: AppID
var appID: AppID
/// Runs the uninstall command.
func run() throws {
@ -29,7 +29,7 @@ extension Mas {
}
func run(appLibrary: AppLibrary) throws {
guard let product = appLibrary.installedApp(forId: appId) else {
guard let product = appLibrary.installedApp(withAppID: appID) else {
throw MASError.notInstalled
}

View file

@ -18,7 +18,7 @@ extension Mas {
)
@Argument(help: "app(s) to upgrade")
var appIds: [String] = []
var appIDs: [String] = []
/// Runs the command.
func run() throws {
@ -55,12 +55,12 @@ extension Mas {
storeSearch: StoreSearch
) throws -> [(SoftwareProduct, SearchResult)] {
let apps: [SoftwareProduct] =
appIds.isEmpty
appIDs.isEmpty
? appLibrary.installedApps
: appIds.compactMap {
if let appId = AppID($0) {
: appIDs.compactMap {
if let appID = AppID($0) {
// if argument an AppID, lookup app by id using argument
return appLibrary.installedApp(forId: appId)
return appLibrary.installedApp(withAppID: appID)
} else {
// if argument not an AppID, lookup app by name using argument
return appLibrary.installedApp(named: $0)
@ -70,7 +70,7 @@ extension Mas {
let promises = apps.map { installedApp in
// only upgrade apps whose local version differs from the store version
firstly {
storeSearch.lookup(app: installedApp.itemIdentifier.uint64Value)
storeSearch.lookup(appID: installedApp.itemIdentifier.uint64Value)
}.map { result -> (SoftwareProduct, SearchResult)? in
guard let storeApp = result, installedApp.isOutdatedWhenComparedTo(storeApp) else {
return nil

View file

@ -17,7 +17,7 @@ extension Mas {
)
@Argument(help: "the app ID to show the vendor's website")
var appId: AppID
var appID: AppID
/// Runs the command.
func run() throws {
@ -26,7 +26,7 @@ extension Mas {
func run(storeSearch: StoreSearch, openCommand: ExternalCommand) throws {
do {
guard let result = try storeSearch.lookup(app: appId).wait()
guard let result = try storeSearch.lookup(appID: appID).wait()
else {
throw MASError.noSearchResultsFound
}

View file

@ -15,9 +15,9 @@ protocol AppLibrary {
/// Finds an app by ID.
///
/// - Parameter forId: MAS ID for app.
/// - Parameter withAppID: MAS ID for app.
/// - Returns: Software Product of app if found; nil otherwise.
func installedApp(forId: AppID) -> SoftwareProduct?
func installedApp(withAppID appID: AppID) -> SoftwareProduct?
/// Uninstalls an app.
///
@ -30,11 +30,11 @@ protocol AppLibrary {
extension AppLibrary {
/// Finds an app by ID.
///
/// - Parameter forId: MAS ID for app.
/// - Parameter withAppID: MAS ID for app.
/// - Returns: Software Product of app if found; nil otherwise.
func installedApp(forId identifier: AppID) -> SoftwareProduct? {
let appId = NSNumber(value: identifier)
return installedApps.first { $0.itemIdentifier == appId }
func installedApp(withAppID appID: AppID) -> SoftwareProduct? {
let appID = NSNumber(value: appID)
return installedApps.first { $0.itemIdentifier == appID }
}
/// Finds an app by name.

View file

@ -61,12 +61,12 @@ class MasStoreSearch: StoreSearch {
/// Looks up app details.
///
/// - Parameter appId: MAS ID of app
/// - Parameter appID: MAS ID of app
/// - Returns: A Promise for the search result record of app, or nil if no apps match the ID,
/// or an Error if there is a problem with the network request.
func lookup(app appId: AppID) -> Promise<SearchResult?> {
guard let url = lookupURL(forApp: appId, inCountry: country) else {
fatalError("Failed to build URL for \(appId)")
func lookup(appID: AppID) -> Promise<SearchResult?> {
guard let url = lookupURL(forAppID: appID, inCountry: country) else {
fatalError("Failed to build URL for \(appID)")
}
return firstly {
loadSearchResults(url)

View file

@ -11,7 +11,7 @@ import PromiseKit
/// Protocol for searching the MAS catalog.
protocol StoreSearch {
func lookup(app appId: AppID) -> Promise<SearchResult?>
func lookup(appID: AppID) -> Promise<SearchResult?>
func search(for appName: String) -> Promise<[SearchResult]>
}
@ -47,15 +47,15 @@ extension StoreSearch {
/// Builds the lookup URL for an app.
///
/// - Parameter appId: MAS app identifier.
/// - Returns: URL for the lookup service or nil if appId can't be encoded.
func lookupURL(forApp appId: AppID, inCountry country: String?) -> URL? {
/// - Parameter appID: MAS app identifier.
/// - Returns: URL for the lookup service or nil if appID can't be encoded.
func lookupURL(forAppID appID: AppID, inCountry country: String?) -> URL? {
guard var components = URLComponents(string: "https://itunes.apple.com/lookup") else {
return nil
}
components.queryItems = [
URLQueryItem(name: "id", value: "\(appId)"),
URLQueryItem(name: "id", value: "\(appID)"),
URLQueryItem(name: "entity", value: "desktopSoftware"),
]

View file

@ -24,12 +24,12 @@ enum AppListFormatter {
var output = ""
for product in products {
let appId = product.itemIdentifier.stringValue
let appID = product.itemIdentifier.stringValue
.padding(toLength: idColumnMinWidth, withPad: " ", startingAt: 0)
let appName = product.appNameOrBundleIdentifier.padding(toLength: maxLength, withPad: " ", startingAt: 0)
let version = product.bundleVersion
output += "\(appId) \(appName) (\(version))\n"
output += "\(appID) \(appName) (\(version))\n"
}
return output.trimmingCharacters(in: .newlines)

View file

@ -20,15 +20,15 @@ enum SearchResultFormatter {
var output = ""
for result in results {
let appId = result.trackId
let appID = result.trackId
let appName = result.trackName.padding(toLength: maxLength, withPad: " ", startingAt: 0)
let version = result.version
let price = result.price ?? 0.0
if includePrice {
output += String(format: "%12lu %@ $%5.2f (%@)\n", appId, appName, price, version)
output += String(format: "%12lu %@ $%5.2f (%@)\n", appID, appName, price, version)
} else {
output += String(format: "%12lu %@ (%@)\n", appId, appName, version)
output += String(format: "%12lu %@ (%@)\n", appID, appName, version)
}
}

View file

@ -18,18 +18,18 @@ public class UninstallSpec: QuickSpec {
Mas.initialize()
}
describe("uninstall command") {
let appId: AppID = 12345
let appID: AppID = 12345
let app = SoftwareProductMock(
appName: "Some App",
bundleIdentifier: "com.some.app",
bundlePath: "/tmp/Some.app",
bundleVersion: "1.0",
itemIdentifier: NSNumber(value: appId)
itemIdentifier: NSNumber(value: appID)
)
let mockLibrary = AppLibraryMock()
context("dry run") {
let uninstall = try! Mas.Uninstall.parse(["--dry-run", String(appId)])
let uninstall = try! Mas.Uninstall.parse(["--dry-run", String(appID)])
beforeEach {
mockLibrary.reset()
@ -49,7 +49,7 @@ public class UninstallSpec: QuickSpec {
}
}
context("wet run") {
let uninstall = try! Mas.Uninstall.parse([String(appId)])
let uninstall = try! Mas.Uninstall.parse([String(appID)])
beforeEach {
mockLibrary.reset()

View file

@ -54,13 +54,13 @@ public class MasStoreSearchSpec: QuickSpec {
context("when lookup used") {
it("can find slack") {
let appId: AppID = 803_453_959
let appID: AppID = 803_453_959
let networkSession = NetworkSessionMockFromFile(responseFile: "lookup/slack.json")
let storeSearch = MasStoreSearch(networkManager: NetworkManager(session: networkSession))
var lookup: SearchResult?
do {
lookup = try storeSearch.lookup(app: appId).wait()
lookup = try storeSearch.lookup(appID: appID).wait()
} catch {
let maserror = error as! MASError
if case .jsonParsing(let nserror) = maserror {
@ -70,7 +70,7 @@ public class MasStoreSearchSpec: QuickSpec {
guard let result = lookup else { fatalError("lookup result was nil") }
expect(result.trackId) == appId
expect(result.trackId) == appID
expect(result.bundleId) == "com.tinyspeck.slackmacgap"
expect(result.price) == 0
expect(result.sellerName) == "Slack Technologies, Inc."

View file

@ -19,8 +19,8 @@ class StoreSearchMock: StoreSearch {
return .value(results)
}
func lookup(app appId: AppID) -> Promise<SearchResult?> {
guard let result = apps[appId]
func lookup(appID: AppID) -> Promise<SearchResult?> {
guard let result = apps[appID]
else {
return Promise(error: MASError.noSearchResultsFound)
}