Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use async/await for URL session #5814

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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