Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load json files directly with NSJSONSerialization if the spec path ha… #127

Merged
merged 1 commit into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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