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.
This commit is contained in:
Andy Gocke 2015-10-23 15:21:49 -07:00
parent 9350db24be
commit 2ccecbf78e
3 changed files with 63 additions and 6 deletions

View file

@ -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<string, string>[] s_compilerNameLookupTable =
{
new KeyValuePair<string, string>(".cs", "csc"),
new KeyValuePair<string, string>(".vb", "vbc"),
new KeyValuePair<string, string>(".fs", "fsc")
};
/// <summary>
/// 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.
/// </summary>
private static bool TryDetectCompilerName(IEnumerable<string> 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<DiagnosticMessage> diagnostics)
{
Reporter.Output.Writer.WriteLine();

View file

@ -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<string, string> Commands { get; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

View file

@ -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<string>();
project.Owners = rawProject.ValueAsStringArray("owners") ?? Array.Empty<string>();