Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH2637 GH2638: Update CreateAssemblyInfo alias #2639

Merged
merged 2 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,51 @@ public void Should_Add_CustomAttributes_If_Set()
Assert.Contains("[assembly: TestAttribute(\"TestValue\")]", result);
}

[Fact]
public void Should_Add_CustomAttributes_If_Set_With_Boolean_Value()
{
// Given
var fixture = new AssemblyInfoFixture();
fixture.Settings.CustomAttributes = new Collection<AssemblyInfoCustomAttribute> { new AssemblyInfoCustomAttribute { Name = "TestAttribute", NameSpace = "Test.NameSpace", Value = true } };

// When
var result = fixture.CreateAndReturnContent();

// Then
Assert.Contains("using Test.NameSpace;", result);
Assert.Contains("[assembly: TestAttribute(true)]", result);
}

[Fact]
public void Should_Add_CustomAttributes_If_Set_With_Null_Value()
{
// Given
var fixture = new AssemblyInfoFixture();
fixture.Settings.CustomAttributes = new Collection<AssemblyInfoCustomAttribute> { new AssemblyInfoCustomAttribute { Name = "TestAttribute", NameSpace = "Test.NameSpace", Value = null } };

// When
var result = fixture.CreateAndReturnContent();

// Then
Assert.Contains("using Test.NameSpace;", result);
Assert.Contains("[assembly: TestAttribute()]", result);
}

[Fact]
public void Should_Add_CustomAttributes_If_Set_With_Empty_Value()
{
// Given
var fixture = new AssemblyInfoFixture();
fixture.Settings.CustomAttributes = new Collection<AssemblyInfoCustomAttribute> { new AssemblyInfoCustomAttribute { Name = "TestAttribute", NameSpace = "Test.NameSpace", Value = string.Empty } };

// When
var result = fixture.CreateAndReturnContent();

// Then
Assert.Contains("using Test.NameSpace;", result);
Assert.Contains("[assembly: TestAttribute()]", result);
}

[Fact]
public void Should_Add_MetadataAttributes_If_Set()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand Down Expand Up @@ -222,7 +222,7 @@ public void Should_Add_ComVisible_Attribute_If_Set()

// Then
Assert.Contains("Imports System.Runtime.InteropServices", result);
Assert.Contains("<Assembly: ComVisible(true)>", result);
Assert.Contains("<Assembly: ComVisible(True)>", result);
}

[Fact]
Expand All @@ -237,7 +237,7 @@ public void Should_Add_CLSCompliant_Attribute_If_Set()

// Then
Assert.Contains("Imports System", result);
Assert.Contains("<Assembly: CLSCompliant(true)>", result);
Assert.Contains("<Assembly: CLSCompliant(True)>", result);
}

[Fact]
Expand Down Expand Up @@ -301,6 +301,51 @@ public void Should_Add_CustomAttributes_If_Set()
Assert.Contains("Imports Test.NameSpace", result);
Assert.Contains("<Assembly: TestAttribute(\"TestValue\")>", result);
}

[Fact]
public void Should_Add_CustomAttributes_If_Set_With_Boolean_Value()
{
// Given
var fixture = new AssemblyInfoFixture_VB();
fixture.Settings.CustomAttributes = new Collection<AssemblyInfoCustomAttribute> { new AssemblyInfoCustomAttribute { Name = "TestAttribute", NameSpace = "Test.NameSpace", Value = true } };

// When
var result = fixture.CreateAndReturnContent();

// Then
Assert.Contains("Imports Test.NameSpace", result);
Assert.Contains("<Assembly: TestAttribute(True)>", result);
}

[Fact]
public void Should_Add_CustomAttributes_If_Set_With_Null_Value()
{
// Given
var fixture = new AssemblyInfoFixture_VB();
fixture.Settings.CustomAttributes = new Collection<AssemblyInfoCustomAttribute> { new AssemblyInfoCustomAttribute { Name = "TestAttribute", NameSpace = "Test.NameSpace", Value = null } };

// When
var result = fixture.CreateAndReturnContent();

// Then
Assert.Contains("Imports Test.NameSpace", result);
Assert.Contains("<Assembly: TestAttribute()>", result);
}

