From 943102c70f561630f91a75ee11ef7f72ca871c2a Mon Sep 17 00:00:00 2001 From: Dung Le Date: Wed, 7 Aug 2019 14:30:05 +0700 Subject: [PATCH] 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. --- Sources/XiEditor/AppDelegate.swift | 48 +++++++++++++++++------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/Sources/XiEditor/AppDelegate.swift b/Sources/XiEditor/AppDelegate.swift index d353a301..7de0a7fc 100644 --- a/Sources/XiEditor/AppDelegate.swift +++ b/Sources/XiEditor/AppDelegate.swift @@ -98,29 +98,11 @@ class AppDelegate: NSObject, NSApplicationDelegate { } lazy var defaultConfigDirectory: URL = { - let applicationDirectory = FileManager.default.urls( + return FileManager.default.urls( for: .applicationSupportDirectory, in: .userDomainMask) .first! .appendingPathComponent("XiEditor") - - // create application support directory and copy preferences file on first run - if !FileManager.default.fileExists(atPath: applicationDirectory.path) { - do { - - try FileManager.default.createDirectory(at: applicationDirectory, - withIntermediateDirectories: true, - attributes: nil) - let preferencesPath = applicationDirectory.appendingPathComponent(PREFERENCES_FILE_NAME) - let defaultConfigPath = Bundle.main.url(forResource: "client_example", withExtension: "toml") - try FileManager.default.copyItem(at: defaultConfigPath!, to: preferencesPath) - - - } catch let err { - fatalError("Failed to create application support directory \(applicationDirectory.path). \(err)") - } - } - return applicationDirectory }() lazy var errorLogDirectory: URL? = { @@ -168,7 +150,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { let xiCore = CoreConnection(rpcSender: rpcSender) self.xiCore = xiCore updateRpcTracingConfig(collectSamplesOnBoot) - + setupConfigDirectory() xiCore.clientStarted(configDir: getUserConfigDirectory(), clientExtrasDir: bundledPluginPath) // fallback values used by NSUserDefaults @@ -274,6 +256,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { //MARK: - top-level interactions @IBAction func openPreferences(_ sender: NSMenuItem) { let preferencesPath = defaultConfigDirectory.appendingPathComponent(PREFERENCES_FILE_NAME) + setupConfigDirectory() NSDocumentController.shared.openDocument( withContentsOf: preferencesPath, display: true, @@ -285,6 +268,31 @@ class AppDelegate: NSObject, NSApplicationDelegate { } //- MARK: - helpers + func setupConfigDirectory() { + let applicationDirectory = self.defaultConfigDirectory + let preferencesDirectory = applicationDirectory.appendingPathComponent(PREFERENCES_FILE_NAME) + + // At setup, we create this application support directory first. + if !FileManager.default.fileExists(atPath: applicationDirectory.path) { + do { + try FileManager.default.createDirectory(at: applicationDirectory, + withIntermediateDirectories: true, + attributes: nil) + } catch let error { + NSApplication.shared.presentError(error) + } + } + + // Then we copy the example config to that folder as `preferences.xiconfig` if it isn't found. + if !FileManager.default.fileExists(atPath: preferencesDirectory.path) { + do { + let defaultConfigPath = Bundle.main.url(forResource: "client_example", withExtension: "toml") + try FileManager.default.copyItem(at: defaultConfigPath!, to: preferencesDirectory) + } catch let error { + NSApplication.shared.presentError(error) + } + } + } func getUserConfigDirectory() -> String { if let configDir = ProcessInfo.processInfo.environment[XI_CONFIG_DIR] {