Skip to content

Commit 72d71ef

Browse files
authored
Merge pull request #879 from rgoldberg/878-cleanup
Swift cleanup
2 parents c60969f + e8682c3 commit 72d71ef

35 files changed

Lines changed: 83 additions & 97 deletions

Sources/mas/AppStore/PurchaseDownloadObserver.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
internal import CommerceKit
1010

11-
private let downloadingPhaseType = 0 as Int64
12-
private let installingPhaseType = 1 as Int64
13-
private let initialPhaseType = 4 as Int64
14-
private let downloadedPhaseType = 5 as Int64
11+
private var downloadingPhaseType: Int64 { 0 }
12+
private var installingPhaseType: Int64 { 1 }
13+
private var initialPhaseType: Int64 { 4 }
14+
private var downloadedPhaseType: Int64 { 5 }
1515

1616
class PurchaseDownloadObserver: CKDownloadQueueObserver {
1717
private let appID: AppID

Sources/mas/Commands/Account.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//
88

99
internal import ArgumentParser
10-
private import StoreFoundation
1110

1211
extension MAS {
1312
struct Account: AsyncParsableCommand {

Sources/mas/Commands/Config.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
internal import ArgumentParser
1010
private import Foundation
1111

12-
private let unknown = "unknown"
12+
private var unknown: String { "unknown" }
13+
private var sysCtlByName: String { "sysctlbyname" }
1314

1415
extension MAS {
1516
/// Displays mas config & related system info.
@@ -22,7 +23,7 @@ extension MAS {
2223
var markdown = false
2324

2425
/// Runs the command.
25-
func run() async throws {
26+
func run() async {
2627
if markdown {
2728
printInfo("```text")
2829
}
@@ -53,13 +54,13 @@ extension MAS {
5354
private func configStringValue(_ name: String) -> String {
5455
var size = 0
5556
guard sysctlbyname(name, nil, &size, nil, 0) == 0 else {
56-
perror("sysctlbyname")
57+
perror(sysCtlByName)
5758
return unknown
5859
}
5960

6061
var buffer = [CChar](repeating: 0, count: size)
6162
guard sysctlbyname(name, &buffer, &size, nil, 0) == 0 else {
62-
perror("sysctlbyname")
63+
perror(sysCtlByName)
6364
return unknown
6465
}
6566

Sources/mas/Commands/List.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ extension MAS {
1616
)
1717

1818
/// Runs the command.
19-
func run() async throws {
20-
try run(installedApps: await installedApps)
19+
func run() async {
20+
run(installedApps: await installedApps)
2121
}
2222

23-
func run(installedApps: [InstalledApp]) throws {
23+
func run(installedApps: [InstalledApp]) {
2424
if installedApps.isEmpty {
2525
printError(
2626
"""

Sources/mas/Commands/Open.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
private import AppKit
1010
internal import ArgumentParser
1111

12-
private let masScheme = "macappstore"
12+
private var masScheme: String { "macappstore" }
1313

1414
extension MAS {
1515
/// Opens app page in MAS app. Uses the iTunes Lookup API:

Sources/mas/Commands/Reset.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension MAS {
2020
var debug = false
2121

2222
/// Runs the command.
23-
func run() throws {
23+
func run() {
2424
// The "Reset Application" command in the Mac App Store debug menu performs
2525
// the following steps
2626
//

Sources/mas/Commands/SignOut.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension MAS {
1717
)
1818

1919
/// Runs the command.
20-
func run() throws {
20+
func run() {
2121
ISServiceProxy.genericShared().accountService.signOut()
2222
}
2323
}

Sources/mas/Commands/Uninstall.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private func chown(paths: [String]) throws -> [String: (uid_t, gid_t)] {
8181
throw MASError.runtimeError("Failed to get original gid")
8282
}
8383

84-
let ownerIDsByPath = try paths.reduce(into: [String: (uid_t, gid_t)]()) { dict, path in
84+
let ownerIDsByPath = try paths.reduce(into: [:]) { dict, path in
8585
dict[path] = try getOwnerAndGroupOfItem(atPath: path)
8686
}
8787

Sources/mas/Commands/Version.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension MAS {
1616
)
1717

1818
/// Runs the command.
19-
func run() throws {
19+
func run() {
2020
printInfo(Package.version)
2121
}
2222
}

Sources/mas/Controllers/ITunesSearchAppStoreSearcher.swift

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ struct ITunesSearchAppStoreSearcher: AppStoreSearcher {
3434
/// - Throws: A `MASError.unknownAppID(appID)` if `appID` is invalid.
3535
/// Some other `Error` if any other problem occurs.
3636
func lookup(appID: AppID, inRegion region: ISORegion?) async throws -> SearchResult {
37-
guard let url = lookupURL(forAppID: appID, inRegion: region) else {
38-
fatalError("Failed to build URL for \(appID)")
39-
}
40-
let results = try await loadSearchResults(url)
37+
let results = try await getSearchResults(from: try lookupURL(forAppID: appID, inRegion: region))
4138
guard let result = results.first else {
4239
throw MASError.unknownAppID(appID)
4340
}
@@ -62,10 +59,9 @@ struct ITunesSearchAppStoreSearcher: AppStoreSearcher {
6259

6360
var appSet = Set<SearchResult>()
6461
for entity in entities {
65-
guard let url = searchURL(for: searchTerm, inRegion: region, ofEntity: entity) else {
66-
fatalError("Failed to build URL for \(searchTerm)")
67-
}
68-
appSet.formUnion(try await loadSearchResults(url))
62+
appSet.formUnion(
63+
try await getSearchResults(from: try searchURL(for: searchTerm, inRegion: region, ofEntity: entity))
64+
)
6965
}
7066

7167
return Array(appSet)
@@ -77,13 +73,14 @@ struct ITunesSearchAppStoreSearcher: AppStoreSearcher {
7773
/// - appID: App ID.
7874
/// - region: The `ISORegion` of the storefront in which to lookup apps.
7975
/// - entity: OS platform of apps for which to search.
80-
/// - Returns: URL for the lookup service or nil if appID can't be encoded.
76+
/// - Returns: URL for the lookup service.
77+
/// - Throws: An `MASError.urlParsing` if `appID` can't be encoded.
8178
private func lookupURL(
8279
forAppID appID: AppID,
8380
inRegion region: ISORegion?,
8481
ofEntity entity: Entity = .desktopSoftware
85-
) -> URL? {
86-
url("lookup", URLQueryItem(name: "id", value: String(appID)), inRegion: region, ofEntity: entity)
82+
) throws -> URL {
83+
try url("lookup", URLQueryItem(name: "id", value: String(appID)), inRegion: region, ofEntity: entity)
8784
}
8885

8986
/// Builds the search URL for an app.
@@ -92,23 +89,25 @@ struct ITunesSearchAppStoreSearcher: AppStoreSearcher {
9289
/// - searchTerm: term for which to search in MAS.
9390
/// - region: The `ISORegion` of the storefront in which to lookup apps.
9491
/// - entity: OS platform of apps for which to search.
95-
/// - Returns: URL for the search service or nil if searchTerm can't be encoded.
92+
/// - Returns: URL for the search service.
93+
/// - Throws: An `MASError.urlParsing` if `searchTerm` can't be encoded.
9694
private func searchURL(
9795
for searchTerm: String,
9896
inRegion region: ISORegion?,
9997
ofEntity entity: Entity = .desktopSoftware
100-
) -> URL? {
101-
url("search", URLQueryItem(name: "term", value: searchTerm), inRegion: region, ofEntity: entity)
98+
) throws -> URL {
99+
try url("search", URLQueryItem(name: "term", value: searchTerm), inRegion: region, ofEntity: entity)
102100
}
103101

104102
private func url(
105103
_ action: String,
106104
_ queryItem: URLQueryItem,
107105
inRegion region: ISORegion?,
108106
ofEntity entity: Entity = .desktopSoftware
109-
) -> URL? {
110-
guard var components = URLComponents(string: "https://itunes.apple.com/\(action)") else {
111-
return nil
107+
) throws -> URL {
108+
let urlBase = "https://itunes.apple.com/\(action)"
109+
guard var urlComponents = URLComponents(string: urlBase) else {
110+
throw MASError.urlParsing(urlBase)
112111
}
113112

114113
var queryItems = [
@@ -122,12 +121,15 @@ struct ITunesSearchAppStoreSearcher: AppStoreSearcher {
122121

123122
queryItems.append(queryItem)
124123

125-
components.queryItems = queryItems
124+
urlComponents.queryItems = queryItems
126125

127-
return components.url
126+
guard let url = urlComponents.url else {
127+
throw MASError.urlParsing("\(urlBase)?\(queryItems.map(\.description).joined(separator: "&"))")
128+
}
129+
return url
128130
}
129131

130-
private func loadSearchResults(_ url: URL) async throws -> [SearchResult] {
132+
private func getSearchResults(from url: URL) async throws -> [SearchResult] {
131133
let (data, _) = try await networkSession.data(from: url)
132134
do {
133135
return try JSONDecoder().decode(SearchResultList.self, from: data).results

0 commit comments

Comments
 (0)