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

Expand template variable in Array of Any #651

Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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

#### 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