diff --git a/Sources/ProjectSpec/Project.swift b/Sources/ProjectSpec/Project.swift index 808b964c..c5f78e52 100644 --- a/Sources/ProjectSpec/Project.swift +++ b/Sources/ProjectSpec/Project.swift @@ -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") diff --git a/Sources/ProjectSpec/SpecLoader.swift b/Sources/ProjectSpec/SpecLoader.swift index e5240c08..72108633 100644 --- a/Sources/ProjectSpec/SpecLoader.swift +++ b/Sources/ProjectSpec/SpecLoader.swift @@ -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) } } diff --git a/Sources/XcodeGenKit/SpecLoader.swift b/Sources/XcodeGenKit/SpecLoader.swift index b9a35a2b..2bc8eb77 100644 --- a/Sources/XcodeGenKit/SpecLoader.swift +++ b/Sources/XcodeGenKit/SpecLoader.swift @@ -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 }