2015-12-15 01:39:29 +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.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
using Microsoft.DotNet.ProjectModel;
|
|
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
2015-12-19 00:39:43 +00:00
|
|
|
|
using Microsoft.Extensions.PlatformAbstractions;
|
2015-12-15 01:39:29 +00:00
|
|
|
|
|
2015-12-31 01:02:59 +00:00
|
|
|
|
namespace Microsoft.DotNet.Tools.Test.Utilities
|
2015-12-15 01:39:29 +00:00
|
|
|
|
{
|
|
|
|
|
public sealed class PublishCommand : TestCommand
|
|
|
|
|
{
|
2016-02-16 23:30:39 +00:00
|
|
|
|
private const string PublishSubfolderName = "publish";
|
|
|
|
|
|
2015-12-15 01:39:29 +00:00
|
|
|
|
private Project _project;
|
|
|
|
|
private string _path;
|
|
|
|
|
private string _framework;
|
|
|
|
|
private string _runtime;
|
|
|
|
|
private string _config;
|
|
|
|
|
private string _output;
|
|
|
|
|
|
|
|
|
|
public PublishCommand(string projectPath, string framework="", string runtime="", string output="", string config="")
|
|
|
|
|
: base("dotnet")
|
|
|
|
|
{
|
|
|
|
|
_path = projectPath;
|
|
|
|
|
_project = ProjectReader.GetProject(projectPath);
|
|
|
|
|
_framework = framework;
|
|
|
|
|
_runtime = runtime;
|
|
|
|
|
_output = output;
|
2016-01-11 21:40:47 +00:00
|
|
|
|
_config = config;
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override CommandResult Execute(string args="")
|
2016-01-11 21:40:47 +00:00
|
|
|
|
{
|
2015-12-15 01:39:29 +00:00
|
|
|
|
args = $"publish {BuildArgs()} {args}";
|
|
|
|
|
return base.Execute(args);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 21:40:47 +00:00
|
|
|
|
public override CommandResult ExecuteWithCapturedOutput(string args = "")
|
|
|
|
|
{
|
|
|
|
|
args = $"publish {BuildArgs()} {args}";
|
|
|
|
|
return base.ExecuteWithCapturedOutput(args);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-15 01:39:29 +00:00
|
|
|
|
public string ProjectName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _project.Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string BuildRelativeOutputPath()
|
|
|
|
|
{
|
2016-01-11 21:40:47 +00:00
|
|
|
|
// lets try to build an approximate output path
|
2015-12-15 01:39:29 +00:00
|
|
|
|
string config = string.IsNullOrEmpty(_config) ? "Debug" : _config;
|
2016-01-11 21:40:47 +00:00
|
|
|
|
string framework = string.IsNullOrEmpty(_framework) ?
|
2015-12-15 01:39:29 +00:00
|
|
|
|
_project.GetTargetFrameworks().First().FrameworkName.GetShortFolderName() : _framework;
|
2015-12-19 00:39:43 +00:00
|
|
|
|
string runtime = string.IsNullOrEmpty(_runtime) ? PlatformServices.Default.Runtime.GetLegacyRestoreRuntimeIdentifier() : _runtime;
|
2016-02-16 23:30:39 +00:00
|
|
|
|
string output = Path.Combine(config, framework, runtime, PublishSubfolderName);
|
2015-12-15 01:39:29 +00:00
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DirectoryInfo GetOutputDirectory()
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(_output))
|
|
|
|
|
{
|
2016-01-26 14:39:13 +00:00
|
|
|
|
return new DirectoryInfo(_output);
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 23:41:46 +00:00
|
|
|
|
string output = Path.Combine(_project.ProjectDirectory, "bin", BuildRelativeOutputPath());
|
2015-12-15 01:39:29 +00:00
|
|
|
|
return new DirectoryInfo(output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetOutputExecutable()
|
|
|
|
|
{
|
|
|
|
|
var result = _project.Name;
|
|
|
|
|
result += RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : "";
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string BuildArgs()
|
|
|
|
|
{
|
|
|
|
|
return $"{_path} {GetFrameworkOption()} {GetRuntimeOption()} {GetOutputOption()} {GetConfigOption()}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetFrameworkOption()
|
|
|
|
|
{
|
|
|
|
|
return string.IsNullOrEmpty(_framework) ? "" : $"-f {_framework}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetRuntimeOption()
|
|
|
|
|
{
|
|
|
|
|
return string.IsNullOrEmpty(_runtime) ? "" : $"-r {_runtime}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetOutputOption()
|
|
|
|
|
{
|
2016-01-22 22:05:02 +00:00
|
|
|
|
return string.IsNullOrEmpty(_output) ? "" : $"-o \"{_output}\"";
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetConfigOption()
|
|
|
|
|
{
|
|
|
|
|
return string.IsNullOrEmpty(_config) ? "" : $"-c {_output}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|