From 338b8ff5530765a03c04db08b41a14eb561df2ac Mon Sep 17 00:00:00 2001 From: Alistair Chapman Date: Sat, 26 Oct 2019 23:40:42 +1000 Subject: [PATCH] Add new UseRawValue parameter to skip attribute value quoting --- .../Properties/AssemblyInfoCreatorTests.cs | 15 +++++++++++++++ .../Project/Properties/AssemblyInfoCreatorData.cs | 12 +++++++----- .../Properties/AssemblyInfoCustomAttribute.cs | 8 ++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Cake.Common.Tests/Unit/Solution/Project/Properties/AssemblyInfoCreatorTests.cs b/src/Cake.Common.Tests/Unit/Solution/Project/Properties/AssemblyInfoCreatorTests.cs index 2b66e294d4..8c7ca3530c 100644 --- a/src/Cake.Common.Tests/Unit/Solution/Project/Properties/AssemblyInfoCreatorTests.cs +++ b/src/Cake.Common.Tests/Unit/Solution/Project/Properties/AssemblyInfoCreatorTests.cs @@ -332,6 +332,21 @@ public void Should_Add_Configuration_Attribute_If_Set() Assert.Contains("[assembly: AssemblyConfiguration(\"TheConfiguration\")]", result); } + [Fact] + public void Should_Add_CustomAttributes_If_Set_With_Raw_Value() + { + // Given + var fixture = new AssemblyInfoFixture(); + fixture.Settings.CustomAttributes = new Collection { new AssemblyInfoCustomAttribute { Name = "TestAttribute", NameSpace = "Test.NameSpace", Value = "RawTestValue", UseRawValue = true } }; + + // When + var result = fixture.CreateAndReturnContent(); + + // Then + Assert.Contains("using Test.NameSpace;", result); + Assert.Contains("[assembly: TestAttribute(RawTestValue)]", result); + } + [Fact] public void Should_Add_CustomAttributes_If_Set() { diff --git a/src/Cake.Common/Solution/Project/Properties/AssemblyInfoCreatorData.cs b/src/Cake.Common/Solution/Project/Properties/AssemblyInfoCreatorData.cs index af3a7678ba..ae9ea91319 100644 --- a/src/Cake.Common/Solution/Project/Properties/AssemblyInfoCreatorData.cs +++ b/src/Cake.Common/Solution/Project/Properties/AssemblyInfoCreatorData.cs @@ -72,7 +72,7 @@ public AssemblyInfoCreatorData(AssemblyInfoSettings settings, bool isVisualBasic { foreach (var item in settings.CustomAttributes.Where(item => item != null)) { - AddCustomAttribute(item.Name, item.NameSpace, item.Value); + AddCustomAttribute(item.Name, item.NameSpace, item.Value, item.UseRawValue); } } if (settings.MetaDataAttributes != null) @@ -100,14 +100,14 @@ private void AddAttribute(string name, string @namespace, string value) } } - private void AddCustomAttribute(string name, string @namespace, object value) + private void AddCustomAttribute(string name, string @namespace, object value, bool isRawValue) { - var attributeValue = AttributeValueToString(value); + var attributeValue = AttributeValueToString(value, isRawValue); AddAttributeCore(CustomAttributes, name, @namespace, attributeValue); } - private string AttributeValueToString(object value) + private string AttributeValueToString(object value, bool isRawValue) { switch (value) { @@ -123,7 +123,9 @@ private string AttributeValueToString(object value) { return stringValue == string.Empty ? string.Empty - : string.Concat("\"", value, "\""); + : isRawValue + ? stringValue + : string.Concat("\"", stringValue.Replace("\"", "\\\""), "\""); } default: { diff --git a/src/Cake.Common/Solution/Project/Properties/AssemblyInfoCustomAttribute.cs b/src/Cake.Common/Solution/Project/Properties/AssemblyInfoCustomAttribute.cs index 368722111f..46fa914211 100644 --- a/src/Cake.Common/Solution/Project/Properties/AssemblyInfoCustomAttribute.cs +++ b/src/Cake.Common/Solution/Project/Properties/AssemblyInfoCustomAttribute.cs @@ -26,5 +26,13 @@ public sealed class AssemblyInfoCustomAttribute /// /// The value for the attribute. public object Value { get; set; } + + /// + /// Gets or sets a value indicating whether the value is raw or should be quoted in the created attribute. + /// + /// + /// true if should be treated as raw; otherwise, false. + /// + public bool UseRawValue { get; set; } } } \ No newline at end of file