[Fact]
public void Should_Add_CustomAttributes_If_Set_With_Empty_Value()
{
// Given
var fixture = new AssemblyInfoFixture_VB();
fixture.Settings.CustomAttributes = new Collection<AssemblyInfoCustomAttribute> { new AssemblyInfoCustomAttribute { Name = "TestAttribute", NameSpace = "Test.NameSpace", Value = string.Empty } };

// When
var result = fixture.CreateAndReturnContent();

// Then
Assert.Contains("Imports Test.NameSpace", result);
Assert.Contains("<Assembly: TestAttribute()>", result);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand Down Expand Up @@ -78,15 +78,19 @@ public void Create(FilePath outputPath, AssemblyInfoSettings settings)
string attributeWithValueFormat = CSharpAttributeWithValueFormat;
string attributeWithKeyValueFormat = CSharpAttributeWithKeyValueFormat;

var isVisualBasicAssemblyInfoFile = false;

if (outputPath.GetExtension() == ".vb")
{
isVisualBasicAssemblyInfoFile = true;
comment = VBComment;
usingFormat = VBUsingFormat;
attributeFormat = VBAttributeFormat;
attributeWithValueFormat = VBAttributeWithValueFormat;
attributeWithKeyValueFormat = VBAttributeWithKeyValueFormat;
}
var data = new AssemblyInfoCreatorData(settings);

var data = new AssemblyInfoCreatorData(settings, isVisualBasicAssemblyInfoFile);

outputPath = outputPath.MakeAbsolute(_environment);
_log.Verbose("Creating assembly info file: {0}", outputPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Cake.Core;

Expand All @@ -16,6 +17,8 @@ internal sealed class AssemblyInfoCreatorData
private readonly Dictionary<string, string> _metadatattributes;
private readonly HashSet<string> _namespaces;
private readonly HashSet<string> _internalVisibleTo;
private readonly string _trueStringValue;
private readonly string _falseStringValue;

public IDictionary<string, string> Attributes => _dictionary;

Expand All @@ -27,14 +30,17 @@ internal sealed class AssemblyInfoCreatorData

public ISet<string> InternalVisibleTo => _internalVisibleTo;

public AssemblyInfoCreatorData(AssemblyInfoSettings settings)
public AssemblyInfoCreatorData(AssemblyInfoSettings settings, bool isVisualBasicAssemblyInfoFile)
{
_dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
_customAttributes = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
_metadatattributes = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
_namespaces = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
_internalVisibleTo = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

_falseStringValue = isVisualBasicAssemblyInfoFile ? "False" : "false";
_trueStringValue = isVisualBasicAssemblyInfoFile ? "True" : "true";

// Add attributes.
AddAttribute("AssemblyTitle", "System.Reflection", settings.Title);
AddAttribute("AssemblyDescription", "System.Reflection", settings.Description);
Expand Down Expand Up @@ -82,7 +88,7 @@ private void AddAttribute(string name, string @namespace, bool? value)
{
if (value != null)
{
AddAttributeCore(Attributes, name, @namespace, value.Value ? "true" : "false");
AddAttributeCore(Attributes, name, @namespace, value.Value ? _trueStringValue : _falseStringValue);
}
}

Expand All @@ -94,11 +100,35 @@ private void AddAttribute(string name, string @namespace, string value)
}
}

private void AddCustomAttribute(string name, string @namespace, string value)
private void AddCustomAttribute(string name, string @namespace, object value)
{
if (value != null)
var attributeValue = AttributeValueToString(value);

AddAttributeCore(CustomAttributes, name, @namespace, attributeValue);
}

private string AttributeValueToString(object value)
{
switch (value)
{
AddAttributeCore(CustomAttributes, name, @namespace, string.Concat("\"", value, "\""));
case null:
{
return string.Empty;
}
case bool boolValue:
{
return boolValue ? _trueStringValue : _falseStringValue;
}
case string stringValue:
{
return stringValue == string.Empty
? string.Empty
: string.Concat("\"", value, "\"");
}
default:
{
return Convert.ToString(value, CultureInfo.InvariantCulture);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand All @@ -25,6 +25,6 @@ public sealed class AssemblyInfoCustomAttribute
/// Gets or sets the value.
/// </summary>
/// <value>The value for the attribute.</value>
public string Value { get; set; }
public object Value { get; set; }
}
}