From 2ccecbf78ed81552d7c87915d9a3d5da1c2303d3 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Fri, 23 Oct 2015 15:21:49 -0700 Subject: [PATCH] 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();