Refactoring the arguments parser into a separate Parser and refactoring the creation of the NativeCompilerSettings into a method of ArgValues.

This commit is contained in:
Livar Cunha 2015-11-20 17:23:19 -08:00
parent db3a9ffc20
commit 39fd1ed5c5
4 changed files with 186 additions and 178 deletions

View file

@ -1,14 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Dnx.Runtime.Common.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
namespace Microsoft.DotNet.Tools.Compiler.Native namespace Microsoft.DotNet.Tools.Compiler.Native
{ {
@ -21,11 +11,70 @@ namespace Microsoft.DotNet.Tools.Compiler.Native
public BuildConfiguration? BuildConfiguration { get; set; } public BuildConfiguration? BuildConfiguration { get; set; }
public ArchitectureMode Architecture { get; set; } public ArchitectureMode Architecture { get; set; }
public NativeIntermediateMode? NativeMode { get; set; } public NativeIntermediateMode? NativeMode { get; set; }
public List<string> ReferencePaths { get; set; } public IEnumerable<string> ReferencePaths { get; set; }
public string IlcArgs { get; set; } public string IlcArgs { get; set; }
public List<string> LinkLibPaths { get; set; } public IEnumerable<string> LinkLibPaths { get; set; }
public string AppDepSDKPath { get; set; } public string AppDepSDKPath { get; set; }
public string IlcPath { get; set; } public string IlcPath { get; set; }
}
public NativeCompileSettings GetNativeCompileSettings()
{
var config = NativeCompileSettings.Default;
config.InputManagedAssemblyPath = InputManagedAssemblyPath;
config.Architecture = Architecture;
if (BuildConfiguration.HasValue)
{
config.BuildType = BuildConfiguration.Value;
}
if (!string.IsNullOrEmpty(OutputDirectory))
{
config.OutputDirectory = OutputDirectory;
}
if (!string.IsNullOrEmpty(IntermediateDirectory))
{
config.IntermediateDirectory = IntermediateDirectory;
}
if (NativeMode.HasValue)
{
config.NativeMode = NativeMode.Value;
}
if (!string.IsNullOrEmpty(AppDepSDKPath))
{
config.AppDepSDKPath = AppDepSDKPath;
}
if (!string.IsNullOrEmpty(IlcPath))
{
config.IlcPath = IlcPath;
}
if (!string.IsNullOrEmpty(LogPath))
{
config.LogPath = LogPath;
}
if (!string.IsNullOrEmpty(IlcArgs))
{
config.IlcArgs = IlcArgs;
}
foreach (var reference in ReferencePaths)
{
config.AddReference(reference);
}
foreach (var lib in LinkLibPaths)
{
config.AddLinkLibPath(lib);
}
return config;
}
}
} }

View file

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
namespace Microsoft.DotNet.Tools.Compiler.Native
{
internal static class ArgumentsParser
{
internal static ArgValues Parse(IEnumerable<string> args)
{
string inputAssembly = null;
string outputDirectory = null;
string temporaryOutputDirectory = null;
string configuration = null;
BuildConfiguration? buildConfiguration = null;
string mode = null;
NativeIntermediateMode? nativeMode = null;
string ilcArgs = null;
string ilcPath = null;
string appDepSdk = null;
string logPath = null;
IReadOnlyList<string> references = Array.Empty<string>();
IReadOnlyList<string> linklib = Array.Empty<string>();
try
{
ArgumentSyntax.Parse(args, syntax =>
{
syntax.DefineOption("output", ref outputDirectory, "Output Directory for native executable.");
syntax.DefineOption("temp-output", ref temporaryOutputDirectory, "Directory for intermediate files.");
syntax.DefineOption("configuration", ref configuration,
"debug/release build configuration. Defaults to debug.");
syntax.DefineOption("mode", ref mode, "Code Generation mode. Defaults to ryujit.");
syntax.DefineOptionList("reference", ref references,
"Use to specify Managed DLL references of the app.");
// Custom Extensibility Points to support CoreRT workflow TODO better descriptions
syntax.DefineOption("ilcargs", ref ilcArgs, "Use to specify custom arguments for the IL Compiler.");
syntax.DefineOption("ilcpath", ref ilcPath, "Use to plug in a custom built ilc.exe");
syntax.DefineOptionList("linklib", ref linklib, "Use to link in additional static libs");
// TEMPORARY Hack until CoreRT compatible Framework Libs are available
syntax.DefineOption("appdepsdk", ref appDepSdk, "Use to plug in custom appdepsdk path");
// Optional Log Path
syntax.DefineOption("logpath", ref logPath, "Use to dump Native Compilation Logs to a file.");
syntax.DefineParameter("INPUT_ASSEMBLY", ref inputAssembly,
"The managed input assembly to compile to native.");
if (string.IsNullOrWhiteSpace(inputAssembly))
{
syntax.ReportError("Input Assembly is a required parameter.");
}
if (!string.IsNullOrEmpty(configuration))
{
try
{
buildConfiguration = EnumExtensions.Parse<BuildConfiguration>(configuration);
}
catch (ArgumentException)
{
syntax.ReportError($"Invalid Configuration Option: {configuration}");
}
}
if (!string.IsNullOrEmpty(mode))
{
try
{
nativeMode = EnumExtensions.Parse<NativeIntermediateMode>(mode);
}
catch (ArgumentException)
{
syntax.ReportError($"Invalid Mode Option: {mode}");
}
}
});
}
catch (ArgumentSyntaxException)
{
//return ExitFailed;
}
Console.WriteLine($"Input Assembly: {inputAssembly}");
return new ArgValues
{
InputManagedAssemblyPath = inputAssembly,
OutputDirectory = outputDirectory,
IntermediateDirectory = temporaryOutputDirectory,
Architecture = ArchitectureMode.x64,
BuildConfiguration = buildConfiguration,
NativeMode = nativeMode,
ReferencePaths = references,
IlcArgs = ilcArgs,
IlcPath = ilcPath,
LinkLibPaths = linklib,
AppDepSDKPath = appDepSdk,
LogPath = logPath
};
}
}
}

View file

@ -16,10 +16,16 @@ namespace Microsoft.DotNet.Tools.Compiler.Native
private string _ilcPath; private string _ilcPath;
private string _outputDirectory; private string _outputDirectory;
private string _intermediateDirectory; private string _intermediateDirectory;
private string _logPath;
private string _ilcArgs;
private readonly List<string> _referencePaths; private readonly List<string> _referencePaths;
private readonly List<string> _linkLibPaths; private readonly List<string> _linkLibPaths;
public string LogPath { get; set; } public string LogPath
{
get { return _logPath; }
set { _logPath = Path.GetFullPath(value); }
}
public string InputManagedAssemblyPath public string InputManagedAssemblyPath
{ {
@ -82,7 +88,11 @@ namespace Microsoft.DotNet.Tools.Compiler.Native
} }
// Optional Customization Points (Can be null) // Optional Customization Points (Can be null)
public string IlcArgs { get; set; } public string IlcArgs
{
get { return _ilcArgs; }
set { _ilcArgs = Path.GetFullPath(value); }
}
public IEnumerable<string> LinkLibPaths => _linkLibPaths; public IEnumerable<string> LinkLibPaths => _linkLibPaths;
// Required Customization Points (Must have default) // Required Customization Points (Must have default)

View file

@ -1,8 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.CommandLine;
using System.IO; using System.IO;
using System.Linq;
using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Tools.Compiler.Native namespace Microsoft.DotNet.Tools.Compiler.Native
@ -14,103 +11,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native
DebugHelper.HandleDebugSwitch(ref args); DebugHelper.HandleDebugSwitch(ref args);
return ExecuteApp(args); return ExecuteApp(args);
} }
private static ArgValues GetArgs(string[] args)
{
string inputAssembly = null;
string outputDirectory = null;
string temporaryOutputDirectory = null;
string configuration = null;
BuildConfiguration? buildConfiguration = null;
string mode = null;
NativeIntermediateMode? nativeMode = null;
string ilcArgs = null;
string ilcPath = null;
string appDepSdk = null;
string logPath = null;
IReadOnlyList<string> references = Array.Empty<string>();
IReadOnlyList<string> linklib = Array.Empty<string>();
try
{
ArgumentSyntax.Parse(args, syntax =>
{
syntax.DefineOption("output", ref outputDirectory, "Output Directory for native executable.");
syntax.DefineOption("temp-output", ref temporaryOutputDirectory, "Directory for intermediate files.");
syntax.DefineOption("configuration", ref configuration, "debug/release build configuration. Defaults to debug.");
syntax.DefineOption("mode", ref mode, "Code Generation mode. Defaults to ryujit.");
syntax.DefineOptionList("reference", ref references, "Use to specify Managed DLL references of the app.");
// Custom Extensibility Points to support CoreRT workflow TODO better descriptions
syntax.DefineOption("ilcargs", ref ilcArgs, "Use to specify custom arguments for the IL Compiler.");
syntax.DefineOption("ilcpath", ref ilcPath, "Use to plug in a custom built ilc.exe");
syntax.DefineOptionList("linklib", ref linklib, "Use to link in additional static libs");
// TEMPORARY Hack until CoreRT compatible Framework Libs are available
syntax.DefineOption("appdepsdk", ref appDepSdk, "Use to plug in custom appdepsdk path");
// Optional Log Path
syntax.DefineOption("logpath", ref logPath, "Use to dump Native Compilation Logs to a file.");
syntax.DefineParameter("INPUT_ASSEMBLY", ref inputAssembly, "The managed input assembly to compile to native.");
if (string.IsNullOrWhiteSpace(inputAssembly))
{
syntax.ReportError("Input Assembly is a required parameter.");
}
if (!string.IsNullOrEmpty(configuration))
{
try
{
buildConfiguration = EnumExtensions.Parse<BuildConfiguration>(configuration);
}
catch (ArgumentException)
{
syntax.ReportError($"Invalid Configuration Option: {configuration}");
}
}
if (!string.IsNullOrEmpty(mode))
{
try
{
nativeMode = EnumExtensions.Parse<NativeIntermediateMode>(mode);
}
catch (ArgumentException)
{
syntax.ReportError($"Invalid Mode Option: {mode}");
}
}
});
}
catch (ArgumentSyntaxException)
{
//return ExitFailed;
}
Console.WriteLine($"Input Assembly: {inputAssembly}");
return new ArgValues
{
InputManagedAssemblyPath = inputAssembly,
OutputDirectory = outputDirectory,
IntermediateDirectory = temporaryOutputDirectory,
Architecture = ArchitectureMode.x64,
BuildConfiguration = buildConfiguration,
NativeMode = nativeMode,
ReferencePaths = references.ToList(),
IlcArgs = ilcArgs,
IlcPath = ilcPath,
LinkLibPaths = linklib.ToList(),
AppDepSDKPath = appDepSdk,
LogPath = logPath
};
}
private static int ExecuteApp(string[] args) private static int ExecuteApp(string[] args)
{ {
@ -130,8 +31,8 @@ namespace Microsoft.DotNet.Tools.Compiler.Native
try try
{ {
var cmdLineArgs = GetArgs(args); var cmdLineArgs = ArgumentsParser.Parse(args);
var config = ParseAndValidateArgs(cmdLineArgs); var config = cmdLineArgs.GetNativeCompileSettings();
DirectoryExtensions.CleanOrCreateDirectory(config.OutputDirectory); DirectoryExtensions.CleanOrCreateDirectory(config.OutputDirectory);
DirectoryExtensions.CleanOrCreateDirectory(config.IntermediateDirectory); DirectoryExtensions.CleanOrCreateDirectory(config.IntermediateDirectory);
@ -175,65 +76,5 @@ namespace Microsoft.DotNet.Tools.Compiler.Native
var nArgs = content.Split(new [] {"\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries); var nArgs = content.Split(new [] {"\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries);
return nArgs; return nArgs;
} }
private static NativeCompileSettings ParseAndValidateArgs(ArgValues args)
{
var config = NativeCompileSettings.Default;
config.InputManagedAssemblyPath = args.InputManagedAssemblyPath;
config.Architecture = args.Architecture;
if (args.BuildConfiguration.HasValue)
{
config.BuildType = args.BuildConfiguration.Value;
}
if(!string.IsNullOrEmpty(args.OutputDirectory))
{
config.OutputDirectory = args.OutputDirectory;
}
if(!string.IsNullOrEmpty(args.IntermediateDirectory))
{
config.IntermediateDirectory = args.IntermediateDirectory;
}
if (args.NativeMode.HasValue)
{
config.NativeMode = args.NativeMode.Value;
}
if(!string.IsNullOrEmpty(args.AppDepSDKPath))
{
config.AppDepSDKPath = args.AppDepSDKPath;
}
if (!string.IsNullOrEmpty(args.IlcPath))
{
config.IlcPath = args.IlcPath;
}
if (!string.IsNullOrEmpty(args.LogPath))
{
config.LogPath = Path.GetFullPath(args.LogPath);
}
if (!string.IsNullOrEmpty(args.IlcArgs))
{
config.IlcArgs = Path.GetFullPath(args.IlcArgs);
}
foreach (var reference in args.ReferencePaths)
{
config.AddReference(reference);
}
foreach (var lib in args.LinkLibPaths)
{
config.AddLinkLibPath(lib);
}
return config;
}
} }
} }