Skip to content

Commit

Permalink
Copies default config file when none found
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nangtrongvuon committed Aug 7, 2019
1 parent fb9f67e commit 943102c
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions Sources/XiEditor/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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] {
Expand Down

0 comments on commit 943102c

Please sign in to comment.