Skip to content

Commit

Permalink
Refactor install and update checks
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Apr 9, 2024
1 parent f80f56a commit 84e8587
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions WhiskyKit/Sources/WhiskyKit/WhiskyWine/WhiskyWineInstaller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 84e8587

Please sign in to comment.