forked from ZekeSnider/Jared
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
657e0fb
commit a4b1823
Showing
9 changed files
with
125 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// ConfigurationHelper.swift | ||
// Jared | ||
// | ||
// Created by Zeke Snider on 8/22/20. | ||
// Copyright © 2020 Zeke Snider. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct ConfigurationHelper { | ||
static let fileManager = FileManager.default | ||
|
||
static func getConfiguration() -> ConfigurationFile { | ||
let configPath = ConfigurationHelper.getSupportDirectory() | ||
.appendingPathComponent("config.json") | ||
ConfigurationHelper.createConfigFileIfNotExists(at: configPath, using: fileManager) | ||
|
||
//Read the JSON config file | ||
var config: ConfigurationFile | ||
let jsonData = try? NSData(contentsOfFile: configPath.path, options: .mappedIfSafe) | ||
if let parsedConfig = try? JSONDecoder().decode(ConfigurationFile.self, from: jsonData! as Data) { | ||
config = parsedConfig | ||
} else { | ||
NSLog("Unable to parse configuration file, using default") | ||
config = ConfigurationFile() | ||
} | ||
|
||
return config | ||
} | ||
|
||
static func getSupportDirectory() -> URL { | ||
let filemanager = FileManager.default | ||
let appsupport = filemanager.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0] | ||
let supportDir = appsupport.appendingPathComponent("Jared") | ||
|
||
try! filemanager.createDirectory(at: supportDir, withIntermediateDirectories: true, attributes: nil) | ||
|
||
return supportDir | ||
} | ||
|
||
static func getPluginDirectory() -> URL { | ||
let supportDir = getSupportDirectory() | ||
.appendingPathComponent("Plugins") | ||
|
||
try! fileManager.createDirectory(at: supportDir, withIntermediateDirectories: true, attributes: nil) | ||
|
||
return supportDir | ||
} | ||
|
||
//Copy an empty config file if the conig file does not exist | ||
private static func createConfigFileIfNotExists(at path: URL, using fileManager: FileManager) { | ||
//Copy an empty config file if the conig file does not exist | ||
if !fileManager.fileExists(atPath: path.path) { | ||
try! fileManager.copyItem(at: (Bundle.main.resourceURL?.appendingPathComponent("config.json"))!, to: path) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.