Skip to content

Commit 943102c

Browse files
committed
Copies default config file when none found
Previously xi-mac only copied the base config file on startup - this commit makes it also copies the base config when the user opens the preference folder and no config is found.
1 parent fb9f67e commit 943102c

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

Sources/XiEditor/AppDelegate.swift

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
9898
}
9999

100100
lazy var defaultConfigDirectory: URL = {
101-
let applicationDirectory = FileManager.default.urls(
101+
return FileManager.default.urls(
102102
for: .applicationSupportDirectory,
103103
in: .userDomainMask)
104104
.first!
105105
.appendingPathComponent("XiEditor")
106-
107-
// create application support directory and copy preferences file on first run
108-
if !FileManager.default.fileExists(atPath: applicationDirectory.path) {
109-
do {
110-
111-
try FileManager.default.createDirectory(at: applicationDirectory,
112-
withIntermediateDirectories: true,
113-
attributes: nil)
114-
let preferencesPath = applicationDirectory.appendingPathComponent(PREFERENCES_FILE_NAME)
115-
let defaultConfigPath = Bundle.main.url(forResource: "client_example", withExtension: "toml")
116-
try FileManager.default.copyItem(at: defaultConfigPath!, to: preferencesPath)
117-
118-
119-
} catch let err {
120-
fatalError("Failed to create application support directory \(applicationDirectory.path). \(err)")
121-
}
122-
}
123-
return applicationDirectory
124106
}()
125107

126108
lazy var errorLogDirectory: URL? = {
@@ -168,7 +150,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
168150
let xiCore = CoreConnection(rpcSender: rpcSender)
169151
self.xiCore = xiCore
170152
updateRpcTracingConfig(collectSamplesOnBoot)
171-
153+
setupConfigDirectory()
172154
xiCore.clientStarted(configDir: getUserConfigDirectory(), clientExtrasDir: bundledPluginPath)
173155

174156
// fallback values used by NSUserDefaults
@@ -274,6 +256,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
274256
//MARK: - top-level interactions
275257
@IBAction func openPreferences(_ sender: NSMenuItem) {
276258
let preferencesPath = defaultConfigDirectory.appendingPathComponent(PREFERENCES_FILE_NAME)
259+
setupConfigDirectory()
277260
NSDocumentController.shared.openDocument(
278261
withContentsOf: preferencesPath,
279262
display: true,
@@ -285,6 +268,31 @@ class AppDelegate: NSObject, NSApplicationDelegate {
285268
}
286269

287270
//- MARK: - helpers
271+
func setupConfigDirectory() {
272+
let applicationDirectory = self.defaultConfigDirectory
273+
let preferencesDirectory = applicationDirectory.appendingPathComponent(PREFERENCES_FILE_NAME)
274+
275+
// At setup, we create this application support directory first.
276+
if !FileManager.default.fileExists(atPath: applicationDirectory.path) {
277+
do {
278+
try FileManager.default.createDirectory(at: applicationDirectory,
279+
withIntermediateDirectories: true,
280+
attributes: nil)
281+
} catch let error {
282+
NSApplication.shared.presentError(error)
283+
}
284+
}
285+
286+
// Then we copy the example config to that folder as `preferences.xiconfig` if it isn't found.
287+
if !FileManager.default.fileExists(atPath: preferencesDirectory.path) {
288+
do {
289+
let defaultConfigPath = Bundle.main.url(forResource: "client_example", withExtension: "toml")
290+
try FileManager.default.copyItem(at: defaultConfigPath!, to: preferencesDirectory)
291+
} catch let error {
292+
NSApplication.shared.presentError(error)
293+
}
294+
}
295+
}
288296

289297
func getUserConfigDirectory() -> String {
290298
if let configDir = ProcessInfo.processInfo.environment[XI_CONFIG_DIR] {

0 commit comments

Comments
 (0)