port DNX's ApplicationHostContext as ProjectContext
This commit is contained in:
parent
f230c84b9f
commit
9da2475e2f
85 changed files with 6676 additions and 747 deletions
|
@ -1,7 +1,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.Dnx.Runtime.Common.CommandLine;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.Extensions.ProjectModel;
|
||||
using NuGet.Frameworks;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Publish
|
||||
{
|
||||
|
@ -9,23 +13,24 @@ namespace Microsoft.DotNet.Tools.Publish
|
|||
{
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
DebugHelper.HandleDebugSwitch(ref args);
|
||||
|
||||
var app = new CommandLineApplication();
|
||||
app.Name = "dotnet publish";
|
||||
app.FullName = ".NET Publisher";
|
||||
app.Description = "Publisher for the .NET Platform";
|
||||
app.HelpOption("-h|--help");
|
||||
|
||||
var verbose = app.Option("-v|--verbose", "Be more verbose", CommandOptionType.NoValue);
|
||||
var framework = app.Option("-f|--framework <FRAMEWORK>", "Target framework to compile 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 project = app.Argument("<PROJECT>", "The project to compile, defaults to the current directory. Can be a path to a project.json or a project directory");
|
||||
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");
|
||||
|
||||
app.OnExecute(() =>
|
||||
{
|
||||
// Validate arguments
|
||||
CheckArg(framework, "--framework");
|
||||
CheckArg(runtime, "--runtime");
|
||||
CheckArg(framework);
|
||||
CheckArg(runtime);
|
||||
|
||||
// Locate the project and get the name and full path
|
||||
var path = project.Value;
|
||||
|
@ -33,18 +38,10 @@ namespace Microsoft.DotNet.Tools.Publish
|
|||
{
|
||||
path = Directory.GetCurrentDirectory();
|
||||
}
|
||||
if (!string.Equals(Path.GetFileName(path), "project.json", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
path = Path.Combine(path, "project.json");
|
||||
}
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
Console.Error.WriteLine($"Could not find project: {path}");
|
||||
return 1;
|
||||
}
|
||||
var dir = new FileInfo(path).Directory;
|
||||
|
||||
return Publish(path, framework.Value(), runtime.Value(), dir, output.Value());
|
||||
// Load project context and publish it
|
||||
var context = ProjectContext.Create(path, NuGetFramework.Parse(framework.Value()), new[] { runtime.Value() });
|
||||
return Publish(context, output.Value(), configuration.Value() ?? Constants.DefaultConfiguration);
|
||||
});
|
||||
|
||||
try
|
||||
|
@ -58,32 +55,36 @@ namespace Microsoft.DotNet.Tools.Publish
|
|||
}
|
||||
}
|
||||
|
||||
private static void CheckArg(CommandOption argument, string name)
|
||||
private static void CheckArg(CommandOption argument)
|
||||
{
|
||||
if (!argument.HasValue())
|
||||
{
|
||||
// TODO: GROOOOOOSS
|
||||
throw new OperationCanceledException($"Missing required argument: {name}");
|
||||
throw new OperationCanceledException($"Missing required argument: {argument.LongName}");
|
||||
}
|
||||
}
|
||||
|
||||
private static int Publish(string path, string framework, string runtime, DirectoryInfo projectDir, string outputPath)
|
||||
private static int Publish(ProjectContext context, string outputPath, string configuration)
|
||||
{
|
||||
// Make output directory
|
||||
// TODO(anurse): per-framework and per-configuration output dir
|
||||
// TODO(anurse): configurable base output dir? (maybe dotnet compile doesn't support that?)
|
||||
Reporter.Output.WriteLine($"Publishing {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}/{context.RuntimeIdentifier}");
|
||||
|
||||
// Hackily generate the output path
|
||||
if (string.IsNullOrEmpty(outputPath))
|
||||
{
|
||||
outputPath = Path.Combine(projectDir.FullName, "bin", "publish");
|
||||
outputPath = Path.Combine(
|
||||
context.Project.ProjectDirectory,
|
||||
"bin",
|
||||
configuration,
|
||||
context.TargetFramework.GetTwoDigitShortFolderName(),
|
||||
"publish");
|
||||
}
|
||||
|
||||
if (!Directory.Exists(outputPath))
|
||||
{
|
||||
Directory.CreateDirectory(outputPath);
|
||||
}
|
||||
|
||||
// Compile the project
|
||||
var result = Command.Create("dotnet-compile", $"--framework {framework} --output {outputPath} {path}")
|
||||
// Compile the project (and transitively, all it's dependencies)
|
||||
var result = Command.Create("dotnet-compile", $"--framework {context.TargetFramework.DotNetFrameworkName} {context.Project.ProjectDirectory}")
|
||||
.ForwardStdErr()
|
||||
.ForwardStdOut()
|
||||
.RunAsync()
|
||||
|
@ -94,26 +95,18 @@ namespace Microsoft.DotNet.Tools.Publish
|
|||
return result.ExitCode;
|
||||
}
|
||||
|
||||
// Collect the things needed to publish
|
||||
result = Command.Create("dotnet-resolve-references", $"--framework {framework} --runtime {runtime} --assets runtime --assets native {path}")
|
||||
.CaptureStdOut()
|
||||
.ForwardStdErr(Console.Error)
|
||||
.RunAsync()
|
||||
.Result;
|
||||
if (result.ExitCode != 0)
|
||||
// Use a library exporter to collect publish assets
|
||||
var exporter = context.CreateExporter(configuration);
|
||||
foreach (var export in exporter.GetAllExports())
|
||||
{
|
||||
Console.Error.WriteLine("Failed to resolve references");
|
||||
return result.ExitCode;
|
||||
}
|
||||
var references = result.StdOut.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
|
||||
Reporter.Output.WriteLine($"Publishing {export.Library.Identity.ToString().Green().Bold()} ...");
|
||||
|
||||
// Copy everything to the output
|
||||
foreach (var reference in references)
|
||||
{
|
||||
Console.Error.WriteLine($"Publishing {reference} ...");
|
||||
File.Copy(reference, Path.Combine(outputPath, Path.GetFileName(reference)), overwrite: true);
|
||||
PublishFiles(export.RuntimeAssemblies, outputPath);
|
||||
PublishFiles(export.NativeLibraries, outputPath);
|
||||
}
|
||||
|
||||
// Publishing for windows, TODO(anurse): Publish for Mac/Linux/etc.
|
||||
|
||||
// CoreConsole should be there...
|
||||
var coreConsole = Path.Combine(outputPath, Constants.CoreConsoleName);
|
||||
if (!File.Exists(coreConsole))
|
||||
|
@ -121,14 +114,42 @@ namespace Microsoft.DotNet.Tools.Publish
|
|||
Console.Error.WriteLine($"Unable to find {Constants.CoreConsoleName} at {coreConsole}. You must have an explicit dependency on Microsoft.NETCore.ConsoleHost (for now ;))");
|
||||
return 1;
|
||||
}
|
||||
var outputExe = Path.Combine(outputPath, projectDir.Name + Constants.ExeSuffix);
|
||||
|
||||
// Use the 'command' field to generate the name
|
||||
var outputExe = Path.Combine(outputPath, context.Project.Name + ".exe");
|
||||
if (File.Exists(outputExe))
|
||||
{
|
||||
File.Delete(outputExe);
|
||||
}
|
||||
File.Move(coreConsole, outputExe);
|
||||
|
||||
// Check if the a command name is specified, and rename the necessary files
|
||||
if(context.Project.Commands.Count == 1)
|
||||
{
|
||||
var commandName = context.Project.Commands.Single().Key;
|
||||
|
||||
// Move coreconsole and the matching dll
|
||||
var renamedExe = Path.Combine(outputPath, commandName + ".exe");
|
||||
var renamedDll = Path.ChangeExtension(renamedExe, ".dll");
|
||||
if(File.Exists(renamedExe))
|
||||
{
|
||||
File.Delete(renamedExe);
|
||||
}
|
||||
File.Move(outputExe, renamedExe);
|
||||
File.Move(Path.ChangeExtension(outputExe, ".dll"), renamedDll);
|
||||
outputExe = Path.Combine(outputPath, commandName + ".exe");
|
||||
}
|
||||
|
||||
Console.Error.WriteLine($"Published to {outputExe}");
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static void PublishFiles(IEnumerable<string> files, string outputPath)
|
||||
{
|
||||
foreach (var file in files)
|
||||
{
|
||||
File.Copy(file, Path.Combine(outputPath, Path.GetFileName(file)), overwrite: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue