reduce some repetition

This commit is contained in:
Krzysztof Wicher 2017-02-23 11:24:36 -08:00
parent de7587782e
commit c75d2e446e
8 changed files with 131 additions and 291 deletions

View file

@ -7,11 +7,18 @@ using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Cli;
using System.Diagnostics;
using System;
using System.IO;
namespace Microsoft.DotNet.Tools.Cache
{
public partial class CacheCommand
public partial class CacheCommand : MSBuildForwardingApp
{
private CacheCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
: base(msbuildArgs, msbuildPath)
{
}
public static CacheCommand FromArgs(string[] args, string msbuildPath = null)
{
DebugHelper.HandleDebugSwitch(ref args);
@ -58,32 +65,72 @@ namespace Microsoft.DotNet.Tools.Cache
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(app);
var cache = new CacheCommand(msbuildPath);
bool commandExecuted = false;
List<string> msbuildArgs = null;
app.OnExecute(() =>
{
commandExecuted = true;
cache.Framework = frameworkOption.Value();
cache.Runtime = runtimeOption.Value();
cache.OutputPath = outputOption.Value();
cache.FrameworkVersion = fxOption.Value();
cache.Verbosity = verbosityOption.Value();
cache.SkipOptimization = skipOptimizationOption.HasValue();
cache.IntermediateDir = workingDir.Value();
cache.PreserveIntermediateDir = preserveWorkingDir.HasValue();
cache.ExtraMSBuildArguments = app.RemainingArguments;
cache.ProjectArgument = projectArgument.Value();
msbuildArgs = new List<string>();
if (string.IsNullOrEmpty(projectArgument.Value()))
{
throw new InvalidOperationException(LocalizableStrings.SpecifyEntries);
}
msbuildArgs.Add("/t:ComposeCache");
msbuildArgs.Add(projectArgument.Value());
if (!string.IsNullOrEmpty(frameworkOption.Value()))
{
msbuildArgs.Add($"/p:TargetFramework={frameworkOption.Value()}");
}
if (!string.IsNullOrEmpty(runtimeOption.Value()))
{
msbuildArgs.Add($"/p:RuntimeIdentifier={runtimeOption.Value()}");
}
if (!string.IsNullOrEmpty(outputOption.Value()))
{
var outputPath = Path.GetFullPath(outputOption.Value());
msbuildArgs.Add($"/p:ComposeDir={outputPath}");
}
if (!string.IsNullOrEmpty(fxOption.Value()))
{
msbuildArgs.Add($"/p:FX_Version={fxOption.Value()}");
}
if (!string.IsNullOrEmpty(workingDir.Value()))
{
msbuildArgs.Add($"/p:ComposeWorkingDir={workingDir.Value()}");
}
if (skipOptimizationOption.HasValue())
{
msbuildArgs.Add($"/p:SkipOptimization={skipOptimizationOption.HasValue()}");
}
if (preserveWorkingDir.HasValue())
{
msbuildArgs.Add($"/p:PreserveComposeWorkingDir={preserveWorkingDir.HasValue()}");
}
if (!string.IsNullOrEmpty(verbosityOption.Value()))
{
msbuildArgs.Add($"/verbosity:{verbosityOption.Value()}");
}
msbuildArgs.AddRange(app.RemainingArguments);
return 0;
});
int exitCode = app.Execute(args);
if (!commandExecuted)
if (msbuildArgs == null)
{
throw new CommandCreationException(exitCode);
}
return cache;
return new CacheCommand(msbuildArgs, msbuildPath);
}
public static int Run(string[] args)
@ -102,15 +149,5 @@ namespace Microsoft.DotNet.Tools.Cache
return cmd.Execute();
}
public ProcessStartInfo GetProcessStartInfo()
{
return CreateForwardingApp(_msbuildPath).GetProcessStartInfo();
}
public int Execute()
{
return GetProcessStartInfo().Execute();
}
}
}