Skip to content

Commit

Permalink
Use async/await for URL session
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny committed Sep 29, 2024
1 parent 1ec3fdc commit 60c3b14
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/LintOrAnalyzeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ package struct LintOrAnalyzeCommand {
try postProcessViolations(files: files, builder: builder)
}
if options.checkForUpdates || builder.configuration.checkForUpdates {
UpdateChecker.checkForUpdates()
await UpdateChecker.checkForUpdates()
}
}

Expand Down
18 changes: 4 additions & 14 deletions Source/SwiftLintFramework/UpdateChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import FoundationNetworking
#endif

package enum UpdateChecker {
package static func checkForUpdates() {
package static func checkForUpdates() async {
guard let url = URL(string: "https://api.github.com/repos/realm/SwiftLint/releases/latest"),
let data = sendRequest(to: url),
let data = try? await sendRequest(to: url),
let latestVersionNumber = parseVersionNumber(data) else {
print("Could not check latest SwiftLint version")
return
Expand All @@ -39,20 +39,10 @@ package enum UpdateChecker {
return jsonObject["tag_name"] as? String
}

private static func sendRequest(to url: URL) -> Data? {
private static func sendRequest(to url: URL) async throws -> Data {
var request = URLRequest(url: url)
request.setValue("SwiftLint", forHTTPHeaderField: "User-Agent")
request.setValue("application/vnd.github.v3+json", forHTTPHeaderField: "Accept")
let semaphore = DispatchSemaphore(value: 0)
var result: Data?

let task = URLSession.shared.dataTask(with: request) { data, _, _ in
result = data
semaphore.signal()
}
task.resume()

semaphore.wait()
return result
return try await URLSession.shared.data(for: request).0
}
}
2 changes: 1 addition & 1 deletion Source/swiftlint/Commands/Version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension SwiftLint {
print(Self.value)
}
if checkForUpdates {
UpdateChecker.checkForUpdates()
await UpdateChecker.checkForUpdates()
}
ExitHelper.successfullyExit()
}
Expand Down

0 comments on commit 60c3b14

Please sign in to comment.