From 91056efc8a61404460c6e111c81c69b7f9f8e7d6 Mon Sep 17 00:00:00 2001 From: rmalik Date: Fri, 3 Nov 2017 12:10:00 -0700 Subject: [PATCH] Load json files directly with NSJSONSerialization if the spec path extension is `json` --- Fixtures/include_test.json | 29 +++++++++++++++++++ Sources/ProjectSpec/SpecLoader.swift | 18 +++++++++++- Tests/XcodeGenKitTests/SpecLoadingTests.swift | 19 ++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 Fixtures/include_test.json diff --git a/Fixtures/include_test.json b/Fixtures/include_test.json new file mode 100644 index 000000000..3a7e749af --- /dev/null +++ b/Fixtures/include_test.json @@ -0,0 +1,29 @@ +{ + "include": [ + "included.yml" + ], + "name": "NewName", + "settingGroups": { + "test": { + "MY_SETTING1": "NEW VALUE", + "MY_SETTING3": "VALUE3" + }, + "new": { + "MY_SETTING": "VALUE" + }, + "toReplace:REPLACE": { + "MY_SETTING2": "VALUE2" + } + }, + "targets": { + "NewTarget": { + "type": "application", + "platform": "iOS" + }, + "IncludedTarget": { + "name": "IncludedTargetNew", + "platform": "tvOS", + "sources:REPLACE": "NewSource" + } + } +} diff --git a/Sources/ProjectSpec/SpecLoader.swift b/Sources/ProjectSpec/SpecLoader.swift index bda53a4a4..4365e44ed 100644 --- a/Sources/ProjectSpec/SpecLoader.swift +++ b/Sources/ProjectSpec/SpecLoader.swift @@ -19,7 +19,22 @@ extension ProjectSpec { } private static func loadDictionary(path: Path) throws -> JSONDictionary { - var json = try loadYamlDictionary(path: path) + // Get the current path extension + let pathExtension = path.`extension` + + // Depending on the extension we will either load the file as YAML or JSON + var json = [String:Any]() + switch pathExtension?.lowercased() { + case .some("json"): + let string: String = try path.read() + guard let stringData = string.data(using: .utf8) else { fatalError("Error decoding file at path \(path)") } + guard let jsonObj = try JSONSerialization.jsonObject(with: stringData, options: .allowFragments) as? [String:Any] else { + fatalError("Invalid JSON at path \(path)") + } + json = jsonObj + default: + json = try loadYamlDictionary(path: path) + } var includes: [String] if let includeString = json["include"] as? String { @@ -60,3 +75,4 @@ extension ProjectSpec { return merged } } + diff --git a/Tests/XcodeGenKitTests/SpecLoadingTests.swift b/Tests/XcodeGenKitTests/SpecLoadingTests.swift index 56c104ff8..952010121 100644 --- a/Tests/XcodeGenKitTests/SpecLoadingTests.swift +++ b/Tests/XcodeGenKitTests/SpecLoadingTests.swift @@ -48,6 +48,25 @@ func specLoadingTests() { } } + describe("Spec Loader JSON") { + $0.it("merges includes") { + let path = fixturePath + "include_test.json" + let spec = try ProjectSpec(path: path) + + try expect(spec.name) == "NewName" + try expect(spec.settingGroups) == [ + "test": Settings(dictionary: ["MY_SETTING1": "NEW VALUE", "MY_SETTING2": "VALUE2", "MY_SETTING3": "VALUE3"]), + "new": Settings(dictionary: ["MY_SETTING": "VALUE"]), + "toReplace": Settings(dictionary: ["MY_SETTING2": "VALUE2"]), + ] + try expect(spec.targets) == [ + Target(name: "IncludedTargetNew", type: .application, platform: .tvOS, sources: ["NewSource"]), + Target(name: "NewTarget", type: .application, platform: .iOS), + ] + } + } + + describe("Project Spec Parser") { $0.it("fails with incorrect platform") {