Skip to content

Commit

Permalink
Use async/await for URL session (#5814)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Sep 29, 2024
1 parent 1ec3fdc commit ab699f9
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 18 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
2 changes: 0 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ jobs:
strategy:
maxParallel: 10
matrix:
'Swift 5.10.1':
image: swift:5.10.1-noble
'Swift 6':
image: swift:6.0-noble
container: $[ variables['image'] ]
Expand Down

0 comments on commit ab699f9

Please sign in to comment.