From 5ebd0cd421c9636af867c2bb9eac92d401bee79a Mon Sep 17 00:00:00 2001 From: Nick Guerrera Date: Mon, 22 Aug 2016 16:50:15 -0700 Subject: [PATCH 1/6] Generate AssemblyInfo attributes during build --- build/Targets/ProducesNoOutput.Settings.props | 1 + ...osoft.NETCore.Build.Tasks.UnitTests.csproj | 1 + .../GetAssemblyVersion.cs | 49 ++++++++ .../Microsoft.NETCore.Build.Tasks.csproj | 5 + ...osoft.NETCore.GenerateAssemblyInfo.targets | 110 ++++++++++++++++++ .../Microsoft.NETCore.Sdk.targets | 2 + .../Microsoft.NETCore.Build.Tests.csproj | 1 + .../Microsoft.NETCore.Publish.Tests.csproj | 1 + .../Microsoft.NETCore.TestFramework.csproj | 1 + 9 files changed, 171 insertions(+) create mode 100644 src/Tasks/Microsoft.NETCore.Build.Tasks/GetAssemblyVersion.cs create mode 100644 src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.GenerateAssemblyInfo.targets diff --git a/build/Targets/ProducesNoOutput.Settings.props b/build/Targets/ProducesNoOutput.Settings.props index 796b9f72d040..4f401feabef8 100644 --- a/build/Targets/ProducesNoOutput.Settings.props +++ b/build/Targets/ProducesNoOutput.Settings.props @@ -8,6 +8,7 @@ false Library false + false false true diff --git a/src/Tasks/Microsoft.NETCore.Build.Tasks.UnitTests/Microsoft.NETCore.Build.Tasks.UnitTests.csproj b/src/Tasks/Microsoft.NETCore.Build.Tasks.UnitTests/Microsoft.NETCore.Build.Tasks.UnitTests.csproj index a7722129340c..b264e95994ff 100644 --- a/src/Tasks/Microsoft.NETCore.Build.Tasks.UnitTests/Microsoft.NETCore.Build.Tasks.UnitTests.csproj +++ b/src/Tasks/Microsoft.NETCore.Build.Tasks.UnitTests/Microsoft.NETCore.Build.Tasks.UnitTests.csproj @@ -15,6 +15,7 @@ v1.3 $(OutDir)Tests\ false + false true false true diff --git a/src/Tasks/Microsoft.NETCore.Build.Tasks/GetAssemblyVersion.cs b/src/Tasks/Microsoft.NETCore.Build.Tasks/GetAssemblyVersion.cs new file mode 100644 index 000000000000..109b16d9c1ec --- /dev/null +++ b/src/Tasks/Microsoft.NETCore.Build.Tasks/GetAssemblyVersion.cs @@ -0,0 +1,49 @@ +// 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 System.Collections.Generic; +using System.IO; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; +using NuGet.ProjectModel; +using NuGet.Versioning; + +namespace Microsoft.DotNet.Core.Build.Tasks +{ + /// + /// Determines the assembly version to use for a given semantic version. + /// + public class GetAssemblyVersion : Task + { + /// + /// The semantic version from which to get an assembly version portion. + /// + [Required] + public string SemanticVersion { get; set; } + + /// + /// The assembly version (major.minor.patch) portion of the semantic version. + /// + [Output] + public string AssemblyVersion { get; set; } + + public override bool Execute() + { + NuGetVersion version; + if (string.IsNullOrEmpty(SemanticVersion) || !NuGetVersion.TryParseStrict(SemanticVersion, out version)) + { + // TODO: Localize. Blocked by https://github.com/dotnet/sdk/issues/33 + Log.LogError($"Invalid semantic version: '{SemanticVersion}'"); + return false; + } + + AssemblyVersion = new Version(version.Major, version.Minor, version.Patch).ToString(); + return true; + } + } +} diff --git a/src/Tasks/Microsoft.NETCore.Build.Tasks/Microsoft.NETCore.Build.Tasks.csproj b/src/Tasks/Microsoft.NETCore.Build.Tasks/Microsoft.NETCore.Build.Tasks.csproj index 85709ed865c6..05d36964d5cd 100644 --- a/src/Tasks/Microsoft.NETCore.Build.Tasks/Microsoft.NETCore.Build.Tasks.csproj +++ b/src/Tasks/Microsoft.NETCore.Build.Tasks/Microsoft.NETCore.Build.Tasks.csproj @@ -14,10 +14,12 @@ .NETStandard v1.3 false + false false true + @@ -30,6 +32,9 @@ + + PreserveNewest + PreserveNewest diff --git a/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.GenerateAssemblyInfo.targets b/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.GenerateAssemblyInfo.targets new file mode 100644 index 000000000000..33ed300188c8 --- /dev/null +++ b/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.GenerateAssemblyInfo.targets @@ -0,0 +1,110 @@ + + + + + $(IntermediateOutputPath)$(MSBuildProjectName).AssemblyInfo$(DefaultLanguageSourceExtension) + true + + + + + + + + + <_Parameter1>$(Authors) + + + <_Parameter1>$(Configuration) + + + <_Parameter1>$(Copyright) + + + <_Parameter1>$(Description) + + + <_Parameter1>$(FileVersion) + + + <_Parameter1>$(Version) + + + <_Parameter1>$(Product) + + + <_Parameter1>$(AssemblyName) + + + <_Parameter1>$(AssemblyVersion) + + + <_Parameter1>$(NeutralLanguage) + + + + + + + + + + + + + + + + + + $(AssemblyVersion) + + + + diff --git a/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.Sdk.targets b/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.Sdk.targets index c34dcfd71f05..eaf91a7b3346 100644 --- a/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.Sdk.targets +++ b/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.Sdk.targets @@ -24,6 +24,7 @@ Copyright (c) .NET Foundation. All rights reserved. + - + From 99446a5cb3d4c7e58be45b71690c4f3873297534 Mon Sep 17 00:00:00 2001 From: Nick Guerrera Date: Wed, 24 Aug 2016 14:20:15 -0700 Subject: [PATCH 3/6] Move assembly info to project in template --- .../CSharp/.NETCore/Common/AssemblyInfo.cs | 24 ------------------- .../ConsoleApplication.csproj | 6 ----- .../ConsoleApplication.vstemplate | 1 - .../ConsoleApplication/ProjectTemplate.csproj | 5 ++++ 4 files changed, 5 insertions(+), 31 deletions(-) delete mode 100644 src/Templates/ProjectTemplates/CSharp/.NETCore/Common/AssemblyInfo.cs diff --git a/src/Templates/ProjectTemplates/CSharp/.NETCore/Common/AssemblyInfo.cs b/src/Templates/ProjectTemplates/CSharp/.NETCore/Common/AssemblyInfo.cs deleted file mode 100644 index ada0f912d61b..000000000000 --- a/src/Templates/ProjectTemplates/CSharp/.NETCore/Common/AssemblyInfo.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Resources; -using System.Reflection; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("$projectname$")] -[assembly: AssemblyCompany("$registeredorganization$")] -[assembly: AssemblyProduct("$projectname$")] -[assembly: AssemblyCopyright("Copyright © $registeredorganization$")] -[assembly: NeutralResourcesLanguage("en")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ConsoleApplication.csproj b/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ConsoleApplication.csproj index 37a9dbf6d323..c4b1cbb89e21 100644 --- a/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ConsoleApplication.csproj +++ b/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ConsoleApplication.csproj @@ -45,11 +45,6 @@ TRACE - - AssemblyInfo.cs - Project - AssemblyInfo.cs - project.json.template @@ -69,7 +64,6 @@ - \ No newline at end of file diff --git a/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ConsoleApplication.vstemplate b/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ConsoleApplication.vstemplate index 7548c1741ba3..f555cf5d6ee1 100644 --- a/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ConsoleApplication.vstemplate +++ b/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ConsoleApplication.vstemplate @@ -17,7 +17,6 @@ - AssemblyInfo.cs project.json.template Program.cs diff --git a/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ProjectTemplate.csproj b/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ProjectTemplate.csproj index c22397d6466f..d860762a4bf7 100644 --- a/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ProjectTemplate.csproj +++ b/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ProjectTemplate.csproj @@ -2,6 +2,11 @@ + $registeredorganization$ + Copyright © $registeredorganization$ + $projectname$ + en + 1.0.0 Exe .NETCoreApp v1.0 From 7abc205d092d2e9731e64f1b359c83a52c8a7717 Mon Sep 17 00:00:00 2001 From: Nick Guerrera Date: Thu, 25 Aug 2016 13:50:29 -0700 Subject: [PATCH 4/6] Move defaults from template to target Also, use $(AssemblyTitle) defaulted to $(AssemblyName) instead of using $(AssemblyName) directly for AssemblyTitleAttribute Finally, don't set Authors and Copyright in template since $registeredorg$ is often blank and it clutters the new project. Just let users set authors and copyright as they see fit. This seems more in keeping with the spirit of minimal boilerplate in the new .csproj files. --- .../Microsoft.NETCore.GenerateAssemblyInfo.targets | 4 ++-- .../build/netstandard1.0/Microsoft.NETCore.Sdk.targets | 4 ++++ .../.NETCore/ConsoleApplication/ProjectTemplate.csproj | 5 ----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.GenerateAssemblyInfo.targets b/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.GenerateAssemblyInfo.targets index 0963182a885f..f7cc36283d11 100644 --- a/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.GenerateAssemblyInfo.targets +++ b/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.GenerateAssemblyInfo.targets @@ -60,8 +60,8 @@ Copyright (c) .NET Foundation. All rights reserved. <_Parameter1>$(Product) - - <_Parameter1>$(AssemblyName) + + <_Parameter1>$(AssemblyTitle) <_Parameter1>$(AssemblyVersion) diff --git a/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.Sdk.targets b/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.Sdk.targets index eaf91a7b3346..338fbca5adcd 100644 --- a/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.Sdk.targets +++ b/src/Tasks/Microsoft.NETCore.Build.Tasks/build/netstandard1.0/Microsoft.NETCore.Sdk.targets @@ -43,6 +43,10 @@ Copyright (c) .NET Foundation. All rights reserved. $(VersionPrefix)-$(VersionSuffix) $(VersionPrefix) + + $(AssemblyName) + $(AssemblyName) + en diff --git a/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ProjectTemplate.csproj b/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ProjectTemplate.csproj index d860762a4bf7..c22397d6466f 100644 --- a/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ProjectTemplate.csproj +++ b/src/Templates/ProjectTemplates/CSharp/.NETCore/ConsoleApplication/ProjectTemplate.csproj @@ -2,11 +2,6 @@ - $registeredorganization$ - Copyright © $registeredorganization$ - $projectname$ - en - 1.0.0 Exe .NETCoreApp v1.0 From db8fcbf50ce38c5d8379da031c63830c11077b28 Mon Sep 17 00:00:00 2001 From: Nick Guerrera Date: Thu, 25 Aug 2016 14:47:47 -0700 Subject: [PATCH 5/6] Add basic test coverage for generated assembly info --- .../AppWithLibrary/TestApp/TestApp.csproj | 5 +++++ .../TestLibrary/TestLibrary.csproj | 1 + .../GivenThatWeWantToBuildAnAppWithLibrary.cs | 18 ++++++++++++++++++ .../Microsoft.NETCore.Build.Tests/project.json | 1 + 4 files changed, 25 insertions(+) diff --git a/TestAssets/TestProjects/AppWithLibrary/TestApp/TestApp.csproj b/TestAssets/TestProjects/AppWithLibrary/TestApp/TestApp.csproj index e05386169919..88a9203db839 100644 --- a/TestAssets/TestProjects/AppWithLibrary/TestApp/TestApp.csproj +++ b/TestAssets/TestProjects/AppWithLibrary/TestApp/TestApp.csproj @@ -2,6 +2,11 @@ + 1.2.3-beta + Test Authors + Test Product + Test AssemblyTitle + Copyright (c) Test Authors Exe .NETCoreApp v1.0 diff --git a/TestAssets/TestProjects/AppWithLibrary/TestLibrary/TestLibrary.csproj b/TestAssets/TestProjects/AppWithLibrary/TestLibrary/TestLibrary.csproj index 59e64a769a3f..856db30d51dc 100644 --- a/TestAssets/TestProjects/AppWithLibrary/TestLibrary/TestLibrary.csproj +++ b/TestAssets/TestProjects/AppWithLibrary/TestLibrary/TestLibrary.csproj @@ -2,6 +2,7 @@ + 42.43.44.45-alpha True Library .NETStandard diff --git a/test/Microsoft.NETCore.Build.Tests/GivenThatWeWantToBuildAnAppWithLibrary.cs b/test/Microsoft.NETCore.Build.Tests/GivenThatWeWantToBuildAnAppWithLibrary.cs index 3a9d1a9a2cc6..868cc2e7c965 100644 --- a/test/Microsoft.NETCore.Build.Tests/GivenThatWeWantToBuildAnAppWithLibrary.cs +++ b/test/Microsoft.NETCore.Build.Tests/GivenThatWeWantToBuildAnAppWithLibrary.cs @@ -1,6 +1,7 @@ // 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 Microsoft.DotNet.Cli.Utils; using Microsoft.NETCore.TestFramework; @@ -8,6 +9,7 @@ using Microsoft.NETCore.TestFramework.Commands; using Xunit; using static Microsoft.NETCore.TestFramework.Commands.MSBuildTest; +using FluentAssertions; namespace Microsoft.NETCore.Publish.Tests { @@ -52,6 +54,22 @@ 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"); + 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"); + libInfo.ProductVersion.Should().Be("42.43.44.45-alpha"); } } } \ No newline at end of file diff --git a/test/Microsoft.NETCore.Build.Tests/project.json b/test/Microsoft.NETCore.Build.Tests/project.json index 5e8639cb6761..51ba1f81acfa 100644 --- a/test/Microsoft.NETCore.Build.Tests/project.json +++ b/test/Microsoft.NETCore.Build.Tests/project.json @@ -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": { From 129a84130b154df895d5336972f517a7edfc6c2f Mon Sep 17 00:00:00 2001 From: Nick Guerrera Date: Thu, 25 Aug 2016 15:30:43 -0700 Subject: [PATCH 6/6] Add OS check in test to workaround corefx bug for now --- .../GivenThatWeWantToBuildAnAppWithLibrary.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test/Microsoft.NETCore.Build.Tests/GivenThatWeWantToBuildAnAppWithLibrary.cs b/test/Microsoft.NETCore.Build.Tests/GivenThatWeWantToBuildAnAppWithLibrary.cs index 868cc2e7c965..4514b74f128e 100644 --- a/test/Microsoft.NETCore.Build.Tests/GivenThatWeWantToBuildAnAppWithLibrary.cs +++ b/test/Microsoft.NETCore.Build.Tests/GivenThatWeWantToBuildAnAppWithLibrary.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.IO; +using System.Runtime.InteropServices; using Microsoft.DotNet.Cli.Utils; using Microsoft.NETCore.TestFramework; using Microsoft.NETCore.TestFramework.Assertions; @@ -37,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", @@ -61,7 +62,12 @@ public void It_builds_the_project_successfully() appInfo.FileDescription.Should().Be("Test AssemblyTitle"); appInfo.LegalCopyright.Should().Be("Copyright (c) Test Authors"); appInfo.ProductName.Should().Be("Test Product"); - appInfo.ProductVersion.Should().Be("1.2.3-beta"); + + // 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(); @@ -69,7 +75,12 @@ public void It_builds_the_project_successfully() libInfo.FileDescription.Should().Be("TestLibrary"); libInfo.LegalCopyright.Trim().Should().BeEmpty(); libInfo.ProductName.Should().Be("TestLibrary"); - libInfo.ProductVersion.Should().Be("42.43.44.45-alpha"); + + // 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"); + } } } } \ No newline at end of file