From 04750598d779d3ab0e0b367e0319be6f72b2aaad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Pin=CC=83era?= Date: Mon, 24 Jun 2019 18:10:46 +0200 Subject: [PATCH] Handle settings that are arrays --- .../xcodeproj/Extensions/Array+Extras.swift | 15 +++++++++++ .../Configuration/XCBuildConfiguration.swift | 22 +++++++++------ .../XCBuildConfigurationTests.swift | 27 ++++++++++++++----- 3 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 Sources/xcodeproj/Extensions/Array+Extras.swift diff --git a/Sources/xcodeproj/Extensions/Array+Extras.swift b/Sources/xcodeproj/Extensions/Array+Extras.swift new file mode 100644 index 000000000..52b7f89c4 --- /dev/null +++ b/Sources/xcodeproj/Extensions/Array+Extras.swift @@ -0,0 +1,15 @@ +import Foundation + +extension Array where Element: Hashable { + + /// Return the array with all duplicates removed. + /// + /// i.e. `[ 1, 2, 3, 1, 2 ].uniqued() == [ 1, 2, 3 ]` + /// + /// - note: Taken from stackoverflow.com/a/46354989/3141234, as + /// per @Alexander's comment. + public func uniqued() -> [Element] { + var seen = Set() + return self.filter { seen.insert($0).inserted } + } +} diff --git a/Sources/xcodeproj/Objects/Configuration/XCBuildConfiguration.swift b/Sources/xcodeproj/Objects/Configuration/XCBuildConfiguration.swift index 9db7e039d..bd948c42f 100644 --- a/Sources/xcodeproj/Objects/Configuration/XCBuildConfiguration.swift +++ b/Sources/xcodeproj/Objects/Configuration/XCBuildConfiguration.swift @@ -74,14 +74,20 @@ public final class XCBuildConfiguration: PBXObject { /// - value: Value to be appended. public func append(setting name: String, value: String) { guard !value.isEmpty else { return } - - let existingValue = (buildSettings[name] as? String) ?? "$(inherited)" - let newValue = [existingValue, value].joined(separator: " ") - - // Remove duplicates - let newValueComponents = Set(newValue.split(separator: " ")) - - buildSettings[name] = newValueComponents.joined(separator: " ") + + let existing: Any = buildSettings[name] ?? "$(inherited)" + + switch existing { + case let string as String: + let newValue = [string, value].joined(separator: " ") + buildSettings[name] = newValue + case let array as [String]: + var newValue = array + newValue.append(value) + buildSettings[name] = newValue.uniqued() + default: + break + } } } diff --git a/Tests/xcodeprojTests/Objects/Configuration/XCBuildConfigurationTests.swift b/Tests/xcodeprojTests/Objects/Configuration/XCBuildConfigurationTests.swift index 8c979e76f..4671c4741 100644 --- a/Tests/xcodeprojTests/Objects/Configuration/XCBuildConfigurationTests.swift +++ b/Tests/xcodeprojTests/Objects/Configuration/XCBuildConfigurationTests.swift @@ -44,19 +44,32 @@ final class XCBuildConfigurationTests: XCTestCase { XCTAssertEqual(subject.buildSettings["OTHER_LDFLAGS"] as? String, "flag1 flag2") } - func test_append_removesDuplicates() { + func test_append_removesDuplicates_when_theSettingIsAnArray() { // Given let subject = XCBuildConfiguration(name: "Debug", baseConfiguration: nil, buildSettings: [ - "PRODUCT_NAME": "$(TARGET_NAME:c99extidentifier)", - ]) - + "OTHER_LDFLAGS": ["flag1", "flag2"], + ]) + // When - subject.append(setting: "PRODUCT_NAME", value: "$(TARGET_NAME:c99extidentifier)") - + subject.append(setting: "OTHER_LDFLAGS", value: "flag1") + + // Then + XCTAssertEqual(subject.buildSettings["OTHER_LDFLAGS"] as? [String], ["flag1", "flag2"]) + } + + func test_append_when_theSettingExistsAsAnArray() { + // Given + let subject = XCBuildConfiguration(name: "Debug", + baseConfiguration: nil, + buildSettings: ["OTHER_LDFLAGS": ["flag1", "flag2"]]) + + // When + subject.append(setting: "OTHER_LDFLAGS", value: "flag3") + // Then - XCTAssertEqual(subject.buildSettings["PRODUCT_NAME"] as? String, "$(TARGET_NAME:c99extidentifier)") + XCTAssertEqual(subject.buildSettings["OTHER_LDFLAGS"] as? [String], ["flag1", "flag2", "flag3"]) } private func testDictionary() -> [String: Any] {