Add target framework attribute

This commit is contained in:
Pavel Krymets 2015-12-15 10:32:49 -08:00
parent ce2628d527
commit caf82a96ec
3 changed files with 34 additions and 6 deletions

View file

@ -6,9 +6,9 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Resources; using System.Resources;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp;
using System.IO; using System.IO;
using System.Runtime.Versioning;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Microsoft.Dotnet.Cli.Compiler.Common namespace Microsoft.Dotnet.Cli.Compiler.Common
@ -26,7 +26,8 @@ namespace Microsoft.Dotnet.Cli.Compiler.Common
[typeof(AssemblyVersionAttribute)] = EscapeCharacters(metadata.AssemblyVersion?.ToString()), [typeof(AssemblyVersionAttribute)] = EscapeCharacters(metadata.AssemblyVersion?.ToString()),
[typeof(AssemblyInformationalVersionAttribute)] = EscapeCharacters(metadata.InformationalVersion), [typeof(AssemblyInformationalVersionAttribute)] = EscapeCharacters(metadata.InformationalVersion),
[typeof(AssemblyCultureAttribute)] = EscapeCharacters(metadata.Culture), [typeof(AssemblyCultureAttribute)] = EscapeCharacters(metadata.Culture),
[typeof(NeutralResourcesLanguageAttribute)] = EscapeCharacters(metadata.NeutralLanguage) [typeof(NeutralResourcesLanguageAttribute)] = EscapeCharacters(metadata.NeutralLanguage),
[typeof(TargetFrameworkAttribute)] = EscapeCharacters(metadata.TargetFramework)
}; };
var existingAttributes = new List<Type>(); var existingAttributes = new List<Type>();

View file

@ -5,6 +5,8 @@ using Microsoft.DotNet.ProjectModel;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.CommandLine; using System.CommandLine;
using System.Linq;
using NuGet.Frameworks;
namespace Microsoft.Dotnet.Cli.Compiler.Common namespace Microsoft.Dotnet.Cli.Compiler.Common
{ {
@ -26,6 +28,8 @@ namespace Microsoft.Dotnet.Cli.Compiler.Common
private const string NeutralCultureOptionName = "neutral-language"; private const string NeutralCultureOptionName = "neutral-language";
private const string TargetFrameworkOptionName = "target-framework";
public string Title { get; set; } public string Title { get; set; }
public string Description { get; set; } public string Description { get; set; }
@ -42,8 +46,22 @@ namespace Microsoft.Dotnet.Cli.Compiler.Common
public string NeutralLanguage { get; set; } public string NeutralLanguage { get; set; }
public static AssemblyInfoOptions CreateForProject(Project project) public string TargetFramework { get; set; }
public static AssemblyInfoOptions CreateForProject(ProjectContext context)
{ {
var project = context.ProjectFile;
NuGetFramework targetFramework = null;
// force .NETFramework instead of DNX
if (context.TargetFramework.IsDesktop())
{
targetFramework = new NuGetFramework(FrameworkConstants.FrameworkIdentifiers.Net, context.TargetFramework.Version);
}
else
{
targetFramework = context.TargetFramework;
}
return new AssemblyInfoOptions() return new AssemblyInfoOptions()
{ {
AssemblyVersion = project.Version?.Version.ToString(), AssemblyVersion = project.Version?.Version.ToString(),
@ -52,7 +70,8 @@ namespace Microsoft.Dotnet.Cli.Compiler.Common
Copyright = project.Copyright, Copyright = project.Copyright,
Description = project.Description, Description = project.Description,
Title = project.Title, Title = project.Title,
NeutralLanguage = project.Language NeutralLanguage = project.Language,
TargetFramework = targetFramework.DotNetFrameworkName
}; };
} }
@ -66,6 +85,7 @@ namespace Microsoft.Dotnet.Cli.Compiler.Common
string copyright = null; string copyright = null;
string culture = null; string culture = null;
string neutralCulture = null; string neutralCulture = null;
string targetFramework = null;
syntax.DefineOption(AssemblyVersionOptionName, ref version, UnescapeNewlines, "Assembly version"); syntax.DefineOption(AssemblyVersionOptionName, ref version, UnescapeNewlines, "Assembly version");
@ -83,6 +103,8 @@ namespace Microsoft.Dotnet.Cli.Compiler.Common
syntax.DefineOption(AssemblyFileVersionOptionName, ref fileVersion, UnescapeNewlines, "Assembly title"); syntax.DefineOption(AssemblyFileVersionOptionName, ref fileVersion, UnescapeNewlines, "Assembly title");
syntax.DefineOption(TargetFrameworkOptionName, ref targetFramework, UnescapeNewlines, "Assembly target framework");
return new AssemblyInfoOptions() return new AssemblyInfoOptions()
{ {
AssemblyFileVersion = fileVersion, AssemblyFileVersion = fileVersion,
@ -91,7 +113,8 @@ namespace Microsoft.Dotnet.Cli.Compiler.Common
NeutralLanguage = neutralCulture, NeutralLanguage = neutralCulture,
Description = description, Description = description,
InformationalVersion = informationalVersion, InformationalVersion = informationalVersion,
Title = title Title = title,
TargetFramework = targetFramework
}; };
} }
@ -131,6 +154,10 @@ namespace Microsoft.Dotnet.Cli.Compiler.Common
{ {
options.Add(FormatOption(NeutralCultureOptionName, assemblyInfoOptions.NeutralLanguage)); options.Add(FormatOption(NeutralCultureOptionName, assemblyInfoOptions.NeutralLanguage));
} }
if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.TargetFramework))
{
options.Add(FormatOption(TargetFrameworkOptionName, assemblyInfoOptions.TargetFramework));
}
return options; return options;
} }

View file

@ -311,7 +311,7 @@ namespace Microsoft.DotNet.Tools.Compiler
compilerArgs.AddRange(compilationOptions.SerializeToArgs()); compilerArgs.AddRange(compilationOptions.SerializeToArgs());
// Add metadata options // Add metadata options
compilerArgs.AddRange(AssemblyInfoOptions.SerializeToArgs(AssemblyInfoOptions.CreateForProject(context.ProjectFile))); compilerArgs.AddRange(AssemblyInfoOptions.SerializeToArgs(AssemblyInfoOptions.CreateForProject(context)));
foreach (var dependency in dependencies) foreach (var dependency in dependencies)
{ {