Skip to content

Commit

Permalink
Merge pull request #81 from nguerrera/gen-assembly-info
Browse files Browse the repository at this point in the history
Generate AssemblyInfo attributes during build
  • Loading branch information
nguerrera committed Aug 26, 2016
2 parents 2e9c193 + 512e5fd commit 09567f4
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 32 deletions.
5 changes: 5 additions & 0 deletions TestAssets/TestProjects/AppWithLibrary/TestApp/TestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<PropertyGroup>
<Version>1.2.3-beta</Version>
<Authors>Test Authors</Authors>
<Product>Test Product</Product>
<AssemblyTitle>Test AssemblyTitle</AssemblyTitle>
<Copyright>Copyright (c) Test Authors</Copyright>
<OutputType>Exe</OutputType>
<TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<PropertyGroup>
<Version>42.43.44.45-alpha</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<OutputType>Library</OutputType>
<TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
Expand Down
1 change: 1 addition & 0 deletions build/Targets/ProducesNoOutput.Settings.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<CopyOutputSymbolsToOutputDirectory>false</CopyOutputSymbolsToOutputDirectory>
<OutputType>Library</OutputType>
<GenerateDependencyFile>false</GenerateDependencyFile>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<ResolvePackageDependenciesForBuild>false</ResolvePackageDependenciesForBuild>
<NonShipping>true</NonShipping>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<TargetFrameworkVersion>v1.3</TargetFrameworkVersion>
<OutDir>$(OutDir)Tests\</OutDir>
<GenerateDependencyFile>false</GenerateDependencyFile>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<NonShipping>true</NonShipping>
<ResolvePackageDependenciesForBuild>false</ResolvePackageDependenciesForBuild>
<ResolveNuGetPackages>true</ResolveNuGetPackages>
Expand Down
45 changes: 45 additions & 0 deletions src/Tasks/Microsoft.NETCore.Build.Tasks/GetAssemblyVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Newtonsoft.Json;
using NuGet.Versioning;
using System.Diagnostics;

