cmux/Sources/Update/UpdateController.swift
Lawrence Chen 9817d131f8
Release v1.23.0 (#31)
* Rename cmuxterm to cmux across entire codebase

- Rename GitHub repos: manaflow-ai/cmuxterm -> manaflow-ai/cmux,
  manaflow-ai/homebrew-cmuxterm -> manaflow-ai/homebrew-cmux
- Rename bundle IDs: com.cmuxterm.app -> com.cmux.app
- Rename CLI: CLI/cmuxterm.swift -> CLI/cmux.swift
- Rename homebrew submodule: homebrew-cmuxterm -> homebrew-cmux
- Update all socket paths: /tmp/cmuxterm*.sock -> /tmp/cmux*.sock
- Update all GitHub URLs, DMG names, Sparkle URLs
- Update all source files, scripts, tests, docs, CI workflows

* Bump version to 1.23.0
2026-02-09 15:30:43 -08:00

204 lines
7.1 KiB
Swift

import Sparkle
import Cocoa
import Combine
import SwiftUI
/// Controller for managing Sparkle updates in cmux.
class UpdateController {
private(set) var updater: SPUUpdater
private let userDriver: UpdateDriver
private var installCancellable: AnyCancellable?
private var noUpdateDismissCancellable: AnyCancellable?
private var noUpdateDismissWorkItem: DispatchWorkItem?
var viewModel: UpdateViewModel {
userDriver.viewModel
}
/// True if we're force-installing an update.
var isInstalling: Bool {
installCancellable != nil
}
init() {
let hostBundle = Bundle.main
self.userDriver = UpdateDriver(viewModel: .init(), hostBundle: hostBundle)
self.updater = SPUUpdater(
hostBundle: hostBundle,
applicationBundle: hostBundle,
userDriver: userDriver,
delegate: userDriver
)
installNoUpdateDismissObserver()
}
deinit {
installCancellable?.cancel()
noUpdateDismissCancellable?.cancel()
noUpdateDismissWorkItem?.cancel()
}
/// Start the updater. If startup fails, the error is shown via the custom UI.
func startUpdater() {
ensureSparkleInstallationCache()
do {
try updater.start()
} catch {
userDriver.viewModel.state = .error(.init(
error: error,
retry: { [weak self] in
self?.userDriver.viewModel.state = .idle
self?.startUpdater()
},
dismiss: { [weak self] in
self?.userDriver.viewModel.state = .idle
}
))
}
}
/// Force install the current update by auto-confirming all installable states.
func installUpdate() {
guard viewModel.state.isInstallable else { return }
guard installCancellable == nil else { return }
installCancellable = viewModel.$state.sink { [weak self] state in
guard let self else { return }
guard state.isInstallable else {
self.installCancellable = nil
return
}
state.confirm()
}
}
/// Check for updates (used by the menu item).
@objc func checkForUpdates() {
UpdateLogStore.shared.append("checkForUpdates invoked (state=\(viewModel.state.isIdle ? "idle" : "busy"))")
ensureSparkleInstallationCache()
if viewModel.state == .idle {
updater.checkForUpdates()
return
}
installCancellable?.cancel()
viewModel.state.cancel()
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { [weak self] in
self?.updater.checkForUpdates()
}
}
/// Check for updates once the updater is ready (used by UI tests).
func checkForUpdatesWhenReady(retries: Int = 10) {
let canCheck = updater.canCheckForUpdates
UpdateLogStore.shared.append("checkForUpdatesWhenReady invoked (canCheck=\(canCheck))")
if canCheck {
checkForUpdates()
return
}
guard retries > 0 else {
UpdateLogStore.shared.append("checkForUpdatesWhenReady timed out")
return
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.checkForUpdatesWhenReady(retries: retries - 1)
}
}
/// Validate the check for updates menu item.
func validateMenuItem(_ item: NSMenuItem) -> Bool {
if item.action == #selector(checkForUpdates) {
return updater.canCheckForUpdates
}
return true
}
private func installNoUpdateDismissObserver() {
noUpdateDismissCancellable = Publishers.CombineLatest(viewModel.$state, viewModel.$overrideState)
.receive(on: DispatchQueue.main)
.sink { [weak self] state, overrideState in
self?.scheduleNoUpdateDismiss(for: state, overrideState: overrideState)
}
}
private func scheduleNoUpdateDismiss(for state: UpdateState, overrideState: UpdateState?) {
noUpdateDismissWorkItem?.cancel()
noUpdateDismissWorkItem = nil
guard overrideState == nil else { return }
guard case .notFound(let notFound) = state else { return }
recordUITestTimestamp(key: "noUpdateShownAt")
let workItem = DispatchWorkItem { [weak self] in
guard let self else { return }
guard self.viewModel.overrideState == nil,
case .notFound = self.viewModel.state else { return }
withAnimation(.easeInOut(duration: 0.25)) {
self.recordUITestTimestamp(key: "noUpdateHiddenAt")
self.viewModel.state = .idle
}
notFound.acknowledgement()
}
noUpdateDismissWorkItem = workItem
DispatchQueue.main.asyncAfter(
deadline: .now() + UpdateTiming.noUpdateDisplayDuration,
execute: workItem
)
}
private func recordUITestTimestamp(key: String) {
#if DEBUG
let env = ProcessInfo.processInfo.environment
guard env["CMUX_UI_TEST_MODE"] == "1" else { return }
guard let path = env["CMUX_UI_TEST_TIMING_PATH"] else { return }
let url = URL(fileURLWithPath: path)
var payload: [String: Double] = [:]
if let data = try? Data(contentsOf: url),
let object = try? JSONSerialization.jsonObject(with: data) as? [String: Double] {
payload = object
}
payload[key] = Date().timeIntervalSince1970
if let data = try? JSONSerialization.data(withJSONObject: payload) {
try? data.write(to: url)
}
#endif
}
private func ensureSparkleInstallationCache() {
guard let bundleIdentifier = Bundle.main.bundleIdentifier else { return }
guard let cachesURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else { return }
let baseURL = cachesURL
.appendingPathComponent(bundleIdentifier)
.appendingPathComponent("org.sparkle-project.Sparkle")
let installURL = baseURL.appendingPathComponent("Installation")
var isDirectory: ObjCBool = false
if FileManager.default.fileExists(atPath: installURL.path, isDirectory: &isDirectory) {
if !isDirectory.boolValue {
do {
try FileManager.default.removeItem(at: installURL)
} catch {
UpdateLogStore.shared.append("Failed removing Sparkle installation cache file: \(error)")
return
}
} else {
return
}
}
do {
try FileManager.default.createDirectory(
at: installURL,
withIntermediateDirectories: true,
attributes: [.posixPermissions: 0o700]
)
UpdateLogStore.shared.append("Ensured Sparkle installation cache at \(installURL.path)")
} catch {
UpdateLogStore.shared.append("Failed creating Sparkle installation cache: \(error)")
}
}
}