Make dotnet-publish slightly more readable
This commit is contained in:
parent
8f228f446a
commit
d5791bfa1c
2 changed files with 214 additions and 191 deletions
|
@ -1,16 +1,11 @@
|
||||||
// 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;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using Microsoft.Dnx.Runtime.Common.CommandLine;
|
using Microsoft.Dnx.Runtime.Common.CommandLine;
|
||||||
using Microsoft.DotNet.Cli.Utils;
|
using Microsoft.DotNet.Cli.Utils;
|
||||||
using Microsoft.DotNet.ProjectModel;
|
using Microsoft.DotNet.ProjectModel;
|
||||||
using Microsoft.DotNet.ProjectModel.Compilation;
|
using System;
|
||||||
using Microsoft.DotNet.ProjectModel.Graph;
|
using System.IO;
|
||||||
using NuGet.Frameworks;
|
|
||||||
|
|
||||||
namespace Microsoft.DotNet.Tools.Publish
|
namespace Microsoft.DotNet.Tools.Publish
|
||||||
{
|
{
|
||||||
|
@ -30,58 +25,32 @@ namespace Microsoft.DotNet.Tools.Publish
|
||||||
var runtime = app.Option("-r|--runtime <RUNTIME_IDENTIFIER>", "Target runtime to publish for", CommandOptionType.SingleValue);
|
var runtime = app.Option("-r|--runtime <RUNTIME_IDENTIFIER>", "Target runtime to publish for", CommandOptionType.SingleValue);
|
||||||
var output = app.Option("-o|--output <OUTPUT_PATH>", "Path in which to publish the app", CommandOptionType.SingleValue);
|
var output = app.Option("-o|--output <OUTPUT_PATH>", "Path in which to publish the app", CommandOptionType.SingleValue);
|
||||||
var configuration = app.Option("-c|--configuration <CONFIGURATION>", "Configuration under which to build", CommandOptionType.SingleValue);
|
var configuration = app.Option("-c|--configuration <CONFIGURATION>", "Configuration under which to build", CommandOptionType.SingleValue);
|
||||||
var project = app.Argument("<PROJECT>", "The project to publish, defaults to the current directory. Can be a path to a project.json or a project directory");
|
var projectPath = app.Argument("<PROJECT>", "The project to publish, defaults to the current directory. Can be a path to a project.json or a project directory");
|
||||||
|
|
||||||
app.OnExecute(() =>
|
app.OnExecute(() =>
|
||||||
{
|
{
|
||||||
NuGetFramework nugetframework = null;
|
var publish = new PublishCommand();
|
||||||
|
|
||||||
if (framework.HasValue())
|
publish.Framework = framework.Value();
|
||||||
|
// TODO: Remove default once xplat publish is enabled.
|
||||||
|
publish.Runtime = runtime.Value() ?? RuntimeIdentifier.Current;
|
||||||
|
publish.OutputPath = output.Value();
|
||||||
|
publish.Configuration = configuration.Value() ?? Constants.DefaultConfiguration;
|
||||||
|
|
||||||
|
publish.ProjectPath = projectPath.Value;
|
||||||
|
if (string.IsNullOrEmpty(publish.ProjectPath))
|
||||||
{
|
{
|
||||||
nugetframework = NuGetFramework.Parse(framework.Value());
|
publish.ProjectPath = Directory.GetCurrentDirectory();
|
||||||
|
|
||||||
if (nugetframework.IsUnsupported)
|
|
||||||
{
|
|
||||||
Reporter.Output.WriteLine($"Unsupported framework {framework.Value()}.".Red());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Remove this once xplat publish is enabled.
|
if (!publish.TryPrepareForPublish())
|
||||||
if (!runtime.HasValue())
|
|
||||||
{
|
{
|
||||||
runtime.Values.Add(RuntimeIdentifier.Current);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Locate the project and get the name and full path
|
|
||||||
var path = project.Value;
|
|
||||||
if (string.IsNullOrEmpty(path))
|
|
||||||
{
|
|
||||||
path = Directory.GetCurrentDirectory();
|
|
||||||
}
|
|
||||||
|
|
||||||
var projectContexts = ProjectContext.CreateContextForEachTarget(path);
|
|
||||||
projectContexts = GetMatchingProjectContexts(projectContexts, nugetframework, runtime.Value());
|
|
||||||
|
|
||||||
if (projectContexts.Count() == 0)
|
|
||||||
{
|
|
||||||
string errMsg = $"'{project.Value}' cannot be published";
|
|
||||||
if (framework.HasValue() || runtime.HasValue())
|
|
||||||
{
|
|
||||||
errMsg += $" for '{framework.Value()}' '{runtime.Value()}'";
|
|
||||||
}
|
|
||||||
|
|
||||||
Reporter.Output.WriteLine(errMsg.Red());
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = 0;
|
publish.PublishAllProjects();
|
||||||
foreach (var projectContext in projectContexts)
|
Reporter.Output.WriteLine($"Published {publish.NumberOfPublishedProjects}/{publish.NumberOfProjects} projects successfully");
|
||||||
{
|
return (publish.NumberOfPublishedProjects == publish.NumberOfProjects) ? 0 : 1;
|
||||||
result += Publish(projectContext, output.Value(), configuration.Value() ?? Constants.DefaultConfiguration);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -90,151 +59,10 @@ namespace Microsoft.DotNet.Tools.Publish
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
Reporter.Error.WriteLine(ex.Message.Red());
|
||||||
Console.Error.WriteLine(ex);
|
Reporter.Verbose.WriteLine(ex.ToString().Yellow());
|
||||||
#else
|
|
||||||
Console.Error.WriteLine(ex.Message);
|
|
||||||
#endif
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool CheckArg(CommandOption argument)
|
|
||||||
{
|
|
||||||
if (!argument.HasValue())
|
|
||||||
{
|
|
||||||
Reporter.Error.WriteLine($"Missing required argument: {argument.LongName.Red().Bold()}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// return the matching framework/runtime ProjectContext.
|
|
||||||
// if 'nugetframework' or 'runtime' is null or empty then it matches with any.
|
|
||||||
private static IEnumerable<ProjectContext> GetMatchingProjectContexts(IEnumerable<ProjectContext> contexts, NuGetFramework framework, string runtimeIdentifier)
|
|
||||||
{
|
|
||||||
var matchingContexts = contexts.Where(context =>
|
|
||||||
{
|
|
||||||
if (context.TargetFramework == null || string.IsNullOrEmpty(context.RuntimeIdentifier))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(runtimeIdentifier) || runtimeIdentifier.Equals(context.RuntimeIdentifier))
|
|
||||||
{
|
|
||||||
if (framework == null || framework.Equals(context.TargetFramework))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
return matchingContexts;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Publish the project for given 'framework (ex - dnxcore50)' and 'runtimeID (ex - win7-x64)'
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="context">project that is to be published</param>
|
|
||||||
/// <param name="outputPath">Location of published files</param>
|
|
||||||
/// <param name="configuration">Debug or Release</param>
|
|
||||||
/// <returns>Return 0 if successful else return non-zero</returns>
|
|
||||||
private static int Publish(ProjectContext context, string outputPath, string configuration)
|
|
||||||
{
|
|
||||||
Reporter.Output.WriteLine($"Publishing {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}/{context.RuntimeIdentifier.Yellow()}");
|
|
||||||
|
|
||||||
var options = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
|
|
||||||
|
|
||||||
// Generate the output path
|
|
||||||
if (string.IsNullOrEmpty(outputPath))
|
|
||||||
{
|
|
||||||
outputPath = Path.Combine(
|
|
||||||
context.ProjectFile.ProjectDirectory,
|
|
||||||
Constants.BinDirectoryName,
|
|
||||||
configuration,
|
|
||||||
context.TargetFramework.GetTwoDigitShortFolderName(),
|
|
||||||
context.RuntimeIdentifier);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Directory.Exists(outputPath))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(outputPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compile the project (and transitively, all it's dependencies)
|
|
||||||
var result = Command.Create("dotnet-compile",
|
|
||||||
$"--framework \"{context.TargetFramework.DotNetFrameworkName}\" " +
|
|
||||||
$"--output \"{outputPath}\" " +
|
|
||||||
$"--configuration \"{configuration}\" " +
|
|
||||||
"--no-host " +
|
|
||||||
$"\"{context.ProjectFile.ProjectDirectory}\"")
|
|
||||||
.ForwardStdErr()
|
|
||||||
.ForwardStdOut()
|
|
||||||
.Execute();
|
|
||||||
|
|
||||||
if (result.ExitCode != 0)
|
|
||||||
{
|
|
||||||
return result.ExitCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use a library exporter to collect publish assets
|
|
||||||
var exporter = context.CreateExporter(configuration);
|
|
||||||
|
|
||||||
foreach (var export in exporter.GetAllExports())
|
|
||||||
{
|
|
||||||
// Skip copying project references
|
|
||||||
if (export.Library is ProjectDescription)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Reporter.Verbose.WriteLine($"Publishing {export.Library.Identity.ToString().Green().Bold()} ...");
|
|
||||||
|
|
||||||
PublishFiles(export.RuntimeAssemblies, outputPath);
|
|
||||||
PublishFiles(export.NativeLibraries, outputPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Publish a host if this is an application
|
|
||||||
if (options.EmitEntryPoint.GetValueOrDefault())
|
|
||||||
{
|
|
||||||
Reporter.Verbose.WriteLine($"Making {context.ProjectFile.Name.Cyan()} runnable ...");
|
|
||||||
PublishHost(context, outputPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
Reporter.Output.WriteLine($"Published to {outputPath}".Green().Bold());
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int PublishHost(ProjectContext context, string outputPath)
|
|
||||||
{
|
|
||||||
if (context.TargetFramework.IsDesktop())
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
var hostPath = Path.Combine(AppContext.BaseDirectory, Constants.HostExecutableName);
|
|
||||||
if (!File.Exists(hostPath))
|
|
||||||
{
|
|
||||||
Reporter.Error.WriteLine($"Cannot find {Constants.HostExecutableName} in the dotnet directory.".Red());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
var outputExe = Path.Combine(outputPath, context.ProjectFile.Name + Constants.ExeSuffix);
|
|
||||||
|
|
||||||
// Copy the host
|
|
||||||
File.Copy(hostPath, outputExe, overwrite: true);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void PublishFiles(IEnumerable<LibraryAsset> files, string outputPath)
|
|
||||||
{
|
|
||||||
foreach (var file in files)
|
|
||||||
{
|
|
||||||
File.Copy(file.ResolvedPath, Path.Combine(outputPath, Path.GetFileName(file.ResolvedPath)), overwrite: true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
195
src/Microsoft.DotNet.Tools.Publish/PublishCommand.cs
Normal file
195
src/Microsoft.DotNet.Tools.Publish/PublishCommand.cs
Normal file
|
@ -0,0 +1,195 @@
|
||||||
|
// 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 Microsoft.DotNet.Cli.Utils;
|
||||||
|
using Microsoft.DotNet.ProjectModel;
|
||||||
|
using Microsoft.DotNet.ProjectModel.Compilation;
|
||||||
|
using NuGet.Frameworks;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Microsoft.DotNet.Tools.Publish
|
||||||
|
{
|
||||||
|
public class PublishCommand
|
||||||
|
{
|
||||||
|
public string ProjectPath { get; set; }
|
||||||
|
public string Configuration { get; set; }
|
||||||
|
public string OutputPath { get; set; }
|
||||||
|
public string Framework { get; set; }
|
||||||
|
public string Runtime { get; set; }
|
||||||
|
public NuGetFramework NugetFramework { get; set; }
|
||||||
|
public IEnumerable<ProjectContext> ProjectContexts { get; set; }
|
||||||
|
|
||||||
|
public int NumberOfProjects { get; private set; }
|
||||||
|
public int NumberOfPublishedProjects { get; private set; }
|
||||||
|
|
||||||
|
public bool TryPrepareForPublish()
|
||||||
|
{
|
||||||
|
if (Framework != null)
|
||||||
|
{
|
||||||
|
NugetFramework = NuGetFramework.Parse(Framework);
|
||||||
|
|
||||||
|
if (NugetFramework.IsUnsupported)
|
||||||
|
{
|
||||||
|
Reporter.Output.WriteLine($"Unsupported framework {Framework}.".Red());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ProjectContexts = ProjectContext.CreateContextForEachTarget(ProjectPath);
|
||||||
|
ProjectContexts = GetMatchingProjectContexts(ProjectContexts, NugetFramework, Runtime);
|
||||||
|
|
||||||
|
if (ProjectContexts.Count() == 0)
|
||||||
|
{
|
||||||
|
string errMsg = $"'{ProjectPath}' cannot be published for '{Framework ?? "<no framework provided>"}' '{Runtime ?? "<no runtime provided>"}'";
|
||||||
|
Reporter.Output.WriteLine(errMsg.Red());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void PublishAllProjects()
|
||||||
|
{
|
||||||
|
NumberOfPublishedProjects = 0;
|
||||||
|
NumberOfProjects = 0;
|
||||||
|
foreach (var project in ProjectContexts)
|
||||||
|
{
|
||||||
|
if (PublishProjectContext(project, OutputPath, Configuration))
|
||||||
|
{
|
||||||
|
NumberOfPublishedProjects++;
|
||||||
|
}
|
||||||
|
|
||||||
|
NumberOfProjects++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return the matching framework/runtime ProjectContext.
|
||||||
|
/// If 'nugetframework' or 'runtime' is null or empty then it matches with any.
|
||||||
|
/// </summary>
|
||||||
|
private static IEnumerable<ProjectContext> GetMatchingProjectContexts(IEnumerable<ProjectContext> contexts, NuGetFramework framework, string runtimeIdentifier)
|
||||||
|
{
|
||||||
|
foreach (var context in contexts)
|
||||||
|
{
|
||||||
|
if (context.TargetFramework == null || string.IsNullOrEmpty(context.RuntimeIdentifier))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(runtimeIdentifier) || runtimeIdentifier.Equals(context.RuntimeIdentifier))
|
||||||
|
{
|
||||||
|
if (framework == null || framework.Equals(context.TargetFramework))
|
||||||
|
{
|
||||||
|
yield return context;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Publish the project for given 'framework (ex - dnxcore50)' and 'runtimeID (ex - win7-x64)'
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">project that is to be published</param>
|
||||||
|
/// <param name="outputPath">Location of published files</param>
|
||||||
|
/// <param name="configuration">Debug or Release</param>
|
||||||
|
/// <returns>Return 0 if successful else return non-zero</returns>
|
||||||
|
private static bool PublishProjectContext(ProjectContext context, string outputPath, string configuration)
|
||||||
|
{
|
||||||
|
Reporter.Output.WriteLine($"Publishing {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}/{context.RuntimeIdentifier.Yellow()}");
|
||||||
|
|
||||||
|
var options = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
|
||||||
|
|
||||||
|
// Generate the output path
|
||||||
|
if (string.IsNullOrEmpty(outputPath))
|
||||||
|
{
|
||||||
|
outputPath = Path.Combine(
|
||||||
|
context.ProjectFile.ProjectDirectory,
|
||||||
|
Constants.BinDirectoryName,
|
||||||
|
configuration,
|
||||||
|
context.TargetFramework.GetTwoDigitShortFolderName(),
|
||||||
|
context.RuntimeIdentifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Directory.Exists(outputPath))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(outputPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compile the project (and transitively, all it's dependencies)
|
||||||
|
var result = Command.Create("dotnet-compile",
|
||||||
|
$"--framework \"{context.TargetFramework.DotNetFrameworkName}\" " +
|
||||||
|
$"--output \"{outputPath}\" " +
|
||||||
|
$"--configuration \"{configuration}\" " +
|
||||||
|
"--no-host " +
|
||||||
|
$"\"{context.ProjectFile.ProjectDirectory}\"")
|
||||||
|
.ForwardStdErr()
|
||||||
|
.ForwardStdOut()
|
||||||
|
.Execute();
|
||||||
|
|
||||||
|
if (result.ExitCode != 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use a library exporter to collect publish assets
|
||||||
|
var exporter = context.CreateExporter(configuration);
|
||||||
|
|
||||||
|
foreach (var export in exporter.GetAllExports())
|
||||||
|
{
|
||||||
|
// Skip copying project references
|
||||||
|
if (export.Library is ProjectDescription)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Reporter.Verbose.WriteLine($"Publishing {export.Library.Identity.ToString().Green().Bold()} ...");
|
||||||
|
|
||||||
|
PublishFiles(export.RuntimeAssemblies, outputPath);
|
||||||
|
PublishFiles(export.NativeLibraries, outputPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Publish a host if this is an application
|
||||||
|
if (options.EmitEntryPoint.GetValueOrDefault())
|
||||||
|
{
|
||||||
|
Reporter.Verbose.WriteLine($"Making {context.ProjectFile.Name.Cyan()} runnable ...");
|
||||||
|
PublishHost(context, outputPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
Reporter.Output.WriteLine($"Published to {outputPath}".Green().Bold());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int PublishHost(ProjectContext context, string outputPath)
|
||||||
|
{
|
||||||
|
if (context.TargetFramework.IsDesktop())
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
var hostPath = Path.Combine(AppContext.BaseDirectory, Constants.HostExecutableName);
|
||||||
|
if (!File.Exists(hostPath))
|
||||||
|
{
|
||||||
|
Reporter.Error.WriteLine($"Cannot find {Constants.HostExecutableName} in the dotnet directory.".Red());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var outputExe = Path.Combine(outputPath, context.ProjectFile.Name + Constants.ExeSuffix);
|
||||||
|
|
||||||
|
// Copy the host
|
||||||
|
File.Copy(hostPath, outputExe, overwrite: true);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void PublishFiles(IEnumerable<LibraryAsset> files, string outputPath)
|
||||||
|
{
|
||||||
|
foreach (var file in files)
|
||||||
|
{
|
||||||
|
File.Copy(file.ResolvedPath, Path.Combine(outputPath, Path.GetFileName(file.ResolvedPath)), overwrite: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue