|
3 | 3 |
|
4 | 4 | import AppKit |
5 | 5 | import Combine |
| 6 | +import Darwin |
6 | 7 | import Defaults |
7 | 8 | import Foundation |
8 | 9 | import os.log |
@@ -72,9 +73,195 @@ class ConfigurationState: ObservableObject { |
72 | 73 | @Published private(set) var loading = false |
73 | 74 |
|
74 | 75 | private var subscriptions = Set<AnyCancellable>() |
| 76 | + |
| 77 | + // Hot reload support (watch parent directory and resolved target file) |
| 78 | + private var configDirFD: CInt? |
| 79 | + private var configDirSource: DispatchSourceFileSystemObject? |
| 80 | + private var configFileFD: CInt? |
| 81 | + private var configFileSource: DispatchSourceFileSystemObject? |
| 82 | + private var watchedFilePath: String? |
| 83 | + private var reloadDebounceWorkItem: DispatchWorkItem? |
75 | 84 | } |
76 | 85 |
|
77 | 86 | extension ConfigurationState { |
| 87 | + func reloadFromDisk() { |
| 88 | + do { |
| 89 | + let newConfig = try Configuration.load(from: configurationPath) |
| 90 | + guard newConfig != configuration else { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + loading = true |
| 95 | + configuration = newConfig |
| 96 | + loading = false |
| 97 | + |
| 98 | + Notifier.shared.notify( |
| 99 | + title: NSLocalizedString("Configuration Reloaded", comment: ""), |
| 100 | + body: NSLocalizedString("Your configuration changes are now active.", comment: "") |
| 101 | + ) |
| 102 | + } catch CocoaError.fileReadNoSuchFile { |
| 103 | + loading = true |
| 104 | + configuration = .init() |
| 105 | + loading = false |
| 106 | + |
| 107 | + Notifier.shared.notify( |
| 108 | + title: NSLocalizedString("Configuration Reloaded", comment: ""), |
| 109 | + body: NSLocalizedString("Your configuration changes are now active.", comment: "") |
| 110 | + ) |
| 111 | + } catch { |
| 112 | + Notifier.shared.notify( |
| 113 | + title: NSLocalizedString("Failed to reload configuration", comment: ""), |
| 114 | + body: error.localizedDescription |
| 115 | + ) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + func startHotReload() { |
| 120 | + stopHotReload() |
| 121 | + |
| 122 | + // Directory watcher |
| 123 | + let directoryURL = configurationPath.deletingLastPathComponent() |
| 124 | + let dirFD = open(directoryURL.path, O_EVTONLY) |
| 125 | + guard dirFD >= 0 else { |
| 126 | + return |
| 127 | + } |
| 128 | + configDirFD = dirFD |
| 129 | + |
| 130 | + let dirSource = DispatchSource.makeFileSystemObjectSource( |
| 131 | + fileDescriptor: dirFD, |
| 132 | + eventMask: [.write, .attrib, .rename, .link, .delete, .extend, .revoke], |
| 133 | + queue: .main |
| 134 | + ) |
| 135 | + |
| 136 | + dirSource.setEventHandler { [weak self] in |
| 137 | + guard let self else { |
| 138 | + return |
| 139 | + } |
| 140 | + // Any directory entry change may indicate symlink retarget, file replace, create/delete, etc. |
| 141 | + self.updateFileWatcherIfNeeded() |
| 142 | + self.scheduleReloadFromExternalChange() |
| 143 | + } |
| 144 | + |
| 145 | + dirSource.setCancelHandler { [weak self] in |
| 146 | + if let fd = self?.configDirFD { |
| 147 | + close(fd) |
| 148 | + } |
| 149 | + self?.configDirFD = nil |
| 150 | + } |
| 151 | + |
| 152 | + configDirSource = dirSource |
| 153 | + dirSource.resume() |
| 154 | + |
| 155 | + // File watcher for resolved target (or the file itself if not a symlink) |
| 156 | + updateFileWatcherIfNeeded() |
| 157 | + } |
| 158 | + |
| 159 | + func stopHotReload() { |
| 160 | + reloadDebounceWorkItem?.cancel() |
| 161 | + reloadDebounceWorkItem = nil |
| 162 | + |
| 163 | + configDirSource?.cancel() |
| 164 | + configDirSource = nil |
| 165 | + if let fd = configDirFD { |
| 166 | + close(fd) |
| 167 | + } |
| 168 | + configDirFD = nil |
| 169 | + |
| 170 | + configFileSource?.cancel() |
| 171 | + configFileSource = nil |
| 172 | + if let fd = configFileFD { |
| 173 | + close(fd) |
| 174 | + } |
| 175 | + configFileFD = nil |
| 176 | + watchedFilePath = nil |
| 177 | + } |
| 178 | + |
| 179 | + private func scheduleReloadFromExternalChange() { |
| 180 | + reloadDebounceWorkItem?.cancel() |
| 181 | + let work = DispatchWorkItem { [weak self] in |
| 182 | + self?.reloadFromDisk() |
| 183 | + } |
| 184 | + reloadDebounceWorkItem = work |
| 185 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: work) |
| 186 | + } |
| 187 | + |
| 188 | + private func updateFileWatcherIfNeeded() { |
| 189 | + // Watch resolved target file (or the file itself if not a symlink) |
| 190 | + let linkPath = configurationPath.path |
| 191 | + let resolvedPath = (linkPath as NSString).resolvingSymlinksInPath |
| 192 | + |
| 193 | + // If path unchanged, nothing to do |
| 194 | + if watchedFilePath == resolvedPath, configFileSource != nil { |
| 195 | + // Still ensure file exists; if gone, drop watcher so directory watcher can recreate later |
| 196 | + if !FileManager.default.fileExists(atPath: resolvedPath) { |
| 197 | + configFileSource?.cancel() |
| 198 | + configFileSource = nil |
| 199 | + if let fd = configFileFD { |
| 200 | + close(fd) |
| 201 | + } |
| 202 | + configFileFD = nil |
| 203 | + watchedFilePath = nil |
| 204 | + } |
| 205 | + return |
| 206 | + } |
| 207 | + |
| 208 | + // Path changed or no watcher; rebuild |
| 209 | + configFileSource?.cancel() |
| 210 | + configFileSource = nil |
| 211 | + if let fd = configFileFD { |
| 212 | + close(fd) |
| 213 | + } |
| 214 | + configFileFD = nil |
| 215 | + watchedFilePath = nil |
| 216 | + |
| 217 | + guard FileManager.default.fileExists(atPath: resolvedPath) else { |
| 218 | + return |
| 219 | + } |
| 220 | + |
| 221 | + let fd = open(resolvedPath, O_EVTONLY) |
| 222 | + guard fd >= 0 else { |
| 223 | + return |
| 224 | + } |
| 225 | + configFileFD = fd |
| 226 | + |
| 227 | + let fileSource = DispatchSource.makeFileSystemObjectSource( |
| 228 | + fileDescriptor: fd, |
| 229 | + eventMask: [.write, .attrib, .extend, .delete, .rename, .revoke, .link], |
| 230 | + queue: .main |
| 231 | + ) |
| 232 | + |
| 233 | + fileSource.setEventHandler { [weak self] in |
| 234 | + guard let self else { |
| 235 | + return |
| 236 | + } |
| 237 | + let events = fileSource.data |
| 238 | + if events.contains(.delete) || events.contains(.rename) || events.contains(.revoke) { |
| 239 | + // File removed/replaced; drop watcher and rely on directory watcher to re-add |
| 240 | + self.configFileSource?.cancel() |
| 241 | + self.configFileSource = nil |
| 242 | + if let fd = self.configFileFD { |
| 243 | + close(fd) |
| 244 | + } |
| 245 | + self.configFileFD = nil |
| 246 | + self.watchedFilePath = nil |
| 247 | + self.scheduleReloadFromExternalChange() |
| 248 | + } else { |
| 249 | + self.scheduleReloadFromExternalChange() |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + fileSource.setCancelHandler { [weak self] in |
| 254 | + if let fd = self?.configFileFD { |
| 255 | + close(fd) |
| 256 | + } |
| 257 | + self?.configFileFD = nil |
| 258 | + } |
| 259 | + |
| 260 | + configFileSource = fileSource |
| 261 | + watchedFilePath = resolvedPath |
| 262 | + fileSource.resume() |
| 263 | + } |
| 264 | + |
78 | 265 | func revealInFinder() { |
79 | 266 | NSWorkspace.shared.activateFileViewerSelecting([ConfigurationState.shared.configurationPath.absoluteURL]) |
80 | 267 | } |
|
0 commit comments