Mac: write config.toml through a symlink !222
2 files changed, +29 −4
Layout: unified · split
Sources/OrgApp/ConfigSync.swift +10 −4
| @@ -29,6 +29,9 @@ public final class ConfigSync { | ||
| 29 | 29 | @ObservationIgnored private var observer: NSObjectProtocol? |
| 30 | 30 | @ObservationIgnored private var serial = 0 |
| 31 | 31 | |
| 32 | /// Where `url` leads: a symlinked config.toml is written through, not replaced by a file. | |
| 33 | private var target: URL { url.resolvingSymlinksInPath() } | |
| 34 | ||
| 32 | 35 | public init(url: URL = UserConfig.configFile, defaults: UserDefaults = .standard) { |
| 33 | 36 | self.url = url |
| 34 | 37 | self.defaults = defaults |
| @@ -49,13 +52,13 @@ public final class ConfigSync { | ||
| 49 | 52 | let migrated = UserConfig.usesLegacyNames(text) ? UserConfig.migratingLegacyNames(text) : text |
| 50 | 53 | let completed = UserConfig.addingMissingSections(migrated, from: defaults) |
| 51 | 54 | if completed != text { |
| 52 | try? completed.write(to: url, atomically: true, encoding: .utf8) | |
| 55 | try? completed.write(to: target, atomically: true, encoding: .utf8) | |
| 53 | 56 | self.text = completed |
| 54 | 57 | } |
| 55 | 58 | } |
| 56 | 59 | } else { |
| 57 | 60 | let template = UserConfig.template(from: defaults) |
| 58 | try? template.write(to: url, atomically: true, encoding: .utf8) | |
| 61 | try? template.write(to: target, atomically: true, encoding: .utf8) | |
| 59 | 62 | text = template |
| 60 | 63 | } |
| 61 | 64 | observer = NotificationCenter.default.addObserver(forName: UserDefaults.didChangeNotification, object: defaults, queue: .main) { [weak self] _ in |
| @@ -63,10 +66,13 @@ public final class ConfigSync { | ||
| 63 | 66 | } |
| 64 | 67 | guard watch else { return } |
| 65 | 68 | let directory = url.deletingLastPathComponent().resolvingSymlinksInPath() |
| 69 | // The linked file's folder too, when config.toml is a symlink. | |
| 70 | let linked = target.deletingLastPathComponent().resolvingSymlinksInPath() | |
| 71 | let watchesLinked = linked.path != directory.path | |
| 66 | 72 | // Files still read from where earlier versions kept them reload when edited there too. |
| 67 | 73 | let legacy = UserConfig.legacyDirectory.resolvingSymlinksInPath() |
| 68 | 74 | let watchesLegacy = legacy.path != directory.path && FileManager.default.fileExists(atPath: legacy.path) |
| 69 | watcher = FSEventsWatcher(roots: [directory] + (watchesLegacy ? [legacy] : [])) { [weak self] events in | |
| 75 | watcher = FSEventsWatcher(roots: [directory] + (watchesLinked ? [linked] : []) + (watchesLegacy ? [legacy] : [])) { [weak self] events in | |
| 70 | 76 | let changed = Set(events.compactMap { event -> URL? in |
| 71 | 77 | if case .changed(let url) = event { return url } |
| 72 | 78 | return nil |
| @@ -130,7 +136,7 @@ public final class ConfigSync { | ||
| 130 | 136 | } |
| 131 | 137 | guard current != original else { return } |
| 132 | 138 | text = current |
| 133 | try? current.write(to: url, atomically: true, encoding: .utf8) | |
| 139 | try? current.write(to: target, atomically: true, encoding: .utf8) | |
| 134 | 140 | } |
| 135 | 141 | } |
| 136 | 142 | #endif |
Tests/OrgAppTests/UserConfigTests.swift +19
| @@ -119,6 +119,25 @@ struct UserConfigTests { | ||
| 119 | 119 | #expect(store.integer(forKey: "agendaStartOffset") == 0) |
| 120 | 120 | #expect(sync.problems.isEmpty) |
| 121 | 121 | } |
| 122 | ||
| 123 | /// A config.toml linked to another folder's (as the iOS app's) stays a link when the | |
| 124 | /// Settings window writes it. | |
| 125 | @Test func writesThroughASymlink() throws { | |
| 126 | let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-config-\(UUID().uuidString)") | |
| 127 | defer { try? FileManager.default.removeItem(at: folder) } | |
| 128 | let shared = folder.appendingPathComponent("notes/config.toml") | |
| 129 | let link = folder.appendingPathComponent("orgstar/config.toml") | |
| 130 | try FileManager.default.createDirectory(at: shared.deletingLastPathComponent(), withIntermediateDirectories: true) | |
| 131 | try FileManager.default.createDirectory(at: link.deletingLastPathComponent(), withIntermediateDirectories: true) | |
| 132 | try "fill-column = 80\n".write(to: shared, atomically: true, encoding: .utf8) | |
| 133 | try FileManager.default.createSymbolicLink(at: link, withDestinationURL: shared) | |
| 134 | let store = defaults() | |
| 135 | let sync = ConfigSync(url: link, defaults: store) | |
| 136 | sync.start(watch: false) | |
| 137 | store.set(66, forKey: "fillColumn") | |
| 138 | #expect(try FileManager.default.destinationOfSymbolicLink(atPath: link.path) == shared.path) | |
| 139 | #expect(try String(contentsOf: shared, encoding: .utf8).contains("fill-column = 66")) | |
| 140 | } | |
| 122 | 141 | } |
| 123 | 142 | |
| 124 | 143 | @MainActor |