mirror of
https://github.com/yonaskolb/XcodeGen.git
synced 2026-03-18 20:02:25 +00:00
Merge pull request #127 from bkase/rmalik-json-serialization
Load json files directly with NSJSONSerialization if the spec path ha…
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user