Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Flow project.json info into assembly info. #2950

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DNX
===

Travis: [![Travis](https://travis-ci.org/aspnet/dnx.svg?branch=dev)](https://travis-ci.org/aspnet/dnx)
Travis: [![Travis](https://travis-ci.org/aspnet/dnx.svg?branch=cesarbs/dnx-assembly-info)](https://travis-ci.org/aspnet/dnx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you update the branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To see the thingy going green in the repo page :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

volkswagen!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's pretty weird 😄 You can just check travis itself.


The .NET Execution Environment contains the code required to bootstrap and run an application. This includes things like the compilation system, SDK tools, and the native CLR hosts.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public class CompilationProjectContext
public CompilationTarget Target { get; }
public string ProjectDirectory { get; }
public string ProjectFilePath { get; }
public string Title { get; }
public string Description { get; }
public string Copyright { get; }
public string Language { get; }
public string Version { get; }
public Version AssemblyFileVersion { get; }
public CompilationFiles Files { get; }
Expand All @@ -24,6 +28,10 @@ public CompilationProjectContext(
CompilationTarget target,
string projectDirectory,
string projectFilePath,
string title,
string description,
string copyright,
string language,
string version,
Version assemblyFileVersion,
bool embedInteropTypes,
Expand All @@ -34,6 +42,10 @@ public CompilationProjectContext(
ProjectDirectory = projectDirectory;
ProjectFilePath = projectFilePath;
Files = files;
Title = title;
Description = description;
Copyright = copyright;
Language = language;
Version = version;
AssemblyFileVersion = assemblyFileVersion;
EmbedInteropTypes = embedInteropTypes;
Expand Down
56 changes: 21 additions & 35 deletions src/Microsoft.Dnx.Compilation.CSharp/RoslynCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public CompilationContext CompileProject(
references,
compilationSettings.CompilationOptions);

compilation = ApplyVersionInfo(compilation, projectContext, parseOptions);
compilation = ApplyProjectInfo(compilation, projectContext, parseOptions);

var compilationContext = new CompilationContext(
compilation,
Expand Down Expand Up @@ -252,58 +252,44 @@ private CompilationModules GetCompileModules(CompilationTarget target)
});
}

private static CSharpCompilation ApplyVersionInfo(CSharpCompilation compilation, CompilationProjectContext project,
private static CSharpCompilation ApplyProjectInfo(CSharpCompilation compilation, CompilationProjectContext project,
CSharpParseOptions parseOptions)
{
const string assemblyFileVersionName = "System.Reflection.AssemblyFileVersionAttribute";
const string assemblyVersionName = "System.Reflection.AssemblyVersionAttribute";
const string assemblyInformationalVersion = "System.Reflection.AssemblyInformationalVersionAttribute";
var attributeNameToProjectValue = new Dictionary<string, string>(StringComparer.Ordinal)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any checks for null or invalid values. What happens if the language is null or empty? same for copyright and description.

{
["System.Reflection.AssemblyTitleAttribute"] = project.Title,
["System.Reflection.AssemblyDescriptionAttribute"] = project.Description,
["System.Reflection.AssemblyCopyrightAttribute"] = project.Copyright,
["System.Reflection.AssemblyCultureAttribute"] = project.Language,
["System.Reflection.AssemblyFileVersionAttribute"] = project.AssemblyFileVersion.ToString(),
["System.Reflection.AssemblyVersionAttribute"] = RemovePrereleaseTag(project.Version),
["System.Reflection.AssemblyInformationalVersionAttribute"] = project.Version
};

var assemblyAttributes = compilation.Assembly.GetAttributes();

var foundAssemblyFileVersion = false;
var foundAssemblyVersion = false;
var foundAssemblyInformationalVersion = false;

foreach (var assembly in assemblyAttributes)
foreach (var attribute in assemblyAttributes)
{
string attributeName = assembly.AttributeClass.ToString();
string attributeName = attribute.AttributeClass.ToString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use var instead of string


if (string.Equals(attributeName, assemblyFileVersionName, StringComparison.Ordinal))
{
foundAssemblyFileVersion = true;
}
else if (string.Equals(attributeName, assemblyVersionName, StringComparison.Ordinal))
{
foundAssemblyVersion = true;
}
else if (string.Equals(attributeName, assemblyInformationalVersion, StringComparison.Ordinal))
if (attributeNameToProjectValue.ContainsKey(attributeName))
{
foundAssemblyInformationalVersion = true;
attributeNameToProjectValue.Remove(attributeName);
}
}

var versionAttributes = new StringBuilder();
if (!foundAssemblyFileVersion)
{
versionAttributes.AppendLine($"[assembly:{assemblyFileVersionName}(\"{project.AssemblyFileVersion}\")]");
}

if (!foundAssemblyVersion)
{
versionAttributes.AppendLine($"[assembly:{assemblyVersionName}(\"{RemovePrereleaseTag(project.Version)}\")]");
}
var attributes = new StringBuilder();

if (!foundAssemblyInformationalVersion)
foreach (var attributeValue in attributeNameToProjectValue)
{
versionAttributes.AppendLine($"[assembly:{assemblyInformationalVersion}(\"{project.Version}\")]");
attributes.AppendLine($"[assembly:{attributeValue.Key}(\"{attributeValue.Value}\")]");
}

if (versionAttributes.Length != 0)
if (attributes.Length != 0)
{
compilation = compilation.AddSyntaxTrees(new[]
{
CSharpSyntaxTree.ParseText(versionAttributes.ToString(), parseOptions)
CSharpSyntaxTree.ParseText(attributes.ToString(), parseOptions)
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.Dnx.Compilation/ProjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public static CompilationProjectContext ToCompilationContext(this Project self,
new CompilationTarget(self.Name, frameworkName, configuration, aspect),
self.ProjectDirectory,
self.ProjectFilePath,
self.Title,
self.Description,
self.Copyright,
self.Language,
self.Version.ToString(),
self.AssemblyFileVersion,
self.EmbedInteropTypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ private static CompilationProjectContext CreateMockProject()
new CompilationTarget("MockProject", new FrameworkName(".NETFramework, Version=8.0"), "fakeConfiguration", null),
"c:\\wonderland",
"c:\\wonderland\\project.json",
"Mock title",
"Mock description",
"Mock copyright",
"en",
"0.0.1-rc-fake",
new Version(0, 0, 1),
embedInteropTypes: false,
Expand Down