diff --git a/Sources/ProjectSpec/SpecLoader.swift b/Sources/ProjectSpec/SpecLoader.swift index 0542081a..8c5c0507 100644 --- a/Sources/ProjectSpec/SpecLoader.swift +++ b/Sources/ProjectSpec/SpecLoader.swift @@ -43,6 +43,42 @@ extension Project { } json = merge(dictionary: json, onto: includeDictionary) } + + let basePath = path.parent() + json = expandPaths(in: json, basePath: basePath) + return json } + + private static func expandPaths(in source: Dictionary, basePath: Path) -> Dictionary { + var result = source + + for (key, value) in source { + if let value = value as? String { + result[key] = value.replacingOccurrences(of: "$(CURDIR)", with: basePath.string) + } else if let value = value as? Array { + result[key] = expandPaths(in: value, basePath: basePath) + } else if let value = value as? Dictionary { + result[key] = expandPaths(in: value, basePath: basePath) + } + } + + return result + } + + private static func expandPaths(in source: Array, basePath: Path) -> Array { + var result = source + + for (index, value) in source.enumerated() { + if let value = value as? String { + result[index] = value.replacingOccurrences(of: "$(CURDIR)", with: basePath.string) + } else if let value = value as? Array { + result[index] = expandPaths(in: value, basePath: basePath) + } else if let value = value as? Dictionary { + result[index] = expandPaths(in: value, basePath: basePath) + } + } + + return result + } } diff --git a/Tests/Fixtures/paths_test.yml b/Tests/Fixtures/paths_test.yml new file mode 100644 index 00000000..3f209401 --- /dev/null +++ b/Tests/Fixtures/paths_test.yml @@ -0,0 +1,8 @@ +include: paths_test/included_paths_test.yml +name: NewName +targets: + NewTarget: + type: application + platform: iOS + sources: + - $(CURDIR)/source diff --git a/Tests/Fixtures/paths_test/included_paths_test.yml b/Tests/Fixtures/paths_test/included_paths_test.yml new file mode 100644 index 00000000..f04bece6 --- /dev/null +++ b/Tests/Fixtures/paths_test/included_paths_test.yml @@ -0,0 +1,6 @@ +targets: + IncludedTarget: + type: application + platform: iOS + sources: + - $(CURDIR)/source diff --git a/Tests/XcodeGenKitTests/SpecLoadingTests.swift b/Tests/XcodeGenKitTests/SpecLoadingTests.swift index 565599e7..7a13de6b 100644 --- a/Tests/XcodeGenKitTests/SpecLoadingTests.swift +++ b/Tests/XcodeGenKitTests/SpecLoadingTests.swift @@ -10,6 +10,14 @@ class SpecLoadingTests: XCTestCase { func testSpecLoader() { describe { + $0.it("expands directories") { + let path = fixturePath + "paths_test.yml" + let project = try Project(path: path) + + try expect(project.targets.first!.sources.first!.path) == (fixturePath + "paths_test" + "source").string + try expect(project.targets.last!.sources.first!.path) == (fixturePath + "source").string + } + $0.it("merges includes") { let path = fixturePath + "include_test.yml" let project = try Project(path: path)