From 2ccecbf78ed81552d7c87915d9a3d5da1c2303d3 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Fri, 23 Oct 2015 15:21:49 -0700 Subject: [PATCH 1/3] Add a compilerName option to the project.json If compilerName is not specified in the project.json then dotnet compile will attempt to detect a compiler based on a hardcoded lookup table. --- .../Program.cs | 66 +++++++++++++++++-- .../Project.cs | 2 + .../ProjectReader.cs | 1 + 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.DotNet.Tools.Compiler/Program.cs b/src/Microsoft.DotNet.Tools.Compiler/Program.cs index 9d17b3b3a..cea220dba 100644 --- a/src/Microsoft.DotNet.Tools.Compiler/Program.cs +++ b/src/Microsoft.DotNet.Tools.Compiler/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -182,21 +182,26 @@ namespace Microsoft.DotNet.Tools.Compiler } // Add project source files - compilerArgs.AddRange(context.ProjectFile.Files.SourceFiles.Select(s => $"\"{s}\"")); + var sourceFiles = context.ProjectFile.Files.SourceFiles; + compilerArgs.AddRange(sourceFiles.Select(s => $"\"{s}\"")); if (!AddResources(context.ProjectFile, compilerArgs, intermediateOutputPath)) { return false; } - // TODO: Read this from the project - const string compiler = "csc"; + var compilerName = context.ProjectFile.CompilerName; + if (compilerName == null && !TryDetectCompilerName(sourceFiles, out compilerName)) + { + Console.Error.WriteLine("Could not detect the compiler name. Please specify it in the project.json file."); + return false; + } // Write RSP file - var rsp = Path.Combine(intermediateOutputPath, $"dotnet-compile.{compiler}.rsp"); + var rsp = Path.Combine(intermediateOutputPath, $"dotnet-compile.{compilerName}.rsp"); File.WriteAllLines(rsp, compilerArgs); - var result = Command.Create($"dotnet-compile-{compiler}", $"\"{rsp}\"") + var result = Command.Create($"dotnet-compile-{compilerName}", $"\"{rsp}\"") .OnErrorLine(line => { var diagnostic = ParseDiagnostic(context.ProjectDirectory, line); @@ -237,6 +242,55 @@ namespace Microsoft.DotNet.Tools.Compiler return success; } + private static readonly KeyValuePair[] s_compilerNameLookupTable = + { + new KeyValuePair(".cs", "csc"), + new KeyValuePair(".vb", "vbc"), + new KeyValuePair(".fs", "fsc") + }; + + /// + /// Uses the extension on the source files to try to detect the + /// compiler. If the source files have different extensions or the + /// extension is not recognized, returns false. + /// + private static bool TryDetectCompilerName(IEnumerable sourceFiles, out string compilerName) + { + compilerName = null; + string extension = null; + foreach (var file in sourceFiles) + { + if (!Path.HasExtension(file)) + { + return false; + } + + var tmpExtension = Path.GetExtension(file); + extension = extension ?? tmpExtension; + + if (extension != tmpExtension) + { + return false; + } + } + + if (extension == null) + { + return false; + } + + foreach (var kvp in s_compilerNameLookupTable) + { + if (extension == kvp.Key) + { + compilerName = kvp.Value; + return true; + } + } + + return false; + } + private static void PrintSummary(List diagnostics) { Reporter.Output.Writer.WriteLine(); diff --git a/src/Microsoft.Extensions.ProjectModel/Project.cs b/src/Microsoft.Extensions.ProjectModel/Project.cs index 82e02e9c3..0c2341812 100644 --- a/src/Microsoft.Extensions.ProjectModel/Project.cs +++ b/src/Microsoft.Extensions.ProjectModel/Project.cs @@ -74,6 +74,8 @@ namespace Microsoft.Extensions.ProjectModel public string[] Tags { get; set; } + public string CompilerName { get; set; } + public ProjectFilesCollection Files { get; set; } public IDictionary Commands { get; } = new Dictionary(StringComparer.OrdinalIgnoreCase); diff --git a/src/Microsoft.Extensions.ProjectModel/ProjectReader.cs b/src/Microsoft.Extensions.ProjectModel/ProjectReader.cs index 9237488b2..e2a4dd777 100644 --- a/src/Microsoft.Extensions.ProjectModel/ProjectReader.cs +++ b/src/Microsoft.Extensions.ProjectModel/ProjectReader.cs @@ -139,6 +139,7 @@ namespace Microsoft.Extensions.ProjectModel project.ProjectUrl = rawProject.ValueAsString("projectUrl"); project.LicenseUrl = rawProject.ValueAsString("licenseUrl"); project.IconUrl = rawProject.ValueAsString("iconUrl"); + project.CompilerName = rawProject.ValueAsString("compilerName"); project.Authors = rawProject.ValueAsStringArray("authors") ?? Array.Empty(); project.Owners = rawProject.ValueAsStringArray("owners") ?? Array.Empty(); From 72665a4f9fb787c057773c559262c1ae13efc7d2 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Sun, 25 Oct 2015 23:37:41 -0700 Subject: [PATCH 2/3] Remove compiler name autodetection Also add compilerName: csc to every project.json to compensate. --- src/Microsoft.DotNet.Cli.Utils/project.json | 1 + src/Microsoft.DotNet.Cli/project.json | 1 + .../project.json | 1 + .../Program.cs | 53 +------------------ .../project.json | 1 + .../project.json | 1 + .../project.json | 1 + .../project.json | 2 +- 8 files changed, 9 insertions(+), 52 deletions(-) diff --git a/src/Microsoft.DotNet.Cli.Utils/project.json b/src/Microsoft.DotNet.Cli.Utils/project.json index b6ff1d85e..e177c12c2 100644 --- a/src/Microsoft.DotNet.Cli.Utils/project.json +++ b/src/Microsoft.DotNet.Cli.Utils/project.json @@ -1,5 +1,6 @@ { "version": "1.0.0-*", + "compilerName": "csc", "shared": "**/*.cs", diff --git a/src/Microsoft.DotNet.Cli/project.json b/src/Microsoft.DotNet.Cli/project.json index b927627b4..d6a128e4f 100644 --- a/src/Microsoft.DotNet.Cli/project.json +++ b/src/Microsoft.DotNet.Cli/project.json @@ -1,6 +1,7 @@ { "name": "dotnet", "version": "1.0.0-*", + "compilerName": "csc", "compilationOptions": { "emitEntryPoint": true }, diff --git a/src/Microsoft.DotNet.Tools.Compiler.Csc/project.json b/src/Microsoft.DotNet.Tools.Compiler.Csc/project.json index 37137989e..9a2f3d0eb 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Csc/project.json +++ b/src/Microsoft.DotNet.Tools.Compiler.Csc/project.json @@ -1,6 +1,7 @@ { "name": "dotnet-compile-csc", "version": "1.0.0-*", + "compilerName": "csc", "compilationOptions": { "emitEntryPoint": true }, diff --git a/src/Microsoft.DotNet.Tools.Compiler/Program.cs b/src/Microsoft.DotNet.Tools.Compiler/Program.cs index cea220dba..2119009fe 100644 --- a/src/Microsoft.DotNet.Tools.Compiler/Program.cs +++ b/src/Microsoft.DotNet.Tools.Compiler/Program.cs @@ -191,9 +191,9 @@ namespace Microsoft.DotNet.Tools.Compiler } var compilerName = context.ProjectFile.CompilerName; - if (compilerName == null && !TryDetectCompilerName(sourceFiles, out compilerName)) + if (compilerName == null) { - Console.Error.WriteLine("Could not detect the compiler name. Please specify it in the project.json file."); + Console.Error.WriteLine("Could not find the compiler name. Please specify it in the project.json file."); return false; } @@ -242,55 +242,6 @@ namespace Microsoft.DotNet.Tools.Compiler return success; } - private static readonly KeyValuePair[] s_compilerNameLookupTable = - { - new KeyValuePair(".cs", "csc"), - new KeyValuePair(".vb", "vbc"), - new KeyValuePair(".fs", "fsc") - }; - - /// - /// Uses the extension on the source files to try to detect the - /// compiler. If the source files have different extensions or the - /// extension is not recognized, returns false. - /// - private static bool TryDetectCompilerName(IEnumerable sourceFiles, out string compilerName) - { - compilerName = null; - string extension = null; - foreach (var file in sourceFiles) - { - if (!Path.HasExtension(file)) - { - return false; - } - - var tmpExtension = Path.GetExtension(file); - extension = extension ?? tmpExtension; - - if (extension != tmpExtension) - { - return false; - } - } - - if (extension == null) - { - return false; - } - - foreach (var kvp in s_compilerNameLookupTable) - { - if (extension == kvp.Key) - { - compilerName = kvp.Value; - return true; - } - } - - return false; - } - private static void PrintSummary(List diagnostics) { Reporter.Output.Writer.WriteLine(); diff --git a/src/Microsoft.DotNet.Tools.Compiler/project.json b/src/Microsoft.DotNet.Tools.Compiler/project.json index 60777510f..1d0c647b9 100644 --- a/src/Microsoft.DotNet.Tools.Compiler/project.json +++ b/src/Microsoft.DotNet.Tools.Compiler/project.json @@ -1,6 +1,7 @@ { "name": "dotnet-compile", "version": "1.0.0-*", + "compilerName": "csc", "compilationOptions": { "emitEntryPoint": true }, diff --git a/src/Microsoft.DotNet.Tools.Publish/project.json b/src/Microsoft.DotNet.Tools.Publish/project.json index c0d8ddb35..c663ad76c 100644 --- a/src/Microsoft.DotNet.Tools.Publish/project.json +++ b/src/Microsoft.DotNet.Tools.Publish/project.json @@ -1,6 +1,7 @@ { "name": "dotnet-publish", "version": "1.0.0-*", + "compilerName": "csc", "compilationOptions": { "emitEntryPoint": true }, diff --git a/src/Microsoft.DotNet.Tools.Resgen/project.json b/src/Microsoft.DotNet.Tools.Resgen/project.json index da64c8872..52bbc0388 100644 --- a/src/Microsoft.DotNet.Tools.Resgen/project.json +++ b/src/Microsoft.DotNet.Tools.Resgen/project.json @@ -1,6 +1,7 @@ { "name": "resgen", "version": "1.0.0-*", + "compilerName": "csc", "compilationOptions": { "emitEntryPoint": true }, diff --git a/src/Microsoft.Extensions.ProjectModel/project.json b/src/Microsoft.Extensions.ProjectModel/project.json index c17c5cd2f..c203f4053 100644 --- a/src/Microsoft.Extensions.ProjectModel/project.json +++ b/src/Microsoft.Extensions.ProjectModel/project.json @@ -1,7 +1,7 @@ { "version": "1.0.0-*", "description": "Types to model a .NET Project", - + "compilerName": "csc", "dependencies": { "Microsoft.CSharp": "4.0.1-beta-23419", "System.Collections": "4.0.11-beta-23419", From 953d9348193d0fc88b861342c567a842167ba05f Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Mon, 26 Oct 2015 09:23:58 -0700 Subject: [PATCH 3/3] Add csc as default compilerName if one is not specified Also remove csc from project.json files, since it is no longer necessary. --- src/Microsoft.DotNet.Cli.Utils/project.json | 1 - src/Microsoft.DotNet.Cli/project.json | 1 - src/Microsoft.DotNet.Tools.Compiler.Csc/project.json | 1 - src/Microsoft.DotNet.Tools.Compiler/Program.cs | 6 +----- src/Microsoft.DotNet.Tools.Compiler/project.json | 1 - src/Microsoft.DotNet.Tools.Publish/project.json | 1 - src/Microsoft.DotNet.Tools.Resgen/project.json | 1 - src/Microsoft.Extensions.ProjectModel/project.json | 1 - 8 files changed, 1 insertion(+), 12 deletions(-) diff --git a/src/Microsoft.DotNet.Cli.Utils/project.json b/src/Microsoft.DotNet.Cli.Utils/project.json index e177c12c2..b6ff1d85e 100644 --- a/src/Microsoft.DotNet.Cli.Utils/project.json +++ b/src/Microsoft.DotNet.Cli.Utils/project.json @@ -1,6 +1,5 @@ { "version": "1.0.0-*", - "compilerName": "csc", "shared": "**/*.cs", diff --git a/src/Microsoft.DotNet.Cli/project.json b/src/Microsoft.DotNet.Cli/project.json index d6a128e4f..b927627b4 100644 --- a/src/Microsoft.DotNet.Cli/project.json +++ b/src/Microsoft.DotNet.Cli/project.json @@ -1,7 +1,6 @@ { "name": "dotnet", "version": "1.0.0-*", - "compilerName": "csc", "compilationOptions": { "emitEntryPoint": true }, diff --git a/src/Microsoft.DotNet.Tools.Compiler.Csc/project.json b/src/Microsoft.DotNet.Tools.Compiler.Csc/project.json index 9a2f3d0eb..37137989e 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Csc/project.json +++ b/src/Microsoft.DotNet.Tools.Compiler.Csc/project.json @@ -1,7 +1,6 @@ { "name": "dotnet-compile-csc", "version": "1.0.0-*", - "compilerName": "csc", "compilationOptions": { "emitEntryPoint": true }, diff --git a/src/Microsoft.DotNet.Tools.Compiler/Program.cs b/src/Microsoft.DotNet.Tools.Compiler/Program.cs index 2119009fe..281986a3c 100644 --- a/src/Microsoft.DotNet.Tools.Compiler/Program.cs +++ b/src/Microsoft.DotNet.Tools.Compiler/Program.cs @@ -191,11 +191,7 @@ namespace Microsoft.DotNet.Tools.Compiler } var compilerName = context.ProjectFile.CompilerName; - if (compilerName == null) - { - Console.Error.WriteLine("Could not find the compiler name. Please specify it in the project.json file."); - return false; - } + compilerName = compilerName ?? "csc"; // Write RSP file var rsp = Path.Combine(intermediateOutputPath, $"dotnet-compile.{compilerName}.rsp"); diff --git a/src/Microsoft.DotNet.Tools.Compiler/project.json b/src/Microsoft.DotNet.Tools.Compiler/project.json index 1d0c647b9..60777510f 100644 --- a/src/Microsoft.DotNet.Tools.Compiler/project.json +++ b/src/Microsoft.DotNet.Tools.Compiler/project.json @@ -1,7 +1,6 @@ { "name": "dotnet-compile", "version": "1.0.0-*", - "compilerName": "csc", "compilationOptions": { "emitEntryPoint": true }, diff --git a/src/Microsoft.DotNet.Tools.Publish/project.json b/src/Microsoft.DotNet.Tools.Publish/project.json index c663ad76c..c0d8ddb35 100644 --- a/src/Microsoft.DotNet.Tools.Publish/project.json +++ b/src/Microsoft.DotNet.Tools.Publish/project.json @@ -1,7 +1,6 @@ { "name": "dotnet-publish", "version": "1.0.0-*", - "compilerName": "csc", "compilationOptions": { "emitEntryPoint": true }, diff --git a/src/Microsoft.DotNet.Tools.Resgen/project.json b/src/Microsoft.DotNet.Tools.Resgen/project.json index 52bbc0388..da64c8872 100644 --- a/src/Microsoft.DotNet.Tools.Resgen/project.json +++ b/src/Microsoft.DotNet.Tools.Resgen/project.json @@ -1,7 +1,6 @@ { "name": "resgen", "version": "1.0.0-*", - "compilerName": "csc", "compilationOptions": { "emitEntryPoint": true }, diff --git a/src/Microsoft.Extensions.ProjectModel/project.json b/src/Microsoft.Extensions.ProjectModel/project.json index c203f4053..fc475de1e 100644 --- a/src/Microsoft.Extensions.ProjectModel/project.json +++ b/src/Microsoft.Extensions.ProjectModel/project.json @@ -1,7 +1,6 @@ { "version": "1.0.0-*", "description": "Types to model a .NET Project", - "compilerName": "csc", "dependencies": { "Microsoft.CSharp": "4.0.1-beta-23419", "System.Collections": "4.0.11-beta-23419",