From 856fb8d6d96e103a21ba6f1ab11683c34fe3c101 Mon Sep 17 00:00:00 2001 From: moozzyk Date: Thu, 10 Mar 2016 15:49:11 -0800 Subject: [PATCH] Enable specifying output assembly name in compiler options Addresses #1797 --- .../LibraryWithOutputAssemblyName/Class.cs | 6 +++++ .../project.json | 13 ++++++++++ .../CommonCompilerOptionsExtensions.cs | 12 +++++++++ .../CommonCompilerOptions.cs | 10 ++++++- .../CompilationOutputFiles.cs | 4 ++- .../ProjectReader.cs | 3 ++- .../commands/dotnet-pack/PackageGenerator.cs | 6 ++--- test/dotnet-compile.Tests/CompilerTests.cs | 26 ++++++++++++++++--- 8 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 TestAssets/TestProjects/LibraryWithOutputAssemblyName/Class.cs create mode 100644 TestAssets/TestProjects/LibraryWithOutputAssemblyName/project.json diff --git a/TestAssets/TestProjects/LibraryWithOutputAssemblyName/Class.cs b/TestAssets/TestProjects/LibraryWithOutputAssemblyName/Class.cs new file mode 100644 index 000000000..cfd6c6188 --- /dev/null +++ b/TestAssets/TestProjects/LibraryWithOutputAssemblyName/Class.cs @@ -0,0 +1,6 @@ +namespace LibraryWithOutputAssemblyName +{ + public class MyClass + { + } +} \ No newline at end of file diff --git a/TestAssets/TestProjects/LibraryWithOutputAssemblyName/project.json b/TestAssets/TestProjects/LibraryWithOutputAssemblyName/project.json new file mode 100644 index 000000000..e204fe123 --- /dev/null +++ b/TestAssets/TestProjects/LibraryWithOutputAssemblyName/project.json @@ -0,0 +1,13 @@ +{ + "compilationOptions": { + "outputName": "MyLibrary" + }, + "dependencies": { + "NETStandard.Library": "1.0.0-rc2-23901" + }, + "frameworks": { + "netstandardapp1.5": { + "imports": "dnxcore50" + } + } +} \ No newline at end of file diff --git a/src/Microsoft.DotNet.Compiler.Common/CommonCompilerOptionsExtensions.cs b/src/Microsoft.DotNet.Compiler.Common/CommonCompilerOptionsExtensions.cs index 5255f1437..fe945e3a3 100644 --- a/src/Microsoft.DotNet.Compiler.Common/CommonCompilerOptionsExtensions.cs +++ b/src/Microsoft.DotNet.Compiler.Common/CommonCompilerOptionsExtensions.cs @@ -40,6 +40,8 @@ namespace Microsoft.DotNet.Cli.Compiler.Common internal static readonly OptionTemplate s_additionalArgumentsTemplate = new OptionTemplate("additional-argument"); + internal static readonly OptionTemplate s_outputNameTemplate = new OptionTemplate("output-name"); + public static CommonCompilerOptions Parse(ArgumentSyntax syntax) { IReadOnlyList defines = null; @@ -55,6 +57,7 @@ namespace Microsoft.DotNet.Cli.Compiler.Common bool? publicSign = null; bool? emitEntryPoint = null; bool? generateXmlDocumentation = null; + string outputName = null; IReadOnlyList additionalArguments = null; Func nullableBoolConverter = v => bool.Parse(v); @@ -97,6 +100,8 @@ namespace Microsoft.DotNet.Cli.Compiler.Common syntax.DefineOption(s_generateXmlDocumentation.LongName, ref generateXmlDocumentation, nullableBoolConverter, "Generate XML documentation file"); + syntax.DefineOption(s_outputNameTemplate.LongName, ref outputName, "Output assembly name"); + return new CommonCompilerOptions { Defines = defines, @@ -112,6 +117,7 @@ namespace Microsoft.DotNet.Cli.Compiler.Common DebugType = debugType, EmitEntryPoint = emitEntryPoint, GenerateXmlDocumentation = generateXmlDocumentation, + OutputName = outputName, AdditionalArguments = additionalArguments }; } @@ -131,6 +137,7 @@ namespace Microsoft.DotNet.Cli.Compiler.Common var publicSign = options.PublicSign; var emitEntryPoint = options.EmitEntryPoint; var generateXmlDocumentation = options.GenerateXmlDocumentation; + var outputName = options.OutputName; var additionalArguments = options.AdditionalArguments; var args = new List(); @@ -205,6 +212,11 @@ namespace Microsoft.DotNet.Cli.Compiler.Common args.Add(s_generateXmlDocumentation.ToLongArg(generateXmlDocumentation)); } + if (outputName != null) + { + args.Add(s_outputNameTemplate.ToLongArg(outputName)); + } + return args; } } diff --git a/src/Microsoft.DotNet.ProjectModel/CommonCompilerOptions.cs b/src/Microsoft.DotNet.ProjectModel/CommonCompilerOptions.cs index c417828ef..1a1d3dc95 100644 --- a/src/Microsoft.DotNet.ProjectModel/CommonCompilerOptions.cs +++ b/src/Microsoft.DotNet.ProjectModel/CommonCompilerOptions.cs @@ -39,6 +39,8 @@ namespace Microsoft.DotNet.ProjectModel public IEnumerable AdditionalArguments { get; set; } + public string OutputName { get;set; } + public override bool Equals(object obj) { var other = obj as CommonCompilerOptions; @@ -57,7 +59,8 @@ namespace Microsoft.DotNet.ProjectModel PreserveCompilationContext == other.PreserveCompilationContext && EnumerableEquals(Defines, other.Defines) && EnumerableEquals(SuppressWarnings, other.SuppressWarnings) && - EnumerableEquals(AdditionalArguments, other.AdditionalArguments); + EnumerableEquals(AdditionalArguments, other.AdditionalArguments) && + OutputName == other.OutputName; } private static bool EnumerableEquals(IEnumerable left, IEnumerable right) @@ -153,6 +156,11 @@ namespace Microsoft.DotNet.ProjectModel { result.GenerateXmlDocumentation = option.GenerateXmlDocumentation; } + + if (option.OutputName != null) + { + result.OutputName = option.OutputName; + } } return result; diff --git a/src/Microsoft.DotNet.ProjectModel/CompilationOutputFiles.cs b/src/Microsoft.DotNet.ProjectModel/CompilationOutputFiles.cs index 64d55ae7a..aabbae873 100644 --- a/src/Microsoft.DotNet.ProjectModel/CompilationOutputFiles.cs +++ b/src/Microsoft.DotNet.ProjectModel/CompilationOutputFiles.cs @@ -40,9 +40,11 @@ namespace Microsoft.DotNet.ProjectModel { get { + var compilationOptions = Project.GetCompilerOptions(Framework, Configuration); + return Path.Combine( BasePath, - Project.Name + OutputExtension); + (compilationOptions.OutputName ?? Project.Name) + OutputExtension); } } diff --git a/src/Microsoft.DotNet.ProjectModel/ProjectReader.cs b/src/Microsoft.DotNet.ProjectModel/ProjectReader.cs index 39f28ba12..75e218936 100644 --- a/src/Microsoft.DotNet.ProjectModel/ProjectReader.cs +++ b/src/Microsoft.DotNet.ProjectModel/ProjectReader.cs @@ -575,7 +575,8 @@ namespace Microsoft.DotNet.ProjectModel DebugType = rawOptions.ValueAsString("debugType"), EmitEntryPoint = rawOptions.ValueAsNullableBoolean("emitEntryPoint"), GenerateXmlDocumentation = rawOptions.ValueAsNullableBoolean("xmlDoc"), - PreserveCompilationContext = rawOptions.ValueAsNullableBoolean("preserveCompilationContext") + PreserveCompilationContext = rawOptions.ValueAsNullableBoolean("preserveCompilationContext"), + OutputName = rawOptions.ValueAsString("outputName") }; } diff --git a/src/dotnet/commands/dotnet-pack/PackageGenerator.cs b/src/dotnet/commands/dotnet-pack/PackageGenerator.cs index 44a793b6f..9e9a5ffbc 100644 --- a/src/dotnet/commands/dotnet-pack/PackageGenerator.cs +++ b/src/dotnet/commands/dotnet-pack/PackageGenerator.cs @@ -26,7 +26,7 @@ namespace Microsoft.DotNet.Tools.Compiler public class PackageGenerator { protected ArtifactPathsCalculator ArtifactPathsCalculator { get; } - + protected Project Project { get; } protected string Configuration { get; } @@ -38,7 +38,7 @@ namespace Microsoft.DotNet.Tools.Compiler ArtifactPathsCalculator = artifactPathsCalculator; Project = project; Configuration = configuration; - } + } public bool BuildPackage(IEnumerable contexts, List packDiagnostics) { @@ -323,7 +323,7 @@ namespace Microsoft.DotNet.Tools.Compiler } return Project.Name + outputExtension; - } + } private static string GetDefaultRootOutputPath(Project project, string outputOptionValue) { diff --git a/test/dotnet-compile.Tests/CompilerTests.cs b/test/dotnet-compile.Tests/CompilerTests.cs index 132e1e0c6..b1cbb3f88 100644 --- a/test/dotnet-compile.Tests/CompilerTests.cs +++ b/test/dotnet-compile.Tests/CompilerTests.cs @@ -74,20 +74,20 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests [Fact] public void LibraryWithAnalyzer() - { + { var root = Temp.CreateDirectory(); var testLibDir = root.CreateDirectory("TestLibraryWithAnalyzer"); var sourceTestLibDir = Path.Combine(_testProjectsRoot, "TestLibraryWithAnalyzer"); CopyProjectToTempDir(sourceTestLibDir, testLibDir); - + // run compile var outputDir = Path.Combine(testLibDir.Path, "bin"); var testProject = GetProjectPath(testLibDir); var buildCmd = new BuildCommand(testProject, output: outputDir, framework: DefaultFramework); var result = buildCmd.ExecuteWithCapturedOutput(); result.Should().Pass(); - + Assert.Contains("CA1018", result.StdErr); } @@ -103,7 +103,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests var testProjectDir = Path.Combine(_testProjectsRoot, "TestAppCompilationContext", "TestApp"); var testProject = Path.Combine(testProjectDir, "project.json"); - + var buildCommand = new BuildCommand(testProject); buildCommand.Execute().Should().Pass(); @@ -167,6 +167,24 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests runCommand.Execute().Should().Pass(); } + [Fact] + public void CanSetOutputAssemblyName() + { + var testInstance = + TestAssetsManager + .CreateTestInstance("LibraryWithOutputAssemblyName") + .WithLockFiles(); + + var root = testInstance.TestRoot; + var outputDir = Path.Combine(root, "bin"); + var testProject = ProjectUtils.GetProjectJson(root, "LibraryWithOutputAssemblyName"); + var buildCommand = new BuildCommand(testProject, output: outputDir, framework: DefaultFramework); + var result = buildCommand.ExecuteWithCapturedOutput(); + result.Should().Pass(); + + new DirectoryInfo(outputDir).Should().HaveFiles(new [] { "MyLibrary.dll" }); + } + private void CopyProjectToTempDir(string projectDir, TempDirectory tempDir) { // copy all the files to temp dir