forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateChecker.swift
More file actions
81 lines (68 loc) · 2.43 KB
/
UpdateChecker.swift
File metadata and controls
81 lines (68 loc) · 2.43 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
import Logger
import Preferences
import Sparkle
public protocol UpdateCheckerProtocol {
func checkForUpdates()
func getAutomaticallyChecksForUpdates() -> Bool
func setAutomaticallyChecksForUpdates(_ value: Bool)
}
public protocol UpdateCheckerDelegate: AnyObject {
func prepareForRelaunch(finish: @escaping () -> Void)
}
public final class NoopUpdateChecker: UpdateCheckerProtocol {
public init() {}
public func checkForUpdates() {}
public func getAutomaticallyChecksForUpdates() -> Bool { false }
public func setAutomaticallyChecksForUpdates(_ value: Bool) {}
}
public final class UpdateChecker: UpdateCheckerProtocol {
let updater: SPUUpdater
let delegate = UpdaterDelegate()
public init(hostBundle: Bundle, checkerDelegate: UpdateCheckerDelegate) {
updater = SPUUpdater(
hostBundle: hostBundle,
applicationBundle: Bundle.main,
userDriver: SPUStandardUserDriver(hostBundle: hostBundle, delegate: nil),
delegate: delegate
)
delegate.updateCheckerDelegate = checkerDelegate
do {
try updater.start()
} catch {
Logger.updateChecker.error(error.localizedDescription)
}
}
public convenience init?(hostBundle: Bundle?, checkerDelegate: UpdateCheckerDelegate) {
guard let hostBundle = hostBundle else { return nil }
self.init(hostBundle: hostBundle, checkerDelegate: checkerDelegate)
}
public func checkForUpdates() {
updater.checkForUpdates()
}
public func getAutomaticallyChecksForUpdates() -> Bool {
updater.automaticallyChecksForUpdates
}
public func setAutomaticallyChecksForUpdates(_ value: Bool) {
updater.automaticallyChecksForUpdates = value
}
}
class UpdaterDelegate: NSObject, SPUUpdaterDelegate {
weak var updateCheckerDelegate: UpdateCheckerDelegate?
func updater(
_ updater: SPUUpdater,
shouldPostponeRelaunchForUpdate item: SUAppcastItem,
untilInvokingBlock installHandler: @escaping () -> Void) -> Bool {
if let updateCheckerDelegate {
updateCheckerDelegate.prepareForRelaunch(finish: installHandler)
return true
}
return false
}
func allowedChannels(for updater: SPUUpdater) -> Set<String> {
if UserDefaults.shared.value(for: \.installPrereleases) {
Set(["prerelease"])
} else {
[]
}
}
}