@@ -98,29 +98,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
98
98
}
99
99
100
100
lazy var defaultConfigDirectory : URL = {
101
- let applicationDirectory = FileManager . default. urls (
101
+ return FileManager . default. urls (
102
102
for: . applicationSupportDirectory,
103
103
in: . userDomainMask)
104
104
. first!
105
105
. 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
124
106
} ( )
125
107
126
108
lazy var errorLogDirectory : URL ? = {
@@ -168,7 +150,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
168
150
let xiCore = CoreConnection ( rpcSender: rpcSender)
169
151
self . xiCore = xiCore
170
152
updateRpcTracingConfig ( collectSamplesOnBoot)
171
-
153
+ setupConfigDirectory ( )
172
154
xiCore. clientStarted ( configDir: getUserConfigDirectory ( ) , clientExtrasDir: bundledPluginPath)
173
155
174
156
// fallback values used by NSUserDefaults
@@ -274,6 +256,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
274
256
//MARK: - top-level interactions
275
257
@IBAction func openPreferences( _ sender: NSMenuItem ) {
276
258
let preferencesPath = defaultConfigDirectory. appendingPathComponent ( PREFERENCES_FILE_NAME)
259
+ setupConfigDirectory ( )
277
260
NSDocumentController . shared. openDocument (
278
261
withContentsOf: preferencesPath,
279
262
display: true ,
@@ -285,6 +268,31 @@ class AppDelegate: NSObject, NSApplicationDelegate {
285
268
}
286
269
287
270
//- 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
+ }
288
296
289
297
func getUserConfigDirectory( ) -> String {
290
298
if let configDir = ProcessInfo . processInfo. environment [ XI_CONFIG_DIR] {
0 commit comments