Add support for publishing self-contained applications.

Fix #4204
This commit is contained in:
Eric Erhardt 2016-09-30 17:47:23 -05:00
parent a3a58423d1
commit a7366f5864
6 changed files with 193 additions and 40 deletions

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp1.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.Sdk">
<Version>1.0.0-alpha-20160930-2</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View file

@ -0,0 +1,12 @@
using System;
namespace MSBuildTestApp
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

View file

@ -1,14 +1,12 @@
// 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.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
namespace Microsoft.DotNet.Tools.Publish3
{
public class Publish3Command
public partial class Publish3Command
{
public static int Run(string[] args)
{
@ -47,43 +45,17 @@ namespace Microsoft.DotNet.Tools.Publish3
app.OnExecute(() =>
{
List<string> msbuildArgs = new List<string>();
Publish3Command publish = new Publish3Command();
if (!string.IsNullOrEmpty(projectArgument.Value))
{
msbuildArgs.Add(projectArgument.Value);
}
publish.ProjectPath = projectArgument.Value;
publish.Framework = frameworkOption.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");
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 publish.Execute();
});
return app.Execute(args);

View 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;
}
}
}

View file

@ -7,21 +7,46 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
{
public sealed class Publish3Command : TestCommand
{
private string _framework;
private string _runtime;
public Publish3Command()
: 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 = "")
{
args = $"publish3 {args}";
args = $"publish3 {args} {BuildArgs()}";
return base.Execute(args);
}
public override CommandResult ExecuteWithCapturedOutput(string args = "")
{
args = $"publish3 {args}";
args = $"publish3 {args} {BuildArgs()}";
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}";
}
}

View file

@ -4,6 +4,8 @@
using System;
using System.IO;
using FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.DotNet.Tools.Test.Utilities;
using Xunit;
@ -42,5 +44,34 @@ namespace Microsoft.DotNet.Cli.Publish3.Tests
.And
.HaveStdOutContaining("Hello World");
}
[Fact]
public void ItPublishesARunnableSelfContainedApp()
{
var testAppName = "MSBuildTestAppPackageRefs";
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");
}
}
}