Skip to content

Commit

Permalink
Use async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny committed Jul 4, 2024
1 parent ddeab5d commit 8a4e922
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Source/swiftlint/Commands/Version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ArgumentParser
import SwiftLintFramework

extension SwiftLint {
struct Version: ParsableCommand {
struct Version: AsyncParsableCommand {
@Flag(help: "Display full version info.")
var verbose = false
@Flag(help: "Check whether a later version of SwiftLint is available after processing all files.")
Expand All @@ -12,15 +12,15 @@ extension SwiftLint {

static var value: String { SwiftLintFramework.Version.current.value }

func run() throws {
func run() async {
if verbose, let buildID = ExecutableInfo.buildID {
print("Version:", Self.value)
print("Build ID:", buildID)
} else {
print(Self.value)
}
if checkForUpdates {
UpdateChecker.checkForUpdates()
await UpdateChecker.checkForUpdates()
}
ExitHelper.successfullyExit()
}
Expand Down
2 changes: 1 addition & 1 deletion Source/swiftlint/Helpers/LintOrAnalyzeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct LintOrAnalyzeCommand {
try postProcessViolations(files: files, builder: builder)
}
if options.checkForUpdates || builder.configuration.checkForUpdates {
UpdateChecker.checkForUpdates()
await UpdateChecker.checkForUpdates()
}
}

Expand Down
31 changes: 8 additions & 23 deletions Source/swiftlint/Helpers/UpdateChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import SwiftLintFramework

enum UpdateChecker {
static func checkForUpdates() {
static func checkForUpdates() async {
guard let url = URL(string: "https://api.github.com/repos/realm/SwiftLint/releases/latest"),
let data = sendSynchronousRequest(to: url),
let latestVersionNumber = parseVersionNumber(data)
else {
let data = try? await sendRequest(to: url),
let latestVersionNumber = parseVersionNumber(data) else {
print("Could not check latest SwiftLint version")
return
}
Expand All @@ -30,32 +26,21 @@ enum UpdateChecker {
if latestVersion > SwiftLintFramework.Version.current {
print("A new version of SwiftLint is available: \(latestVersionNumber)")
} else {
print("Your version of SwiftLint is up to date")
print("Your version of SwiftLint is up to date.")
}
}

private static func parseVersionNumber(_ data: Data) -> String? {
guard let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [AnyHashable: Any],
let tagName = jsonObject["tag_name"] as? String else {
guard let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [AnyHashable: Any] else {
return nil
}
return tagName
return jsonObject["tag_name"] as? String
}

private static func sendSynchronousRequest(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
}
}

0 comments on commit 8a4e922

Please sign in to comment.