-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathUpdateDelegate.swift
More file actions
144 lines (131 loc) · 5.95 KB
/
UpdateDelegate.swift
File metadata and controls
144 lines (131 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import Sparkle
import Cocoa
enum UpdateFeedResolver {
static let fallbackFeedURL = "https://github.com/manaflow-ai/cmux/releases/latest/download/appcast.xml"
static func resolvedFeedURLString(infoFeedURL: String?) -> (url: String, isNightly: Bool, usedFallback: Bool) {
guard let infoFeedURL, !infoFeedURL.isEmpty else {
return (fallbackFeedURL, false, true)
}
return (infoFeedURL, infoFeedURL.contains("/nightly/"), false)
}
}
extension UpdateDriver: SPUUpdaterDelegate {
func updaterShouldPromptForPermissionToCheck(forUpdates _: SPUUpdater) -> Bool {
false
}
func feedURLString(for updater: SPUUpdater) -> String? {
#if DEBUG
let env = ProcessInfo.processInfo.environment
if let override = env["CMUX_UI_TEST_FEED_URL"], !override.isEmpty {
UpdateTestURLProtocol.registerIfNeeded()
recordFeedURLString(override, usedFallback: false)
return override
}
#endif
// The feed URL is baked into Info.plist at build time:
// - Stable releases use the stable appcast URL
// - cmux NIGHTLY has the nightly appcast URL injected by CI
let infoFeedURL = Bundle.main.object(forInfoDictionaryKey: "SUFeedURL") as? String
let resolved = UpdateFeedResolver.resolvedFeedURLString(infoFeedURL: infoFeedURL)
UpdateLogStore.shared.append("update channel: \(resolved.isNightly ? "nightly" : "stable")")
recordFeedURLString(resolved.url, usedFallback: resolved.usedFallback)
return resolved.url
}
func updater(_ updater: SPUUpdater, willScheduleUpdateCheckAfterDelay delay: TimeInterval) {
UpdateLogStore.shared.append("next update check scheduled in \(Int(delay.rounded()))s")
}
func updaterWillNotScheduleUpdateCheck(_ updater: SPUUpdater) {
UpdateLogStore.shared.append("automatic update checks disabled; no scheduled check")
}
/// Called when an update is scheduled to install silently,
/// which occurs when automatic download is enabled.
func updater(_ updater: SPUUpdater, willInstallUpdateOnQuit item: SUAppcastItem, immediateInstallationBlock immediateInstallHandler: @escaping () -> Void) -> Bool {
let guardedImmediateInstallHandler = makeGuardedUpdateRelaunchAction(
applicationTerminated: false,
action: immediateInstallHandler
)
DispatchQueue.main.async { [weak viewModel] in
viewModel?.clearDetectedUpdate()
viewModel?.state = .installing(.init(
isAutoUpdate: true,
retryTerminatingApplication: guardedImmediateInstallHandler,
dismiss: { [weak viewModel] in
viewModel?.state = .idle
}
))
}
return true
}
func updater(_ updater: SPUUpdater, didFinishLoading appcast: SUAppcast) {
let count = appcast.items.count
let firstVersion = appcast.items.first?.displayVersionString ?? ""
if firstVersion.isEmpty {
UpdateLogStore.shared.append("appcast loaded (items=\(count))")
} else {
UpdateLogStore.shared.append("appcast loaded (items=\(count), first=\(firstVersion))")
}
}
func updater(_ updater: SPUUpdater, didFindValidUpdate item: SUAppcastItem) {
DispatchQueue.main.async { [weak viewModel] in
viewModel?.recordDetectedUpdate(item)
}
let version = item.displayVersionString
let fileURL = item.fileURL?.absoluteString ?? ""
if fileURL.isEmpty {
UpdateLogStore.shared.append("valid update found: \(version)")
} else {
UpdateLogStore.shared.append("valid update found: \(version) (\(fileURL))")
}
}
func updaterDidNotFindUpdate(_ updater: SPUUpdater, error: Error) {
DispatchQueue.main.async { [weak viewModel] in
viewModel?.clearDetectedUpdate()
}
let nsError = error as NSError
let reasonValue = (nsError.userInfo[SPUNoUpdateFoundReasonKey] as? NSNumber)?.intValue
let reason = reasonValue.map { SPUNoUpdateFoundReason(rawValue: OSStatus($0)) } ?? nil
let reasonText = reason.map(describeNoUpdateFoundReason) ?? "unknown"
let userInitiated = (nsError.userInfo[SPUNoUpdateFoundUserInitiatedKey] as? NSNumber)?.boolValue ?? false
let latestItem = nsError.userInfo[SPULatestAppcastItemFoundKey] as? SUAppcastItem
let latestVersion = latestItem?.displayVersionString ?? ""
if latestVersion.isEmpty {
UpdateLogStore.shared.append("no update found (reason=\(reasonText), userInitiated=\(userInitiated))")
} else {
UpdateLogStore.shared.append("no update found (reason=\(reasonText), userInitiated=\(userInitiated), latest=\(latestVersion))")
}
}
func updater(_ updater: SPUUpdater, userDidMake choice: SPUUserUpdateChoice, forUpdate _: SUAppcastItem, state: SPUUserUpdateState) {
DispatchQueue.main.async { [weak viewModel] in
viewModel?.clearDetectedUpdate()
}
if state.userInitiated, choice != .install {
finishUserInitiatedCheckPresentation()
}
}
func updaterWillRelaunchApplication(_ updater: SPUUpdater) {
Task { @MainActor in
AppDelegate.shared?.persistSessionForUpdateRelaunch()
TerminalController.shared.stop()
NSApp.invalidateRestorableState()
for window in NSApp.windows {
window.invalidateRestorableState()
}
}
}
}
private func describeNoUpdateFoundReason(_ reason: SPUNoUpdateFoundReason) -> String {
switch reason {
case .unknown:
return "unknown"
case .onLatestVersion:
return "onLatestVersion"
case .onNewerThanLatestVersion:
return "onNewerThanLatestVersion"
case .systemIsTooOld:
return "systemIsTooOld"
case .systemIsTooNew:
return "systemIsTooNew"
@unknown default:
return "unknown"
}
}