feat(updates): 使用 Sparkle 从发布 ZIP 下载并重启更新

This commit is contained in:
2026-11-16 16:17:31 +08:00
parent 451d31682d
commit ec37b8e3f9
23 changed files with 716 additions and 219 deletions
+41 -50
View File
@@ -1,64 +1,55 @@
import Foundation
@main struct ReleaseUpdateChecks {
@MainActor static func main() async throws {
func release(_ tag: String, draft: Bool = false, prerelease: Bool = false) -> PublishedRelease {
PublishedRelease(tagName: tag, draft: draft, prerelease: prerelease, assets: [
.init(name: "kymo-1.3-15-arm64-adhoc.zip"),
.init(name: "kymo-1.3-16-arm64-adhoc.zip.sha256"),
.init(name: "kymo-1.2-99-arm64-adhoc.zip")
])
}
let stable = release("v1.3")
let comparisons = try [
stable.isNewer(than: "1.3.0", build: 15), stable.isNewer(than: "1.3", build: 14),
stable.isNewer(than: "1.2", build: 99), stable.isNewer(than: "2.0", build: 1),
release("v1.10").isNewer(than: "1.9", build: 30)
]
precondition(comparisons == [false, true, true, false, true])
precondition(stable.page.absoluteString == "https://git.airnan.cn/wangairnan/Kymo/releases/tag/v1.3")
for invalid in [release("v1.4", draft: true), release("v1.4", prerelease: true), release("v1.4-rc.1"), release("main")] {
precondition((try? invalid.isNewer(than: "1.3", build: 15)) == nil)
}
@MainActor static func main() {
let app = Bundle(path: CommandLine.arguments[1])!
precondition(app.object(forInfoDictionaryKey: "SUScheduledCheckInterval") as? Int == 6 * 60 * 60)
precondition(app.object(forInfoDictionaryKey: "SUEnableAutomaticChecks") as? Bool == true)
precondition(app.object(forInfoDictionaryKey: "SURequireSignedFeed") as? Bool == true)
precondition(app.object(forInfoDictionaryKey: "SUVerifyUpdateBeforeExtraction") as? Bool == true)
let suite = "KymoReleaseUpdateChecks." + UUID().uuidString
let defaults = UserDefaults(suiteName: suite)!
defer { defaults.removePersistentDomain(forName: suite) }
var pending: [CheckedContinuation<PublishedRelease, Error>] = []
let updates = ReleaseUpdates(defaults: defaults, version: "1.2", build: 10) {
try await withCheckedThrowingContinuation { pending.append($0) }
let updates = ReleaseUpdates(defaults: defaults)
let release = ReleaseUpdates.Version(display: "1.5", build: "17", infoURL: nil)
var checks = 0, downloads = 0, restarts = 0
updates.beginCheck = {
checks += 1
updates.transition(to: .checking)
}
func wait(_ condition: () -> Bool) async {
let deadline = ContinuousClock.now.advanced(by: .seconds(3))
while !condition() && ContinuousClock.now < deadline { await Task.yield() }
precondition(condition())
updates.beginDownload = { version in
precondition(version == release)
downloads += 1
updates.transition(to: .downloading)
}
precondition(updates.automatic)
updates.start()
updates.check()
await wait { pending.count == 1 }
var changes = [Bool]()
updates.changeAutomatic = { changes.append($0) }
updates.automatic = false
precondition(updates.phase == .idle)
let restarted = ReleaseUpdates(defaults: defaults)
restarted.start()
precondition(!restarted.automatic && restarted.phase == .idle)
precondition(changes == [false] && !ReleaseUpdates(defaults: defaults).automatic)
updates.check()
await wait { pending.count == 2 }
pending.removeLast().resume(returning: stable)
await wait { updates.phase == .checked }
pending.removeLast().resume(throwing: URLError(.cancelled))
await Task.yield()
precondition(updates.available == stable && updates.phase == .checked)
updates.automatic = true
await wait { pending.count == 1 }
pending.removeLast().resume(throwing: URLError(.notConnectedToInternet))
await wait { updates.phase == .failed }
updates.check()
await wait { pending.count == 1 }
pending.removeLast().resume(returning: release("v1.0"))
await wait { updates.phase == .checked }
precondition(updates.available == nil)
updates.stop()
print("PASS: release versions and build assets, stable releases, automatic preference, cancellation, single request, manual retry and recovery")
precondition(checks == 1 && updates.phase == .checking)
updates.found(release)
updates.performAction()
updates.performAction()
precondition(downloads == 1)
updates.transition(to: .failed, error: "offline")
updates.performAction()
precondition(checks == 2 && updates.errorMessage == nil)
updates.found(release)
updates.performAction()
updates.transition(to: .extracting, fraction: 0.5)
updates.check()
precondition(checks == 2 && updates.fraction == 0.5)
updates.ready { restarts += 1 }
precondition(restarts == 0 && updates.phase == .ready)
updates.performAction()
updates.performAction()
precondition(restarts == 1 && downloads == 2 && updates.phase == .restarting)
updates.transition(to: .current)
precondition(updates.version == nil)
print("PASS: preserved preference, manual check, shared download/restart actions, busy state, retry and current version")
}
}