Skip to content

Commit

Permalink
Handle settings that are arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Piñera committed Jun 24, 2019
1 parent 4bbec91 commit 0475059
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
15 changes: 15 additions & 0 deletions 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<Element>()
return self.filter { seen.insert($0).inserted }
}
}
22 changes: 14 additions & 8 deletions Sources/xcodeproj/Objects/Configuration/XCBuildConfiguration.swift
Expand Up @@ -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
}
}
}

Expand Down
Expand Up @@ -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] {
Expand Down

0 comments on commit 0475059

Please sign in to comment.