Skip to content

Commit

Permalink
Merge pull request #127 from bkase/rmalik-json-serialization
Browse files Browse the repository at this point in the history
Load json files directly with NSJSONSerialization if the spec path ha…
  • Loading branch information
yonaskolb authored Nov 3, 2017
2 parents b9163a0 + 91056ef commit 61900fa
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Fixtures/include_test.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
18 changes: 17 additions & 1 deletion Sources/ProjectSpec/SpecLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -60,3 +75,4 @@ extension ProjectSpec {
return merged
}
}

19 changes: 19 additions & 0 deletions Tests/XcodeGenKitTests/SpecLoadingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 61900fa

Please sign in to comment.