2015-12-31 01:02:59 +00: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-28 01:46:43 +00:00
|
|
|
|
using System.IO;
|
2015-12-31 01:02:59 +00:00
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-10-28 01:46:43 +00:00
|
|
|
|
using NuGet.Frameworks;
|
2015-12-31 01:02:59 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Test.Utilities
|
|
|
|
|
{
|
|
|
|
|
public sealed class BuildCommand : TestCommand
|
|
|
|
|
{
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
private bool _captureOutput;
|
|
|
|
|
|
|
|
|
|
private string _configuration;
|
|
|
|
|
|
2017-01-06 06:55:43 +00:00
|
|
|
|
private string _framework;
|
2016-10-28 01:46:43 +00:00
|
|
|
|
|
|
|
|
|
private string _runtime;
|
|
|
|
|
|
|
|
|
|
private bool _noDependencies;
|
|
|
|
|
|
|
|
|
|
private DirectoryInfo _outputPath;
|
|
|
|
|
|
|
|
|
|
private FileInfo _projectFile;
|
|
|
|
|
|
|
|
|
|
private DirectoryInfo _workingDirectory;
|
|
|
|
|
|
|
|
|
|
public BuildCommand()
|
|
|
|
|
: base("dotnet")
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public override CommandResult Execute(string args = "")
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
args = $"build {GetNoDependencies()} {GetProjectFile()} {GetOutputPath()} {GetConfiguration()} {GetFramework()} {GetRuntime()} {args}";
|
|
|
|
|
|
|
|
|
|
if (_workingDirectory != null)
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
this.WithWorkingDirectory(_workingDirectory.FullName);
|
2015-12-31 01:02:59 +00:00
|
|
|
|
}
|
2016-10-28 01:46:43 +00:00
|
|
|
|
|
|
|
|
|
if (_captureOutput)
|
2016-02-03 18:57:25 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
return base.ExecuteWithCapturedOutput(args);
|
2016-02-03 18:57:25 +00:00
|
|
|
|
}
|
2016-10-28 01:46:43 +00:00
|
|
|
|
else
|
2016-02-18 09:09:23 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
return base.Execute(args);
|
2016-02-18 09:09:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-31 01:02:59 +00:00
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public override CommandResult ExecuteWithCapturedOutput(string args = "")
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
WithCapturedOutput();
|
2015-12-31 01:02:59 +00:00
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
return Execute(args);
|
2015-12-31 01:02:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public BuildCommand WithCapturedOutput()
|
2016-03-02 01:42:44 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
_captureOutput = true;
|
|
|
|
|
|
|
|
|
|
return this;
|
2016-03-02 01:42:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public BuildCommand WithConfiguration(string configuration)
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
_configuration = configuration;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-31 01:02:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public BuildCommand WithFramework(NuGetFramework framework)
|
2017-01-06 06:55:43 +00:00
|
|
|
|
{
|
|
|
|
|
_framework = framework.GetShortFolderName();
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BuildCommand WithFramework(string framework)
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
_framework = framework;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-31 01:02:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public BuildCommand WithRuntime(string runtime)
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
_runtime = runtime;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-31 01:02:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public BuildCommand WithNoDependencies()
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
_noDependencies = true;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-31 01:02:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public BuildCommand WithOutputPath(DirectoryInfo outputPath)
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
_outputPath = outputPath;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-31 01:02:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public BuildCommand WithProjectDirectory(DirectoryInfo projectDirectory)
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
_workingDirectory = projectDirectory;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-31 01:02:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public BuildCommand WithProjectFile(FileInfo projectFile)
|
2015-12-21 18:42:41 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
_projectFile = projectFile;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-21 18:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public BuildCommand WithWorkingDirectory(DirectoryInfo workingDirectory)
|
2015-12-21 18:42:41 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
_workingDirectory = workingDirectory;
|
|
|
|
|
|
|
|
|
|
return this;
|
2015-12-21 18:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
private string GetConfiguration()
|
2016-02-05 23:29:34 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
if (_configuration == null)
|
2016-02-05 23:29:34 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
return null;
|
2016-02-05 23:29:34 +00:00
|
|
|
|
}
|
2016-10-28 01:46:43 +00:00
|
|
|
|
|
|
|
|
|
return $"--configuration {_configuration}";
|
2016-02-05 23:29:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
private string GetFramework()
|
2016-05-12 17:33:32 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
if (_framework == null)
|
2016-05-12 17:33:32 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
return null;
|
2016-05-12 17:33:32 +00:00
|
|
|
|
}
|
2016-10-28 01:46:43 +00:00
|
|
|
|
|
2017-01-06 06:55:43 +00:00
|
|
|
|
return $"--framework {_framework}";
|
2016-05-12 17:33:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
private string GetRuntime()
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
if (_runtime == null)
|
2016-04-30 03:19:35 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
return null;
|
2016-04-30 03:19:35 +00:00
|
|
|
|
}
|
2015-12-31 01:02:59 +00:00
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
return $"--runtime {_runtime}";
|
2015-12-31 01:02:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
private string GetNoDependencies()
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
if (!_noDependencies)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-12-31 01:02:59 +00:00
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
return "--no-dependencies";
|
2016-01-08 19:03:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
private string GetOutputPath()
|
2016-03-15 18:50:14 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
if (_outputPath == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-03-15 18:50:14 +00:00
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
return $"\"{_outputPath.FullName}\"";
|
2016-03-16 23:39:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
private string GetProjectFile()
|
2016-03-16 23:39:59 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
if (_projectFile == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-12-31 01:02:59 +00:00
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
return $"\"{_projectFile.FullName}\"";
|
2015-12-31 01:02:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|