Skip to content

Commit c4bae62

Browse files
committed
refactor: Improve async update checking and command escaping in services
1 parent ade5945 commit c4bae62

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

MyMacCleaner/Core/Services/AppUpdateChecker.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,29 @@ actor AppUpdateChecker {
8080
func checkUpdates(for appURLs: [URL], progress: @escaping (Double) -> Void) async -> [AppUpdate] {
8181
var updates: [AppUpdate] = []
8282
let total = appURLs.count
83-
var completed = 0
8483

85-
await withTaskGroup(of: AppUpdate?.self) { group in
84+
let results = await withTaskGroup(of: AppUpdate?.self, returning: [AppUpdate?].self) { group in
8685
for appURL in appURLs {
8786
group.addTask {
8887
await self.checkSparkleUpdate(for: appURL)
8988
}
9089
}
9190

91+
var collected: [AppUpdate?] = []
9292
for await update in group {
93-
completed += 1
93+
collected.append(update)
94+
let currentProgress = Double(collected.count) / Double(total)
9495
await MainActor.run {
95-
progress(Double(completed) / Double(total))
96-
}
97-
if let update = update, update.hasUpdate {
98-
updates.append(update)
96+
progress(currentProgress)
9997
}
10098
}
99+
return collected
100+
}
101+
102+
for result in results {
103+
if let update = result, update.hasUpdate {
104+
updates.append(update)
105+
}
101106
}
102107

103108
return updates

MyMacCleaner/Core/Services/AuthorizationService.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ final class AuthorizationService {
6767
// MARK: - Private Methods
6868

6969
private func runWithAdminPrivileges(_ command: String) async -> Bool {
70+
// Escape command before entering async block to avoid capturing self
71+
let escapedCommand = escapeForAppleScript(command)
72+
7073
return await withCheckedContinuation { continuation in
7174
DispatchQueue.global(qos: .userInitiated).async {
7275
let script = """
73-
do shell script "\(self.escapeForAppleScript(command))" with administrator privileges
76+
do shell script "\(escapedCommand)" with administrator privileges
7477
"""
7578

7679
var error: NSDictionary?
@@ -85,10 +88,13 @@ final class AuthorizationService {
8588
}
8689

8790
private func runWithAdminPrivilegesAndCapture(_ command: String) async -> String? {
91+
// Escape command before entering async block to avoid capturing self
92+
let escapedCommand = escapeForAppleScript(command)
93+
8894
return await withCheckedContinuation { continuation in
8995
DispatchQueue.global(qos: .userInitiated).async {
9096
let script = """
91-
do shell script "\(self.escapeForAppleScript(command))" with administrator privileges
97+
do shell script "\(escapedCommand)" with administrator privileges
9298
"""
9399

94100
var error: NSDictionary?

MyMacCleaner/Core/Services/FileScanner.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ actor FileScanner {
208208
return items
209209
}
210210

211-
for case let fileURL as URL in enumerator {
211+
while let fileURL = enumerator.nextObject() as? URL {
212212
// Check depth
213213
let depth = fileURL.pathComponents.count - url.pathComponents.count
214214
if depth > maxDepth {
@@ -295,7 +295,7 @@ actor FileScanner {
295295
return 0
296296
}
297297

298-
for case let fileURL as URL in enumerator {
298+
while let fileURL = enumerator.nextObject() as? URL {
299299
guard let resourceValues = try? fileURL.resourceValues(forKeys: resourceKeys),
300300
resourceValues.isDirectory == false else {
301301
continue

MyMacCleaner/Core/Services/StartupItemsService.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ actor StartupItemsService {
188188
}
189189
}
190190

191-
nonisolated private static func parseBTMOutput(_ output: String) -> [StartupItem] {
191+
private static nonisolated func parseBTMOutput(_ output: String) -> [StartupItem] {
192192
var items: [StartupItem] = []
193193
let lines = output.components(separatedBy: "\n")
194194

@@ -505,7 +505,7 @@ actor StartupItemsService {
505505

506506
let name = parts[0]
507507
let path = parts[1]
508-
// isHidden is parsed but not used currently (parts[2])
508+
// parts[2] contains isHidden but not used currently
509509

510510
let item = StartupItem(
511511
id: "loginitem:\(path)",
@@ -706,7 +706,7 @@ actor StartupItemsService {
706706

707707
// MARK: - Code Signing
708708

709-
nonisolated private static func getCodeSigningTeam(for path: String) -> String? {
709+
private static nonisolated func getCodeSigningTeam(for path: String) -> String? {
710710
let process = Process()
711711
process.executableURL = URL(fileURLWithPath: "/usr/bin/codesign")
712712
process.arguments = ["-dv", "--verbose=2", path]

0 commit comments

Comments
 (0)