Merge pull request #4286 from eerhardt/SelfContainedPublish
Add support for publishing self-contained applications.
This commit is contained in:
commit
f12f274f81
4 changed files with 156 additions and 40 deletions
|
@ -1,14 +1,12 @@
|
||||||
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
// 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.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Microsoft.DotNet.Cli.CommandLine;
|
using Microsoft.DotNet.Cli.CommandLine;
|
||||||
using Microsoft.DotNet.Cli.Utils;
|
using Microsoft.DotNet.Cli.Utils;
|
||||||
using Microsoft.DotNet.Tools.MSBuild;
|
|
||||||
|
|
||||||
namespace Microsoft.DotNet.Tools.Publish3
|
namespace Microsoft.DotNet.Tools.Publish3
|
||||||
{
|
{
|
||||||
public class Publish3Command
|
public partial class Publish3Command
|
||||||
{
|
{
|
||||||
public static int Run(string[] args)
|
public static int Run(string[] args)
|
||||||
{
|
{
|
||||||
|
@ -47,43 +45,17 @@ namespace Microsoft.DotNet.Tools.Publish3
|
||||||
|
|
||||||
app.OnExecute(() =>
|
app.OnExecute(() =>
|
||||||
{
|
{
|
||||||
List<string> msbuildArgs = new List<string>();
|
Publish3Command publish = new Publish3Command();
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(projectArgument.Value))
|
publish.ProjectPath = projectArgument.Value;
|
||||||
{
|
publish.Framework = frameworkOption.Value();
|
||||||
msbuildArgs.Add(projectArgument.Value);
|
publish.Runtime = runtimeOption.Value();
|
||||||
}
|
publish.OutputPath = outputOption.Value();
|
||||||
|
publish.Configuration = configurationOption.Value();
|
||||||
|
publish.VersionSuffix = versionSuffixOption.Value();
|
||||||
|
publish.ExtraMSBuildArguments = app.RemainingArguments;
|
||||||
|
|
||||||
msbuildArgs.Add("/t:Publish");
|
return publish.Execute();
|
||||||
|
|
||||||
if (frameworkOption.HasValue())
|
|
||||||
{
|
|
||||||
msbuildArgs.Add($"/p:TargetFramework={frameworkOption.Value()}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (runtimeOption.HasValue())
|
|
||||||
{
|
|
||||||
msbuildArgs.Add($"/p:RuntimeIdentifier={runtimeOption.Value()}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (outputOption.HasValue())
|
|
||||||
{
|
|
||||||
msbuildArgs.Add($"/p:PublishDir={outputOption.Value()}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (configurationOption.HasValue())
|
|
||||||
{
|
|
||||||
msbuildArgs.Add($"/p:Configuration={configurationOption.Value()}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (versionSuffixOption.HasValue())
|
|
||||||
{
|
|
||||||
msbuildArgs.Add($"/p:VersionSuffix={versionSuffixOption.Value()}");
|
|
||||||
}
|
|
||||||
|
|
||||||
msbuildArgs.AddRange(app.RemainingArguments);
|
|
||||||
|
|
||||||
return new MSBuildForwardingApp(msbuildArgs).Execute();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return app.Execute(args);
|
return app.Execute(args);
|
||||||
|
|
88
src/dotnet/commands/dotnet-publish3/Publish3Command.cs
Normal file
88
src/dotnet/commands/dotnet-publish3/Publish3Command.cs
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
// 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.Collections.Generic;
|
||||||
|
using Microsoft.DotNet.Cli.Utils;
|
||||||
|
using Microsoft.DotNet.Tools.MSBuild;
|
||||||
|
using Microsoft.DotNet.Tools.Restore3;
|
||||||
|
|
||||||
|
namespace Microsoft.DotNet.Tools.Publish3
|
||||||
|
{
|
||||||
|
public partial class Publish3Command
|
||||||
|
{
|
||||||
|
public string ProjectPath { get; set; }
|
||||||
|
public string Framework { get; set; }
|
||||||
|
public string Runtime { get; set; }
|
||||||
|
public string OutputPath { get; set; }
|
||||||
|
public string Configuration { get; set; }
|
||||||
|
public string VersionSuffix { get; set; }
|
||||||
|
|
||||||
|
public List<string> ExtraMSBuildArguments { get; set; }
|
||||||
|
|
||||||
|
private Publish3Command()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Execute()
|
||||||
|
{
|
||||||
|
int restoreResult = EnsureRestored();
|
||||||
|
if (restoreResult != 0)
|
||||||
|
{
|
||||||
|
throw new GracefulException("Restore failed. Please fix the errors and try publishing again.");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<string> msbuildArgs = new List<string>();
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(ProjectPath))
|
||||||
|
{
|
||||||
|
msbuildArgs.Add(ProjectPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
msbuildArgs.Add("/t:Publish");
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(Framework))
|
||||||
|
{
|
||||||
|
msbuildArgs.Add($"/p:TargetFramework={Framework}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(Runtime))
|
||||||
|
{
|
||||||
|
msbuildArgs.Add($"/p:RuntimeIdentifier={Runtime}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(OutputPath))
|
||||||
|
{
|
||||||
|
msbuildArgs.Add($"/p:PublishDir={OutputPath}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(Configuration))
|
||||||
|
{
|
||||||
|
msbuildArgs.Add($"/p:Configuration={Configuration}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(VersionSuffix))
|
||||||
|
{
|
||||||
|
msbuildArgs.Add($"/p:VersionSuffix={VersionSuffix}");
|
||||||
|
}
|
||||||
|
|
||||||
|
msbuildArgs.AddRange(ExtraMSBuildArguments);
|
||||||
|
|
||||||
|
return new MSBuildForwardingApp(msbuildArgs).Execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ensures that the project has been restored for the specified runtime.
|
||||||
|
/// </summary>
|
||||||
|
private int EnsureRestored()
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(Runtime))
|
||||||
|
{
|
||||||
|
result = Restore3Command.Run(new[] { $"/p:RuntimeIdentifiers={Runtime}" });
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,21 +7,46 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
|
||||||
{
|
{
|
||||||
public sealed class Publish3Command : TestCommand
|
public sealed class Publish3Command : TestCommand
|
||||||
{
|
{
|
||||||
|
private string _framework;
|
||||||
|
private string _runtime;
|
||||||
|
|
||||||
public Publish3Command()
|
public Publish3Command()
|
||||||
: base("dotnet")
|
: base("dotnet")
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Publish3Command WithFramework(string framework)
|
||||||
|
{
|
||||||
|
_framework = framework;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Publish3Command WithRuntime(string runtime)
|
||||||
|
{
|
||||||
|
_runtime = runtime;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public override CommandResult Execute(string args = "")
|
public override CommandResult Execute(string args = "")
|
||||||
{
|
{
|
||||||
args = $"publish3 {args}";
|
args = $"publish3 {args} {BuildArgs()}";
|
||||||
return base.Execute(args);
|
return base.Execute(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override CommandResult ExecuteWithCapturedOutput(string args = "")
|
public override CommandResult ExecuteWithCapturedOutput(string args = "")
|
||||||
{
|
{
|
||||||
args = $"publish3 {args}";
|
args = $"publish3 {args} {BuildArgs()}";
|
||||||
return base.ExecuteWithCapturedOutput(args);
|
return base.ExecuteWithCapturedOutput(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string BuildArgs()
|
||||||
|
{
|
||||||
|
return string.Join(" ",
|
||||||
|
FrameworkOption,
|
||||||
|
RuntimeOption);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string FrameworkOption => string.IsNullOrEmpty(_framework) ? "" : $"-f {_framework}";
|
||||||
|
private string RuntimeOption => string.IsNullOrEmpty(_runtime) ? "" : $"-r {_runtime}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
|
using Microsoft.DotNet.Cli.Utils;
|
||||||
|
using Microsoft.DotNet.PlatformAbstractions;
|
||||||
using Microsoft.DotNet.Tools.Test.Utilities;
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
@ -42,5 +44,34 @@ namespace Microsoft.DotNet.Cli.Publish3.Tests
|
||||||
.And
|
.And
|
||||||
.HaveStdOutContaining("Hello World");
|
.HaveStdOutContaining("Hello World");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ItPublishesARunnableSelfContainedApp()
|
||||||
|
{
|
||||||
|
var testAppName = "MSBuildTestApp";
|
||||||
|
var testInstance = TestAssetsManager
|
||||||
|
.CreateTestInstance(testAppName);
|
||||||
|
|
||||||
|
var testProjectDirectory = testInstance.TestRoot;
|
||||||
|
var rid = RuntimeEnvironment.GetRuntimeIdentifier();
|
||||||
|
|
||||||
|
new Publish3Command()
|
||||||
|
.WithFramework("netcoreapp1.0")
|
||||||
|
.WithRuntime(rid)
|
||||||
|
.WithWorkingDirectory(testProjectDirectory)
|
||||||
|
.Execute()
|
||||||
|
.Should()
|
||||||
|
.Pass();
|
||||||
|
|
||||||
|
var configuration = Environment.GetEnvironmentVariable("CONFIGURATION") ?? "Debug";
|
||||||
|
var outputProgram = Path.Combine(testProjectDirectory, "bin", configuration, "netcoreapp1.0", rid, "publish", $"{testAppName}{Constants.ExeSuffix}");
|
||||||
|
|
||||||
|
new TestCommand(outputProgram)
|
||||||
|
.ExecuteWithCapturedOutput()
|
||||||
|
.Should()
|
||||||
|
.Pass()
|
||||||
|
.And
|
||||||
|
.HaveStdOutContaining("Hello World");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue