From 177dff2bd851925dda862a36126004f8002fbe45 Mon Sep 17 00:00:00 2001 From: Brian Kelley <bkelley@microsoft.com> Date: Fri, 2 Mar 2018 11:38:51 -0800 Subject: [PATCH] Fix Writing of showEnvVarsInLog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Xcode writes showEnvVarsInLog to the pbxproj file if it’s set to false. If it’s true it’s omitted. Update xcproj to use the same behavior to eliminate diffs when round tripping Xcode projects. This requires defaulting to true when decoding the project file if the key is omitted. I also updated the default parameter in the initializer to true for good measure. --- CHANGELOG.md | 1 + Sources/xcproj/PBXShellScriptBuildPhase.swift | 9 ++++--- .../PBXShellScriptBuildPhaseSpec.swift | 24 ++++++++++++++++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e096a830..10ea76adc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Fixed - `PBXObject.isEqual(to:)` overrides correctly call super https://github.com/xcodeswift/xcproj/pull/239 by @briantkelley - `PBXAggregateTarget` does not write `buildRules` https://github.com/xcodeswift/xcproj/pull/241 by @briantkelley +- Writes showEnvVarsInLog only when false https://github.com/xcodeswift/xcproj/pull/240 by @briantkelley ## 4.1.0 diff --git a/Sources/xcproj/PBXShellScriptBuildPhase.swift b/Sources/xcproj/PBXShellScriptBuildPhase.swift index 8c45bc885..b6db6e223 100644 --- a/Sources/xcproj/PBXShellScriptBuildPhase.swift +++ b/Sources/xcproj/PBXShellScriptBuildPhase.swift @@ -46,7 +46,7 @@ final public class PBXShellScriptBuildPhase: PBXBuildPhase { shellScript: String? = nil, buildActionMask: UInt = defaultBuildActionMask, runOnlyForDeploymentPostprocessing: Bool = false, - showEnvVarsInLog: Bool = false) { + showEnvVarsInLog: Bool = true) { self.name = name self.inputPaths = inputPaths self.outputPaths = outputPaths @@ -76,7 +76,7 @@ final public class PBXShellScriptBuildPhase: PBXBuildPhase { self.outputPaths = (try container.decodeIfPresent(.outputPaths)) ?? [] self.shellPath = try container.decodeIfPresent(.shellPath) self.shellScript = try container.decodeIfPresent(.shellScript) - self.showEnvVarsInLog = try container.decodeIntBool(.showEnvVarsInLog) + self.showEnvVarsInLog = try container.decodeIntBoolIfPresent(.showEnvVarsInLog) ?? true try super.init(from: decoder) } @@ -117,7 +117,10 @@ extension PBXShellScriptBuildPhase: PlistSerializable { if let shellScript = shellScript { dictionary["shellScript"] = .string(CommentedString(shellScript)) } - dictionary["showEnvVarsInLog"] = .string(CommentedString("\(showEnvVarsInLog.int)")) + if !showEnvVarsInLog { + // Xcode only writes this key if it's set to false; default is true and is omitted + dictionary["showEnvVarsInLog"] = .string(CommentedString("\(showEnvVarsInLog.int)")) + } return (key: CommentedString(reference, comment: self.name ?? "ShellScript"), value: .dictionary(dictionary)) } diff --git a/Tests/xcprojTests/PBXShellScriptBuildPhaseSpec.swift b/Tests/xcprojTests/PBXShellScriptBuildPhaseSpec.swift index 21a233b1c..2f3204d3e 100644 --- a/Tests/xcprojTests/PBXShellScriptBuildPhaseSpec.swift +++ b/Tests/xcprojTests/PBXShellScriptBuildPhaseSpec.swift @@ -1,6 +1,6 @@ import Foundation import XCTest -import xcproj +@testable import xcproj final class PBXShellScriptBuildPhaseSpec: XCTestCase { @@ -30,6 +30,28 @@ final class PBXShellScriptBuildPhaseSpec: XCTestCase { XCTAssertEqual(one, another) } + func test_write_showEnvVarsInLog() { + let show = PBXShellScriptBuildPhase(showEnvVarsInLog: true) + let doNotShow = PBXShellScriptBuildPhase(showEnvVarsInLog: false) + + let proj = PBXProj(rootObject: "rootObject", + objectVersion: 48, + objects: ["show": show, + "doNotShow": doNotShow]) + + let (_, showPlistValue) = show.plistKeyAndValue(proj: proj, reference: "ref") + let (_, doNotShowPlistValue) = doNotShow.plistKeyAndValue(proj: proj, reference: "ref") + + if case PlistValue.dictionary(let showDictionary) = showPlistValue, + case PlistValue.dictionary(let doNotShowDictionary) = doNotShowPlistValue { + + XCTAssertNil(showDictionary["showEnvVarsInLog"]) + XCTAssertEqual(doNotShowDictionary["showEnvVarsInLog"]?.string, "0") + } else { + XCTAssert(false) + } + } + private func testDictionary() -> [String: Any] { return [ "files": ["files"],