Skip to content

Commit

Permalink
Merge pull request #651 from kateinoigakukun/expand-any-array-templat…
Browse files Browse the repository at this point in the history
…e-var

Expand template variable in Array of Any
  • Loading branch information
yonaskolb committed Sep 11, 2019
2 parents faf4930 + e3b2414 commit c2f9ff2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Added `includes` to `sources` for a Target. This follows the same glob-style as `excludes` but functions as a way to only include files that match a specified pattern. Useful if you only want a certain file type, for example specifying `**/*.swift`. [#637](https://github.com/yonaskolb/XcodeGen/pull/637) @bclymer
- Support `dylib` SDK. [#650](https://github.com/yonaskolb/XcodeGen/pull/650)

#### Fixed

- Expand template variable in Array of Any [#651](https://github.com/yonaskolb/XcodeGen/pull/651) @kateinoigakukun

## 2.7.0

#### Added
Expand Down
29 changes: 18 additions & 11 deletions Sources/ProjectSpec/SpecFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,25 @@ extension Dictionary where Key == String, Value: Any {
func replaceString(_ template: String, with replacement: String) -> JSONDictionary {
var replaced: JSONDictionary = self
for (key, value) in self {
switch value {
case let dictionary as JSONDictionary:
replaced[key] = dictionary.replaceString(template, with: replacement)
case let string as String:
replaced[key] = string.replacingOccurrences(of: template, with: replacement)
case let array as [JSONDictionary]:
replaced[key] = array.map { $0.replaceString(template, with: replacement) }
case let array as [String]:
replaced[key] = array.map { $0.replacingOccurrences(of: template, with: replacement) }
default: break
}
replaced[key] = replace(value: value, template, with: replacement)
}
return replaced
}

func replace(value: Any, _ template: String, with replacement: String) -> Any {
switch value {
case let dictionary as JSONDictionary:
return dictionary.replaceString(template, with: replacement)
case let string as String:
return string.replacingOccurrences(of: template, with: replacement)
case let array as [JSONDictionary]:
return array.map { $0.replaceString(template, with: replacement) }
case let array as [String]:
return array.map { $0.replacingOccurrences(of: template, with: replacement) }
case let anyArray as [Any]:
return anyArray.map { self.replace(value: $0, template, with: replacement) }
default:
return value
}
}
}
7 changes: 5 additions & 2 deletions Tests/XcodeGenKitTests/SpecLoadingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,10 @@ class SpecLoadingTests: XCTestCase {
"targetTemplates": [
"temp": [
"platform": "iOS",
"sources": ["templateSource"],
"sources": [
"templateSource",
["path": "Sources/${target_name}"]
],
],
"temp2": [
"type": "framework",
Expand All @@ -482,7 +485,7 @@ class SpecLoadingTests: XCTestCase {
try expect(target.type) == .framework // uses value
try expect(target.platform) == .iOS // uses latest value
try expect(target.deploymentTarget) == Version("1.2.0") // keeps value
try expect(target.sources) == ["replacedSource", "templateSource", "targetSource"] // merges array in order
try expect(target.sources) == ["replacedSource", "templateSource", "Sources/Framework", "targetSource"] // merges array in order and replace ${target_name}
try expect(target.configFiles["debug"]) == "Configs/Framework/debug.xcconfig" // replaces $target_name
try expect(target.configFiles["release"]) == "Configs/Framework/release.xcconfig" // replaces ${target_name}
}
Expand Down

0 comments on commit c2f9ff2

Please sign in to comment.