From 84e8587831255dd4a30a8324edde892cad1c071a Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Mon, 8 Apr 2024 22:31:55 -0400 Subject: [PATCH] Refactor install and update checks --- .../WhiskyWine/WhiskyWineInstaller.swift | 56 ++++++++++--------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/WhiskyKit/Sources/WhiskyKit/WhiskyWine/WhiskyWineInstaller.swift b/WhiskyKit/Sources/WhiskyKit/WhiskyWine/WhiskyWineInstaller.swift index 1a4f44f6..0ccdd344 100644 --- a/WhiskyKit/Sources/WhiskyKit/WhiskyWine/WhiskyWineInstaller.swift +++ b/WhiskyKit/Sources/WhiskyKit/WhiskyWine/WhiskyWineInstaller.swift @@ -32,7 +32,7 @@ public class WhiskyWineInstaller { public static let binFolder: URL = libraryFolder.appending(path: "Wine").appending(path: "bin") public static func isWhiskyWineInstalled() -> Bool { - return FileManager.default.fileExists(atPath: libraryFolder.path) + return whiskyWineVersion() != nil } public static func install(from: URL) { @@ -61,32 +61,38 @@ public class WhiskyWineInstaller { } public static func shouldUpdateWhiskyWine() async -> (Bool, SemanticVersion) { - if let localVersion = whiskyWineVersion() { - let versionPlistURL = "https://data.getwhisky.app/Wine/WhiskyWineVersion.plist" - - if let remoteUrl = URL(string: versionPlistURL) { - return await withCheckedContinuation { continuation in - URLSession.shared.dataTask(with: URLRequest(url: remoteUrl)) { data, _, error in - do { - if error == nil, let data = data { - let decoder = PropertyListDecoder() - let remoteInfo = try decoder.decode(WhiskyWineVersion.self, from: data) - let remoteVersion = remoteInfo.version - - let isRemoteNewer = remoteVersion > localVersion - print(isRemoteNewer) - continuation.resume(returning: (isRemoteNewer, remoteVersion)) - return - } - if let error = error { - print(error) - } - } catch { + let versionPlistURL = "https://data.getwhisky.app/Wine/WhiskyWineVersion.plist" + let localVersion = whiskyWineVersion() + + var remoteVersion: SemanticVersion? + + if let remoteUrl = URL(string: versionPlistURL) { + remoteVersion = await withCheckedContinuation { continuation in + URLSession.shared.dataTask(with: URLRequest(url: remoteUrl)) { data, _, error in + do { + if error == nil, let data = data { + let decoder = PropertyListDecoder() + let remoteInfo = try decoder.decode(WhiskyWineVersion.self, from: data) + let remoteVersion = remoteInfo.version + + continuation.resume(returning: remoteVersion) + return + } + if let error = error { print(error) } - continuation.resume(returning: (false, SemanticVersion(0, 0, 0))) - }.resume() - } + } catch { + print(error) + } + + continuation.resume(returning: nil) + }.resume() + } + } + + if let localVersion = localVersion, let remoteVersion = remoteVersion { + if localVersion < remoteVersion { + return (true, remoteVersion) } }