From ac3e14c89e51e1b15d2f33d3d0fdb9a842272d9b Mon Sep 17 00:00:00 2001 From: discostu105 Date: Sun, 21 Feb 2016 23:01:55 +0100 Subject: [PATCH] Added TargetFrameworkVersion to AssemblyInfoOptions in order to let AssemblyInfoFileGenerator if attributes are compatible with framework version. E.g. TargetFrameworkAttribute is not .NET 2.0 compatible. fixes #1480 --- .../AssemblyInfoFileGenerator.cs | 12 ++++++++---- .../AssemblyInfoOptions.cs | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.DotNet.Compiler.Common/AssemblyInfoFileGenerator.cs b/src/Microsoft.DotNet.Compiler.Common/AssemblyInfoFileGenerator.cs index ab24413fb..e93561d65 100644 --- a/src/Microsoft.DotNet.Compiler.Common/AssemblyInfoFileGenerator.cs +++ b/src/Microsoft.DotNet.Compiler.Common/AssemblyInfoFileGenerator.cs @@ -59,8 +59,7 @@ namespace Microsoft.DotNet.Cli.Compiler.Common private static Dictionary GetProjectAttributes(AssemblyInfoOptions metadata) { - return new Dictionary() - { + var attributes = new Dictionary() { [typeof(AssemblyTitleAttribute)] = EscapeCharacters(metadata.Title), [typeof(AssemblyDescriptionAttribute)] = EscapeCharacters(metadata.Description), [typeof(AssemblyCopyrightAttribute)] = EscapeCharacters(metadata.Copyright), @@ -68,9 +67,14 @@ namespace Microsoft.DotNet.Cli.Compiler.Common [typeof(AssemblyVersionAttribute)] = EscapeCharacters(metadata.AssemblyVersion?.ToString()), [typeof(AssemblyInformationalVersionAttribute)] = EscapeCharacters(metadata.InformationalVersion), [typeof(AssemblyCultureAttribute)] = EscapeCharacters(metadata.Culture), - [typeof(NeutralResourcesLanguageAttribute)] = EscapeCharacters(metadata.NeutralLanguage), - [typeof(TargetFrameworkAttribute)] = EscapeCharacters(metadata.TargetFramework) + [typeof(NeutralResourcesLanguageAttribute)] = EscapeCharacters(metadata.NeutralLanguage) }; + + // only .net 4.0+ compatible + if (metadata.TargetFrameworkVersion == null || metadata.TargetFrameworkVersion >= new Version(4, 0)) { + attributes[typeof(TargetFrameworkAttribute)] = EscapeCharacters(metadata.TargetFramework); + }; + return attributes; } private static bool IsSameAttribute(Type attributeType, AttributeSyntax attributeSyntax) diff --git a/src/Microsoft.DotNet.Compiler.Common/AssemblyInfoOptions.cs b/src/Microsoft.DotNet.Compiler.Common/AssemblyInfoOptions.cs index 0fe593c8d..5b66b520f 100644 --- a/src/Microsoft.DotNet.Compiler.Common/AssemblyInfoOptions.cs +++ b/src/Microsoft.DotNet.Compiler.Common/AssemblyInfoOptions.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; using Microsoft.DotNet.ProjectModel; using System.Collections.Generic; using System.CommandLine; @@ -28,6 +29,8 @@ namespace Microsoft.DotNet.Cli.Compiler.Common private const string TargetFrameworkOptionName = "target-framework"; + private const string TargetFrameworkVersionOptionName = "target-framework-version"; + public string Title { get; set; } public string Description { get; set; } @@ -46,6 +49,8 @@ namespace Microsoft.DotNet.Cli.Compiler.Common public string TargetFramework { get; set; } + public Version TargetFrameworkVersion { get; set; } + public static AssemblyInfoOptions CreateForProject(ProjectContext context) { var project = context.ProjectFile; @@ -69,7 +74,8 @@ namespace Microsoft.DotNet.Cli.Compiler.Common Description = project.Description, Title = project.Title, NeutralLanguage = project.Language, - TargetFramework = targetFramework.DotNetFrameworkName + TargetFramework = targetFramework.DotNetFrameworkName, + TargetFrameworkVersion = targetFramework.Version }; } @@ -84,6 +90,7 @@ namespace Microsoft.DotNet.Cli.Compiler.Common string culture = null; string neutralCulture = null; string targetFramework = null; + string targetFrameworkVersion = null; syntax.DefineOption(AssemblyVersionOptionName, ref version, UnescapeNewlines, "Assembly version"); @@ -103,6 +110,8 @@ namespace Microsoft.DotNet.Cli.Compiler.Common syntax.DefineOption(TargetFrameworkOptionName, ref targetFramework, UnescapeNewlines, "Assembly target framework"); + syntax.DefineOption(TargetFrameworkVersionOptionName, ref targetFrameworkVersion, UnescapeNewlines, "Assembly target framework version"); + return new AssemblyInfoOptions() { AssemblyFileVersion = fileVersion, @@ -112,7 +121,8 @@ namespace Microsoft.DotNet.Cli.Compiler.Common Description = description, InformationalVersion = informationalVersion, Title = title, - TargetFramework = targetFramework + TargetFramework = targetFramework, + TargetFrameworkVersion = new Version(targetFrameworkVersion) }; } @@ -156,6 +166,10 @@ namespace Microsoft.DotNet.Cli.Compiler.Common { options.Add(FormatOption(TargetFrameworkOptionName, assemblyInfoOptions.TargetFramework)); } + if (assemblyInfoOptions.TargetFrameworkVersion != null) + { + options.Add(FormatOption(TargetFrameworkVersionOptionName, assemblyInfoOptions.TargetFrameworkVersion.ToString())); + } return options; }