2015-12-30 17:02:59 -08:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
using System.IO;
|
2015-12-30 17:02:59 -08:00
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-10-27 18:46:43 -07:00
|
|
|
|
using NuGet.Frameworks;
|
2015-12-30 17:02:59 -08:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Test.Utilities
|
|
|
|
|
{
|
2017-03-14 14:07:51 -07:00
|
|
|
|
public sealed class BuildCommand : DotnetCommand
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
private bool _captureOutput;
|
|
|
|
|
|
|
|
|
|
private string _configuration;
|
|
|
|
|
|
2017-01-05 22:55:43 -08:00
|
|
|
|
private string _framework;
|
2016-10-27 18:46:43 -07:00
|
|
|
|
|
|
|
|
|
private string _runtime;
|
|
|
|
|
|
|
|
|
|
private bool _noDependencies;
|
|
|
|
|
|
|
|
|
|
private DirectoryInfo _outputPath;
|
|
|
|
|
|
|
|
|
|
private FileInfo _projectFile;
|
|
|
|
|
|
|
|
|
|
private DirectoryInfo _workingDirectory;
|
|
|
|
|
|
|
|
|
|
public override CommandResult Execute(string args = "")
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
args = $"build {GetNoDependencies()} {GetProjectFile()} {GetOutputPath()} {GetConfiguration()} {GetFramework()} {GetRuntime()} {args}";
|
|
|
|
|
|
|
|
|
|
if (_workingDirectory != null)
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
this.WithWorkingDirectory(_workingDirectory.FullName);
|
2015-12-30 17:02:59 -08:00
|
|
|
|
}
|
2016-10-27 18:46:43 -07:00
|
|
|
|
|
|
|
|
|
if (_captureOutput)
|
2016-02-03 10:57:25 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
return base.ExecuteWithCapturedOutput(args);
|
2016-02-03 10:57:25 -08:00
|
|
|
|
}
|
2016-10-27 18:46:43 -07:00
|
|
|
|
else
|
2016-02-18 01:09:23 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
return base.Execute(args);
|
2016-02-18 01:09:23 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-30 17:02:59 -08:00
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
public override CommandResult ExecuteWithCapturedOutput(string args = "")
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
WithCapturedOutput();
|
2015-12-30 17:02:59 -08:00
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
return Execute(args);
|
2015-12-30 17:02:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
public BuildCommand WithCapturedOutput()
|
2016-03-01 17:42:44 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
_captureOutput = true;
|
|
|
|
|
|
|
|
|
|
return this;
|
2016-03-01 17:42:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
public BuildCommand WithConfiguration(string configuration)
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
_configuration = configuration;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-30 17:02:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
public BuildCommand WithFramework(NuGetFramework framework)
|
2017-01-05 22:55:43 -08:00
|
|
|
|
{
|
|
|
|
|
_framework = framework.GetShortFolderName();
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BuildCommand WithFramework(string framework)
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
_framework = framework;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-30 17:02:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
public BuildCommand WithRuntime(string runtime)
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
_runtime = runtime;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-30 17:02:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
public BuildCommand WithNoDependencies()
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
_noDependencies = true;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-30 17:02:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
public BuildCommand WithOutputPath(DirectoryInfo outputPath)
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
_outputPath = outputPath;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-30 17:02:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
public BuildCommand WithProjectDirectory(DirectoryInfo projectDirectory)
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
_workingDirectory = projectDirectory;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-30 17:02:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
public BuildCommand WithProjectFile(FileInfo projectFile)
|
2015-12-21 10:42:41 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
_projectFile = projectFile;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-21 10:42:41 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
public BuildCommand WithWorkingDirectory(DirectoryInfo workingDirectory)
|
2015-12-21 10:42:41 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
_workingDirectory = workingDirectory;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-21 10:42:41 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
private string GetConfiguration()
|
2016-02-05 15:29:34 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
if (_configuration == null)
|
2016-02-05 15:29:34 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
return null;
|
2016-02-05 15:29:34 -08:00
|
|
|
|
}
|
2016-10-27 18:46:43 -07:00
|
|
|
|
|
|
|
|
|
return $"--configuration {_configuration}";
|
2016-02-05 15:29:34 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
private string GetFramework()
|
2016-05-12 10:33:32 -07:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
if (_framework == null)
|
2016-05-12 10:33:32 -07:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
return null;
|
2016-05-12 10:33:32 -07:00
|
|
|
|
}
|
2016-10-27 18:46:43 -07:00
|
|
|
|
|
2017-01-05 22:55:43 -08:00
|
|
|
|
return $"--framework {_framework}";
|
2016-05-12 10:33:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
private string GetRuntime()
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
if (_runtime == null)
|
2016-04-29 22:19:35 -05:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
return null;
|
2016-04-29 22:19:35 -05:00
|
|
|
|
}
|
2015-12-30 17:02:59 -08:00
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
return $"--runtime {_runtime}";
|
2015-12-30 17:02:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
private string GetNoDependencies()
|
2015-12-30 17:02:59 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
if (!_noDependencies)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-12-30 17:02:59 -08:00
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
return "--no-dependencies";
|
2016-01-08 11:03:14 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
private string GetOutputPath()
|
2016-03-15 11:50:14 -07:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
if (_outputPath == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-03-15 11:50:14 -07:00
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
return $"\"{_outputPath.FullName}\"";
|
2016-03-16 16:39:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
private string GetProjectFile()
|
2016-03-16 16:39:59 -07:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
if (_projectFile == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-12-30 17:02:59 -08:00
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
return $"\"{_projectFile.FullName}\"";
|
2015-12-30 17:02:59 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|