Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dfe7f28

Browse files
Scheme config variants wrong assignment for similar config names (yonaskolb#976)
* Failing test for yonaskolb#975 * fixes yonaskolb#975 * chore: refactor to properly select a config from a collection with specific variant and config type chore: updated changelog * fix: lowercase compare on config variant names * fix CI * fix missing scheme for CI * fix schemes for CI * Update CHANGELOG.md Co-authored-by: Yonas Kolb <[email protected]> * Update Sources/ProjectSpec/Config.swift Co-authored-by: Yonas Kolb <[email protected]> * - fix compilation issue - duplicated test for config variant name (uppercase/lowercase) Co-authored-by: Yonas Kolb <[email protected]>
1 parent 3a193ea commit dfe7f28

File tree

6 files changed

+66
-26
lines changed

6 files changed

+66
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858

5959
[Commits](https://github.com/yonaskolb/XcodeGen/compare/2.18.0...2.19.0)
6060

61+
#### Fixed
62+
- Lookup scheme config variants by whole words, fixing incorrect assignment in names that contain subtrings of each other (eg PreProd and Prod) [#976](https://github.com/yonaskolb/XcodeGen/pull/976) @stefanomondino
63+
6164
## 2.18.0
6265

6366
#### Added

Sources/ProjectSpec/Config.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,34 @@ public struct Config: Hashable {
1010
self.type = type
1111
}
1212

13-
public static var defaultConfigs: [Config] = [Config(name: "Debug", type: .debug), Config(name: "Release", type: .release)]
13+
public static var defaultConfigs: [Config] = [Config(name: ConfigType.debug.name, type: .debug), Config(name: ConfigType.release.name, type: .release)]
1414
}
1515

1616
public enum ConfigType: String {
1717
case debug
1818
case release
19+
20+
public var name: String {
21+
rawValue.prefix(1).uppercased() + rawValue.dropFirst()
22+
}
23+
}
24+
25+
public extension Collection where Element == Config {
26+
func first(including configVariant: String, for type: ConfigType) -> Config? {
27+
first(where: { $0.type == type && $0.name.variantName(for: $0.type) == configVariant })
28+
}
29+
}
30+
31+
private extension String {
32+
func variantName(for configType: ConfigType?) -> String {
33+
self.components(separatedBy: " ")
34+
.compactMap { component in
35+
if component.lowercased() == (configType?.name.lowercased() ?? "") {
36+
return nil
37+
}
38+
return component
39+
}
40+
.joined(separator: " ")
41+
.trimmingCharacters(in: CharacterSet.whitespaces)
42+
}
1943
}

Sources/ProjectSpec/SpecValidation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ extension Project {
9090
}
9191

9292
if let scheme = target.scheme {
93-
93+
9494
for configVariant in scheme.configVariants {
95-
if !configs.contains(where: { $0.name.contains(configVariant) && $0.type == .debug }) {
95+
if configs.first(including: configVariant, for: .debug) == nil {
9696
errors.append(.invalidTargetSchemeConfigVariant(
9797
target: target.name,
9898
configVariant: configVariant,
9999
configType: .debug
100100
))
101101
}
102-
if !configs.contains(where: { $0.name.contains(configVariant) && $0.type == .release }) {
102+
if configs.first(including: configVariant, for: .release) == nil {
103103
errors.append(.invalidTargetSchemeConfigVariant(
104104
target: target.name,
105105
configVariant: configVariant,

Sources/XcodeGenKit/SchemeGenerator.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ public class SchemeGenerator {
7070
for configVariant in targetScheme.configVariants {
7171

7272
let schemeName = "\(target.name) \(configVariant)"
73-
73+
7474
let debugConfig = project.configs
75-
.first { $0.type == .debug && $0.name.contains(configVariant) }!
75+
.first(including: configVariant, for: .debug)!
76+
7677
let releaseConfig = project.configs
77-
.first { $0.type == .release && $0.name.contains(configVariant) }!
78-
78+
.first(including: configVariant, for: .release)!
79+
7980
let scheme = Scheme(
8081
name: schemeName,
8182
target: target,

Tests/PerformanceTests/TestProject.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ extension Project {
116116
Config(name: "Release Test", type: .release),
117117
Config(name: "Debug Staging", type: .debug),
118118
Config(name: "Release Staging", type: .release),
119-
Config(name: "Debug Production", type: .debug),
120-
Config(name: "Release Production", type: .release),
119+
Config(name: "Debug Prod", type: .debug),
120+
Config(name: "Release Prod", type: .release),
121121
],
122122
targets: targets,
123123
aggregateTargets: [],

Tests/XcodeGenKitTests/SchemeGeneratorTests.swift

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -193,32 +193,44 @@ class SchemeGeneratorTests: XCTestCase {
193193
}
194194

195195
$0.it("generates target schemes from config variant") {
196-
let configVariants = ["Test", "Production"]
196+
let configVariants = ["Test", "PreProd", "Prod"]
197197
var target = app
198198
target.scheme = TargetScheme(configVariants: configVariants)
199+
200+
// Including here a double test for custom upper/lowercase in config types
199201
let configs: [Config] = [
200202
Config(name: "Test Debug", type: .debug),
201-
Config(name: "Production Debug", type: .debug),
203+
Config(name: "PreProd debug", type: .debug),
204+
Config(name: "Prod Debug", type: .debug),
202205
Config(name: "Test Release", type: .release),
203-
Config(name: "Production Release", type: .release),
206+
Config(name: "PreProd release", type: .release),
207+
Config(name: "Prod Release", type: .release),
204208
]
205209

206210
let project = Project(name: "test", configs: configs, targets: [target, framework])
207211
let xcodeProject = try project.generateXcodeProject()
208212

209-
try expect(xcodeProject.sharedData?.schemes.count) == 2
210-
211-
let xcscheme = try unwrap(xcodeProject.sharedData?.schemes
212-
.first(where: { $0.name == "\(target.name) Test" }))
213-
let buildActionEntry = try unwrap(xcscheme.buildAction?.buildActionEntries.first)
214-
215-
try expect(buildActionEntry.buildableReference.blueprintIdentifier.count > 0) == true
216-
217-
try expect(xcscheme.launchAction?.buildConfiguration) == "Test Debug"
218-
try expect(xcscheme.testAction?.buildConfiguration) == "Test Debug"
219-
try expect(xcscheme.profileAction?.buildConfiguration) == "Test Release"
220-
try expect(xcscheme.analyzeAction?.buildConfiguration) == "Test Debug"
221-
try expect(xcscheme.archiveAction?.buildConfiguration) == "Test Release"
213+
try expect(xcodeProject.sharedData?.schemes.count) == 3
214+
try configVariants.forEach { variantName in
215+
let xcscheme = try unwrap(xcodeProject.sharedData?.schemes
216+
.first(where: { $0.name == "\(target.name) \(variantName)" }))
217+
let buildActionEntry = try unwrap(xcscheme.buildAction?.buildActionEntries.first)
218+
219+
try expect(buildActionEntry.buildableReference.blueprintIdentifier.count > 0) == true
220+
if variantName == "PreProd" {
221+
try expect(xcscheme.launchAction?.buildConfiguration) == "\(variantName) debug"
222+
try expect(xcscheme.testAction?.buildConfiguration) == "\(variantName) debug"
223+
try expect(xcscheme.profileAction?.buildConfiguration) == "\(variantName) release"
224+
try expect(xcscheme.analyzeAction?.buildConfiguration) == "\(variantName) debug"
225+
try expect(xcscheme.archiveAction?.buildConfiguration) == "\(variantName) release"
226+
} else {
227+
try expect(xcscheme.launchAction?.buildConfiguration) == "\(variantName) Debug"
228+
try expect(xcscheme.testAction?.buildConfiguration) == "\(variantName) Debug"
229+
try expect(xcscheme.profileAction?.buildConfiguration) == "\(variantName) Release"
230+
try expect(xcscheme.analyzeAction?.buildConfiguration) == "\(variantName) Debug"
231+
try expect(xcscheme.archiveAction?.buildConfiguration) == "\(variantName) Release"
232+
}
233+
}
222234
}
223235

224236
$0.it("generates environment variables for target schemes") {

0 commit comments

Comments
 (0)