namespace Microsoft.DotNet.Core.Build.Tasks
{
/// <summary>
/// Determines the assembly version to use for a given semantic version.
/// </summary>
public class GetAssemblyVersion : Task
{
/// <summary>
/// The nuget version from which to get an assembly version portion.
/// </summary>
[Required]
public string NuGetVersion { get; set; }

/// <summary>
/// The assembly version (major.minor.patch.revision) portion of the nuget version.
/// </summary>
[Output]
public string AssemblyVersion { get; set; }

public override bool Execute()
{
// Using try/catch instead of TryParse so that we don't need to maintain our own error message here.
try
{
AssemblyVersion = NuGet.Versioning.NuGetVersion.Parse(NuGetVersion).Version.ToString();
return true;
}
catch (ArgumentException ex)
{
Log.LogError(ex.Message);
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
<TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v1.3</TargetFrameworkVersion>
<GenerateDependencyFile>false</GenerateDependencyFile>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<ResolvePackageDependenciesForBuild>false</ResolvePackageDependenciesForBuild>
<ResolveNuGetPackages>true</ResolveNuGetPackages>
</PropertyGroup>
<ItemGroup>
<Compile Include="GetAssemblyVersion.cs" />
<Compile Include="ITaskItemExtensions.cs" />
<Compile Include="ProjectContext.cs" />
<Compile Include="NuGetPackageResolver.cs" />
Expand All @@ -31,6 +33,9 @@
<Compile Include="RuntimeConfig.cs" />
<Compile Include="RuntimeConfigFramework.cs" />
<Compile Include="RuntimeOptions.cs" />
<None Include="build\netstandard1.0\Microsoft.NETCore.GenerateAssemblyInfo.targets">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="build\netstandard1.0\Microsoft.NETCore.Sdk.targets">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<!--
***********************************************************************************************
Microsoft.NETCore.GenerateAssemblyInfo.targets
WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
created a backup copy. Incorrect changes to this file will make it
impossible to load or build your projects from the command-line or the IDE.
Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
============================================================
GenerateAssemblyInfo
Generates assembly info source to intermediate directory
============================================================
-->
<PropertyGroup>
<GeneratedAssemblyInfoFile Condition="'$(GeneratedAssemblyInfoFile)' ==''">$(IntermediateOutputPath)$(MSBuildProjectName).AssemblyInfo$(DefaultLanguageSourceExtension)</GeneratedAssemblyInfoFile>
<GenerateAssemblyInfo Condition="'$(GenerateAssemblyInfo)' == ''">true</GenerateAssemblyInfo>
</PropertyGroup>

<!--
Note that this must run before every invocation of CoreCompile to ensure that all compiler
runs see the generated assembly info. There is at least one scenario involving Xaml
where CoreCompile is invoked without other potential hooks such as Compile or CoreBuild,
etc., so we hook directly on to CoreCompile. Furthermore, we must run *after*
PrepareForBuild to ensure that the intermediate directory has been created.
-->
<Target Name="GenerateAssemblyInfo"
BeforeTargets="CoreCompile"
DependsOnTargets="PrepareForBuild;GetAssemblyVersion;CoreGenerateAssemblyInfo"
Condition="'$(GenerateAssemblyInfo)' == 'true'"
Inputs="$(MSBuildAllProjects)"
Outputs="$(GeneratedAssemblyInfoFile)" />

<Target Name="CoreGenerateAssemblyInfo"
Condition="'$(Language)'=='VB' or '$(Language)'=='C#'">
<ItemGroup>
<AssemblyAttribute Include="System.Reflection.AssemblyCompanyAttribute" Condition="'$(Authors)' != ''">
<_Parameter1>$(Authors)</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyConfigurationAttribute" Condition="'$(Configuration)' != ''">
<_Parameter1>$(Configuration)</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyCopyrightAttribute" Condition="'$(Copyright)' != ''">
<_Parameter1>$(Copyright)</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyDescriptionAttribute" Condition="'$(Description)' != ''">
<_Parameter1>$(Description)</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyFileVersionAttribute" Condition="'$(FileVersion)' != ''">
<_Parameter1>$(FileVersion)</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyInformationalVersionAttribute" Condition="'$(Version)' != ''">
<_Parameter1>$(Version)</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyProductAttribute" Condition="'$(Product)' != ''">
<_Parameter1>$(Product)</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyTitleAttribute" Condition="'$(AssemblyTitle)' != ''">
<_Parameter1>$(AssemblyTitle)</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyVersionAttribute" Condition="'$(AssemblyVersion)' != ''">
<_Parameter1>$(AssemblyVersion)</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Resources.NeutralResourcesLanguage" Condition="'$(NeutralLanguage)' != ''">
<_Parameter1>$(NeutralLanguage)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<WriteCodeFragment AssemblyAttributes="@(AssemblyAttribute)" Language="$(Language)" OutputFile="$(GeneratedAssemblyInfoFile)">
<Output TaskParameter="OutputFile" ItemName="Compile" />
<Output TaskParameter="OutputFile" ItemName="FileWrites" />
</WriteCodeFragment>
</Target>


<!--
==================================================================
GetAssemblyVersion
Parses the nuget package version set in $(Version) and returns
the implied $(AssemblyVersion) and $(FileVersion).
e.g.:
<Version>1.2.3-beta.4</Version>
implies:
<AssemblyVersion>1.2.3</AssemblyVersion>
<FileVersion>1.2.3</FileVersion>
Note that if $(AssemblyVersion) or $(FileVersion) are are already set, it
is considered an override of the default inference from $(Version) and they
are left unchanged by this target.
==================================================================
-->
<Target Name="GetAssemblyVersion">
<GetAssemblyVersion Condition="'$(AssemblyVersion)' == ''" NuGetVersion="$(Version)">
<Output TaskParameter="AssemblyVersion" PropertyName="AssemblyVersion" />
</GetAssemblyVersion>

<PropertyGroup>
<FileVersion Condition="'$(FileVersion)' == ''">$(AssemblyVersion)</FileVersion>
</PropertyGroup>
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Copyright (c) .NET Foundation. All rights reserved.

<UsingTask TaskName="GenerateDepsFile" AssemblyFile="$(MicrosoftNETCoreBuildTasksAssembly)" />
<UsingTask TaskName="GenerateRuntimeConfigurationFiles" AssemblyFile="$(MicrosoftNETCoreBuildTasksAssembly)" />
<UsingTask TaskName="GetAssemblyVersion" AssemblyFile="$(MicrosoftNETCoreBuildTasksAssembly)" />

<PropertyGroup>
<!-- We don't use any of MSBuild's resolution logic for resolving the framework, so just set these two
Expand All @@ -42,6 +43,10 @@ Copyright (c) .NET Foundation. All rights reserved.
<VersionSuffix Condition=" '$(VersionSuffix)' == '' "></VersionSuffix>
<Version Condition=" '$(Version)' == '' and '$(VersionSuffix)' != '' ">$(VersionPrefix)-$(VersionSuffix)</Version>
<Version Condition=" '$(Version)' == '' ">$(VersionPrefix)</Version>

<AssemblyTitle Condition="'$(AssemblyTitle)' == ''">$(AssemblyName)</AssemblyTitle>
<Product Condition="'$(Product)' == ''">$(AssemblyName)</Product>
<NeutralLanguage Condition="'$(NeutralLanguage)' == ''">en</NeutralLanguage>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -130,6 +135,7 @@ Copyright (c) .NET Foundation. All rights reserved.
</ItemGroup>
</Target>

<Import Project="$(MSBuildThisFileDirectory)Microsoft.NETCore.GenerateAssemblyInfo.targets" Condition="Exists('$(MSBuildThisFileDirectory)Microsoft.NETCore.GenerateAssemblyInfo.targets')" />
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NETCore.Publish.targets" Condition="Exists('$(MSBuildThisFileDirectory)Microsoft.NETCore.Publish.targets')" />

</Project>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<SharedTemplateFiles Include="..\Common\AssemblyInfo.cs">
<Link>AssemblyInfo.cs</Link>
<Type>Project</Type>
<TargetPath>AssemblyInfo.cs</TargetPath>
</SharedTemplateFiles>
<ProjectJsonTemplate Include="project-json-template.json">
<TargetPath>project.json.template</TargetPath>
</ProjectJsonTemplate>
Expand All @@ -69,7 +64,6 @@
<ItemGroup>
<None Include="project.json" />
</ItemGroup>

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(ProjectRootDir)build\Targets\Templates.Imports.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
</TemplateData>
<TemplateContent>
<Project File="ProjectTemplate.csproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="true" TargetFileName="Properties\AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="project.json">project.json.template</ProjectItem>
<ProjectItem ReplaceParameters="true" OpenInEditor="true">Program.cs</ProjectItem>
</Project>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.NETCore.TestFramework;
using Microsoft.NETCore.TestFramework.Assertions;
using Microsoft.NETCore.TestFramework.Commands;
using Xunit;
using static Microsoft.NETCore.TestFramework.Commands.MSBuildTest;
using FluentAssertions;

namespace Microsoft.NETCore.Publish.Tests
{
Expand Down Expand Up @@ -35,7 +38,7 @@ public void It_builds_the_project_successfully()

var outputDirectory = buildCommand.GetOutputDirectory();

outputDirectory.Should().OnlyHaveFiles(new [] {
outputDirectory.Should().OnlyHaveFiles(new[] {
"TestApp.dll",
"TestApp.pdb",
"TestApp.deps.json",
Expand All @@ -52,6 +55,32 @@ public void It_builds_the_project_successfully()
.Pass()
.And
.HaveStdOutContaining("This string came from the test library!");

var appInfo = FileVersionInfo.GetVersionInfo(Path.Combine(outputDirectory.FullName, "TestApp.dll"));
appInfo.CompanyName.Should().Be("Test Authors");
appInfo.FileVersion.Should().Be("1.2.3.0");
appInfo.FileDescription.Should().Be("Test AssemblyTitle");
appInfo.LegalCopyright.Should().Be("Copyright (c) Test Authors");
appInfo.ProductName.Should().Be("Test Product");

// This check is blocked from working on non-Windows by https://github.com/dotnet/corefx/issues/11163
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
appInfo.ProductVersion.Should().Be("1.2.3-beta");
}

var libInfo = FileVersionInfo.GetVersionInfo(Path.Combine(outputDirectory.FullName, "TestLibrary.dll"));
libInfo.CompanyName.Trim().Should().BeEmpty();
libInfo.FileVersion.Should().Be("42.43.44.45");
libInfo.FileDescription.Should().Be("TestLibrary");
libInfo.LegalCopyright.Trim().Should().BeEmpty();
libInfo.ProductName.Should().Be("TestLibrary");

// This check is blocked from working on non-Windows by https://github.com/dotnet/corefx/issues/11163
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
libInfo.ProductVersion.Should().Be("42.43.44.45-alpha");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<TargetFrameworkVersion>v1.6</TargetFrameworkVersion>
<OutDir>$(OutDir)Tests\</OutDir>
<GenerateDependencyFile>false</GenerateDependencyFile>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<NonShipping>true</NonShipping>
<ResolvePackageDependenciesForBuild>false</ResolvePackageDependenciesForBuild>
<ResolveNuGetPackages>true</ResolveNuGetPackages>
Expand Down
1 change: 1 addition & 0 deletions test/Microsoft.NETCore.Build.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"dependencies": {
"Microsoft.DotNet.Cli.Utils": "1.0.0-preview2-003121",
"FluentAssertions": "4.0.0",
"System.Diagnostics.FileVersionInfo": "4.0.0",
"xunit": "2.1.0"
},
"frameworks": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<TargetFrameworkVersion>v1.6</TargetFrameworkVersion>
<OutDir>$(OutDir)Tests\</OutDir>
<GenerateDependencyFile>false</GenerateDependencyFile>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<NonShipping>true</NonShipping>
<ResolvePackageDependenciesForBuild>false</ResolvePackageDependenciesForBuild>
<ResolveNuGetPackages>true</ResolveNuGetPackages>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<TargetFrameworkVersion>v1.6</TargetFrameworkVersion>
<OutDir>$(OutDir)Tests\</OutDir>
<GenerateDependencyFile>false</GenerateDependencyFile>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<NonShipping>true</NonShipping>
<ResolvePackageDependenciesForBuild>false</ResolvePackageDependenciesForBuild>
<ResolveNuGetPackages>true</ResolveNuGetPackages>
Expand Down

0 comments on commit 09567f4

Please sign in to comment.