Skip to content

Commit 4caba7b

Browse files
committed
Add support for @(AssemblyMetadata) items that turn into assembly attributes
Given that `AssemblyMetadataAttribute` is such a common assembly-level attribute, this adds support for specifying it directly via simple items, such as: ``` <AssemblyMetadata Include="Foo" Value="Bar" /> <AssemblyMetadata Include="Bar" Value="Baz" /> ``` This also avoids having to learn the `_Parameter1` and `_Parameter2` syntax in `AssemblyAttribute` elements, and is more similar to the way other higher-level properties like `AssemblyTitle` or `Product` are also turned into assembly attributes. Partially fixes #3166 The feature can be disabled by setting `$(GenerateAssemblyMetadataAttributes)` to `false`.
1 parent b07b215 commit 4caba7b

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.GenerateAssemblyInfo.targets

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Copyright (c) .NET Foundation. All rights reserved.
3333
<GenerateAssemblyTitleAttribute Condition="'$(GenerateAssemblyTitleAttribute)' == ''">true</GenerateAssemblyTitleAttribute>
3434
<GenerateAssemblyVersionAttribute Condition="'$(GenerateAssemblyVersionAttribute)' == ''">true</GenerateAssemblyVersionAttribute>
3535
<GenerateNeutralResourcesLanguageAttribute Condition="'$(GenerateNeutralResourcesLanguageAttribute)' == ''">true</GenerateNeutralResourcesLanguageAttribute>
36+
<GenerateAssemblyMetadataAttributes Condition="'$(GenerateAssemblyMetadataAttributes)' == ''">true</GenerateAssemblyMetadataAttributes>
3637
<IncludeSourceRevisionInInformationalVersion Condition="'$(IncludeSourceRevisionInInformationalVersion)' == ''">true</IncludeSourceRevisionInInformationalVersion>
3738
</PropertyGroup>
3839

@@ -94,6 +95,10 @@ Copyright (c) .NET Foundation. All rights reserved.
9495
<AssemblyAttribute Include="System.Resources.NeutralResourcesLanguageAttribute" Condition="'$(NeutralLanguage)' != '' and '$(GenerateNeutralResourcesLanguageAttribute)' == 'true'">
9596
<_Parameter1>$(NeutralLanguage)</_Parameter1>
9697
</AssemblyAttribute>
98+
<AssemblyAttribute Include="System.Reflection.AssemblyMetadata" Condition="%(AssemblyMetadata.Identity) != '' and '$(GenerateAssemblyMetadataAttributes)' == 'true'">
99+
<_Parameter1>%(AssemblyMetadata.Identity)</_Parameter1>
100+
<_Parameter2>%(AssemblyMetadata.Value)</_Parameter2>
101+
</AssemblyAttribute>
97102
</ItemGroup>
98103
</Target>
99104

src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToControlGeneratedAssemblyInfo.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,5 +362,64 @@ BuildCommand BuildProject(string buildNumber)
362362
return command;
363363
}
364364
}
365+
366+
[Fact]
367+
public void It_includes_assembly_metadata()
368+
{
369+
var testAsset = _testAssetsManager
370+
.CopyTestAsset("HelloWorld")
371+
.WithSource()
372+
.WithTargetFramework("netstandard2.0")
373+
.WithProjectChanges((path, project) =>
374+
{
375+
var ns = project.Root.Name.Namespace;
376+
377+
project.Root.Add(
378+
new XElement(ns + "ItemGroup",
379+
new XElement(ns + "AssemblyMetadata",
380+
new XAttribute("Include", "MetadataKey"),
381+
new XAttribute("Value", "MetadataValue"))));
382+
});
383+
384+
new RestoreCommand(Log, testAsset.TestRoot).Execute().Should().Pass();
385+
386+
var buildCommand = new BuildCommand(Log, testAsset.TestRoot);
387+
buildCommand.Execute().Should().Pass();
388+
389+
var assemblyPath = Path.Combine(buildCommand.GetOutputDirectory("netstandard2.0").FullName, "HelloWorld.dll");
390+
var info = AssemblyInfo.Get(assemblyPath);
391+
392+
AssemblyInfo.Get(assemblyPath)["AssemblyMetadataAttribute"].Should().Be("MetadataKey:MetadataValue");
393+
}
394+
395+
[Fact]
396+
public void It_respects_out_out_of_assembly_metadata()
397+
{
398+
var testAsset = _testAssetsManager
399+
.CopyTestAsset("HelloWorld")
400+
.WithSource()
401+
.WithTargetFramework("netstandard2.0")
402+
.WithProjectChanges((path, project) =>
403+
{
404+
var ns = project.Root.Name.Namespace;
405+
406+
project.Root.Add(
407+
new XElement(ns + "PropertyGroup",
408+
new XElement(ns + "GenerateAssemblyMetadataAttributes", "false")),
409+
new XElement(ns + "ItemGroup",
410+
new XElement(ns + "AssemblyMetadata",
411+
new XAttribute("Include", "MetadataKey"),
412+
new XAttribute("Value", "MetadataValue"))));
413+
});
414+
415+
new RestoreCommand(Log, testAsset.TestRoot).Execute().Should().Pass();
416+
417+
var buildCommand = new BuildCommand(Log, testAsset.TestRoot);
418+
buildCommand.Execute().Should().Pass();
419+
420+
var assemblyPath = Path.Combine(buildCommand.GetOutputDirectory("netstandard2.0").FullName, "HelloWorld.dll");
421+
422+
Assert.False(AssemblyInfo.Get(assemblyPath).ContainsKey("AssemblyMetadataAttribute"));
423+
}
365424
}
366425
}

0 commit comments

Comments
 (0)