Use the Spec struct rather than dictionaries directly

This commit is contained in:
Ell Neal
2019-01-13 18:14:52 +00:00
parent c90e045bc8
commit 8928088dfe
3 changed files with 10 additions and 46 deletions
+4 -2
View File
@@ -132,9 +132,11 @@ extension Project: Equatable {
extension Project {
public init(basePath: Path, jsonDictionary: JSONDictionary) throws {
public init(spec: Spec, basePath: Path) throws {
self.basePath = basePath
let jsonDictionary = try Project.resolveProject(jsonDictionary: jsonDictionary)
let jsonDictionary = try Project.resolveProject(jsonDictionary: spec.resolvedDictionary())
name = try jsonDictionary.json(atKeyPath: "name")
settings = jsonDictionary.json(atKeyPath: "settings") ?? .empty
settingGroups = jsonDictionary.json(atKeyPath: "settingGroups")
+3 -41
View File
@@ -6,46 +6,8 @@ import Yams
extension Project {
public init(path: Path) throws {
let dictionary = try Project.loadDictionary(path: path)
try self.init(basePath: path.parent(), jsonDictionary: dictionary)
}
public static func loadDictionary(path: Path) throws -> JSONDictionary {
// Depending on the extension we will either load the file as YAML or JSON
var json: [String: Any]
if path.extension?.lowercased() == "json" {
let data: Data = try path.read()
let jsonData = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
guard let jsonDictionary = jsonData as? [String: Any] else {
fatalError("Invalid JSON at path \(path)")
}
json = jsonDictionary
} else {
json = try loadYamlDictionary(path: path)
}
var includes: [String]
if let includeString = json["include"] as? String {
includes = [includeString]
} else if let includeArray = json["include"] as? [String] {
includes = includeArray
} else {
includes = []
}
if !includes.isEmpty {
var includeDictionary: JSONDictionary = [:]
for include in includes {
let includePath = path.parent() + include
let dictionary = try loadDictionary(path: includePath)
includeDictionary = merge(dictionary: dictionary, onto: includeDictionary)
}
json = merge(dictionary: json, onto: includeDictionary)
}
return json
}
let basePath = path.parent()
let template = try Spec(filename: path.lastComponent, basePath: basePath)
try self.init(spec: template, basePath: basePath)
}
}
+3 -3
View File
@@ -16,11 +16,11 @@ public class SpecLoader {
}
public func loadProject(path: Path) throws -> Project {
let dictionary = try Project.loadDictionary(path: path)
let project = try Project(basePath: path.parent(), jsonDictionary: dictionary)
let template = try Project.Spec(filename: path.lastComponent, basePath: path.parent())
let project = try Project(spec: template, basePath: path.parent())
self.project = project
projectDictionary = dictionary
projectDictionary = template.jsonDictionary
return project
}