From b3ba1cb0ec19ef6e52c98c954f7ca9a94f5ce82e Mon Sep 17 00:00:00 2001 From: Yonas Kolb Date: Tue, 27 Mar 2018 15:47:23 +1100 Subject: [PATCH] automatically set DevelopmentTeam and ProvisioningStyle TargetAttributes --- Docs/ProjectSpec.md | 5 +++- Sources/ProjectSpec/Settings.swift | 6 ++-- Sources/XcodeGenKit/PBXProjGenerator.swift | 28 +++++++++++++++++-- .../ProjectGeneratorTests.swift | 4 ++- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Docs/ProjectSpec.md b/Docs/ProjectSpec.md index bdb62887..bc5e8729 100644 --- a/Docs/ProjectSpec.md +++ b/Docs/ProjectSpec.md @@ -165,7 +165,10 @@ Settings are merged in the following order: groups, base, configs. - [ ] **dependencies**: **[[Dependency](#dependency)]** - Dependencies for the target - [ ] **scheme**: **[Target Scheme](#target-scheme)** - Generated scheme with tests or config variants - [ ] **legacy**: **[Legacy Target](#legacy-target)** - When present, opt-in to make an Xcode "External Build System" legacy target instead. -- [ ] **attributes**: **[String: Any]** - This sets values in the project `TargetAttributes`. It is merged with `attributes` from the project and anything automatically added by XcodeGen, with any duplicate values being override by values specified here. This is for advanced use only. +- [ ] **attributes**: **[String: Any]** - This sets values in the project `TargetAttributes`. It is merged with `attributes` from the project and anything automatically added by XcodeGen, with any duplicate values being override by values specified here. This is for advanced use only. Properties that are already set include: + - `DevelopmentTeam`: if all configurations have the same `DEVELOPMENT_TEAM` setting + - `ProvisioningStyle`: if all configurations have the same `CODE_SIGN_STYLE` setting + - `TestTargetID`: if all configurations have the same `TEST_TARGET_NAME` setting ### Product Type diff --git a/Sources/ProjectSpec/Settings.swift b/Sources/ProjectSpec/Settings.swift index 5e5238d6..d77d3070 100644 --- a/Sources/ProjectSpec/Settings.swift +++ b/Sources/ProjectSpec/Settings.swift @@ -5,9 +5,9 @@ import xcproj public struct Settings: Equatable, JSONObjectConvertible, CustomStringConvertible { - public let buildSettings: BuildSettings - public let configSettings: [String: Settings] - public let groups: [String] + public var buildSettings: BuildSettings + public var configSettings: [String: Settings] + public var groups: [String] public init(buildSettings: BuildSettings = [:], configSettings: [String: Settings] = [:], groups: [String] = []) { self.buildSettings = buildSettings diff --git a/Sources/XcodeGenKit/PBXProjGenerator.swift b/Sources/XcodeGenKit/PBXProjGenerator.swift index fcd802ab..dd1b83db 100644 --- a/Sources/XcodeGenKit/PBXProjGenerator.swift +++ b/Sources/XcodeGenKit/PBXProjGenerator.swift @@ -243,9 +243,33 @@ public class PBXProjGenerator { } for target in spec.targets { - if !target.attributes.isEmpty, let targetObject = targetObjects[target.name] { - targetAttributes[targetObject.reference, default: [:]].merge(target.attributes) + guard let targetReference = targetObjects[target.name]?.reference else { + continue } + if !target.attributes.isEmpty { + targetAttributes[targetReference, default: [:]].merge(target.attributes) + } + + func getSingleBuildSetting(_ setting: String) -> String? { + let settings = spec.configs.flatMap { + spec.getCombinedBuildSettings(basePath: spec.basePath, target: target, config: $0)[setting] as? String + } + guard settings.count == spec.configs.count, + let firstSetting = settings.first, + settings.filter({ $0 == firstSetting}).count == settings.count else { + return nil + } + return firstSetting + } + + func setTargetAttribute(attribute: String, buildSetting: String) { + if let setting = getSingleBuildSetting(buildSetting) { + targetAttributes[targetReference, default: [:]].merge([attribute: setting]) + } + } + + setTargetAttribute(attribute: "ProvisioningStyle", buildSetting: "CODE_SIGN_STYLE") + setTargetAttribute(attribute: "DevelopmentTeam", buildSetting: "DEVELOPMENT_TEAM") } return targetAttributes.isEmpty ? nil : ["TargetAttributes": targetAttributes] diff --git a/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift b/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift index 91ecd21a..ca0c96dd 100644 --- a/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift +++ b/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift @@ -209,10 +209,11 @@ func projectGeneratorTests() { $0.it("generates target attributes") { var appTargetWithAttributes = application + appTargetWithAttributes.settings.buildSettings["DEVELOPMENT_TEAM"] = "123" appTargetWithAttributes.attributes = ["ProvisioningStyle": "Automatic"] var testTargetWithAttributes = uiTest - testTargetWithAttributes.attributes = ["ProvisioningStyle": "Manual"] + testTargetWithAttributes.settings.buildSettings["CODE_SIGN_STYLE"] = "Manual" var spec = ProjectSpec(basePath: "", name: "test", targets: [appTargetWithAttributes, framework, testTargetWithAttributes]) let pbxProject = try getPbxProj(spec) @@ -231,6 +232,7 @@ func projectGeneratorTests() { try expect(targetAttributes[uiTestTarget.reference]?["TestTargetID"] as? String) == appTarget.reference try expect(targetAttributes[uiTestTarget.reference]?["ProvisioningStyle"] as? String) == "Manual" try expect(targetAttributes[appTarget.reference]?["ProvisioningStyle"] as? String) == "Automatic" + try expect(targetAttributes[appTarget.reference]?["DevelopmentTeam"] as? String) == "123" } $0.it("generates platform version") {