2015-12-11 07:00:35 +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;
|
2016-02-03 18:57:25 +00:00
|
|
|
|
using Microsoft.DotNet.Cli.Compiler.Common;
|
2016-04-13 17:19:38 +00:00
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-02-02 18:50:59 +00:00
|
|
|
|
using Microsoft.DotNet.Files;
|
2016-04-13 17:19:38 +00:00
|
|
|
|
using Microsoft.DotNet.ProjectModel;
|
|
|
|
|
using Microsoft.DotNet.ProjectModel.Compilation;
|
2016-03-25 22:40:16 +00:00
|
|
|
|
using Microsoft.DotNet.ProjectModel.Graph;
|
2016-04-13 17:19:38 +00:00
|
|
|
|
using Microsoft.DotNet.ProjectModel.Utilities;
|
|
|
|
|
using Microsoft.DotNet.Tools.Common;
|
|
|
|
|
using Microsoft.Extensions.PlatformAbstractions;
|
|
|
|
|
using NuGet.Frameworks;
|
2015-12-11 07:00:35 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Publish
|
|
|
|
|
{
|
2016-01-31 05:47:50 +00:00
|
|
|
|
public partial class PublishCommand
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
2016-02-16 23:30:39 +00:00
|
|
|
|
private const string PublishSubfolderName = "publish";
|
|
|
|
|
|
2015-12-11 07:00:35 +00:00
|
|
|
|
public string ProjectPath { get; set; }
|
|
|
|
|
public string Configuration { get; set; }
|
2016-02-03 18:57:25 +00:00
|
|
|
|
public string BuildBasePath { get; set; }
|
2015-12-11 07:00:35 +00:00
|
|
|
|
public string OutputPath { get; set; }
|
|
|
|
|
public string Framework { get; set; }
|
|
|
|
|
public string Runtime { get; set; }
|
2015-12-21 20:23:05 +00:00
|
|
|
|
public bool NativeSubdirectories { get; set; }
|
2015-12-11 07:00:35 +00:00
|
|
|
|
public NuGetFramework NugetFramework { get; set; }
|
2016-03-09 19:36:16 +00:00
|
|
|
|
public IList<ProjectContext> ProjectContexts { get; set; }
|
2016-02-18 09:09:23 +00:00
|
|
|
|
public string VersionSuffix { get; set; }
|
2015-12-11 07:00:35 +00:00
|
|
|
|
public int NumberOfProjects { get; private set; }
|
|
|
|
|
public int NumberOfPublishedProjects { get; private set; }
|
2016-03-29 21:15:26 +00:00
|
|
|
|
public bool ShouldBuild { get; set; }
|
2015-12-11 07:00:35 +00:00
|
|
|
|
|
|
|
|
|
public bool TryPrepareForPublish()
|
|
|
|
|
{
|
|
|
|
|
if (Framework != null)
|
|
|
|
|
{
|
|
|
|
|
NugetFramework = NuGetFramework.Parse(Framework);
|
|
|
|
|
|
|
|
|
|
if (NugetFramework.IsUnsupported)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Output.WriteLine($"Unsupported framework {Framework}.".Red());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-09 19:36:16 +00:00
|
|
|
|
ProjectContexts = SelectContexts(ProjectPath, NugetFramework, Runtime).ToList();
|
2015-12-19 00:39:43 +00:00
|
|
|
|
if (!ProjectContexts.Any())
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
2016-03-09 19:36:16 +00:00
|
|
|
|
string errMsg = $"'{ProjectPath}' cannot be published for '{Framework ?? "<no framework provided>"}' '{Runtime ?? "<no runtime provided>"}'";
|
2015-12-11 07:00:35 +00:00
|
|
|
|
Reporter.Output.WriteLine(errMsg.Red());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void PublishAllProjects()
|
|
|
|
|
{
|
|
|
|
|
NumberOfPublishedProjects = 0;
|
|
|
|
|
NumberOfProjects = 0;
|
2016-01-11 21:40:47 +00:00
|
|
|
|
|
2015-12-11 07:00:35 +00:00
|
|
|
|
foreach (var project in ProjectContexts)
|
|
|
|
|
{
|
2016-02-03 18:57:25 +00:00
|
|
|
|
if (PublishProjectContext(project, BuildBasePath, OutputPath, Configuration, NativeSubdirectories))
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
|
|
|
|
NumberOfPublishedProjects++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NumberOfProjects++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-03-01 23:35:32 +00:00
|
|
|
|
/// Publish the project for given 'framework (ex - netstandardapp1.5)' and 'runtimeID (ex - win7-x64)'
|
2015-12-11 07:00:35 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">project that is to be published</param>
|
2016-01-20 23:41:46 +00:00
|
|
|
|
/// <param name="baseOutputPath">Location of published files</param>
|
2015-12-11 07:00:35 +00:00
|
|
|
|
/// <param name="configuration">Debug or Release</param>
|
2016-01-20 23:41:46 +00:00
|
|
|
|
/// <param name="nativeSubdirectories"></param>
|
2015-12-11 07:00:35 +00:00
|
|
|
|
/// <returns>Return 0 if successful else return non-zero</returns>
|
2016-02-18 09:09:23 +00:00
|
|
|
|
private bool PublishProjectContext(ProjectContext context, string buildBasePath, string outputPath, string configuration, bool nativeSubdirectories)
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
2016-03-09 19:36:16 +00:00
|
|
|
|
var target = context.TargetFramework.DotNetFrameworkName;
|
|
|
|
|
if (!string.IsNullOrEmpty(context.RuntimeIdentifier))
|
|
|
|
|
{
|
|
|
|
|
target = $"{target}/{context.RuntimeIdentifier}";
|
|
|
|
|
}
|
|
|
|
|
Reporter.Output.WriteLine($"Publishing {context.RootProject.Identity.Name.Yellow()} for {target.Yellow()}");
|
2015-12-11 07:00:35 +00:00
|
|
|
|
|
|
|
|
|
var options = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
|
2016-02-17 18:08:27 +00:00
|
|
|
|
var outputPaths = context.GetOutputPaths(configuration, buildBasePath, outputPath);
|
2016-02-18 09:09:23 +00:00
|
|
|
|
|
2016-01-26 14:39:13 +00:00
|
|
|
|
if (string.IsNullOrEmpty(outputPath))
|
|
|
|
|
{
|
2016-02-17 18:08:27 +00:00
|
|
|
|
outputPath = Path.Combine(outputPaths.RuntimeOutputPath, PublishSubfolderName);
|
2016-01-26 14:39:13 +00:00
|
|
|
|
}
|
2015-12-11 07:00:35 +00:00
|
|
|
|
|
2016-01-11 21:40:47 +00:00
|
|
|
|
var contextVariables = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "publish:ProjectPath", context.ProjectDirectory },
|
|
|
|
|
{ "publish:Configuration", configuration },
|
2016-01-25 04:28:52 +00:00
|
|
|
|
{ "publish:OutputPath", outputPath },
|
2016-02-09 23:30:04 +00:00
|
|
|
|
{ "publish:TargetFramework", context.TargetFramework.GetShortFolderName() },
|
|
|
|
|
{ "publish:FullTargetFramework", context.TargetFramework.DotNetFrameworkName },
|
2016-01-11 21:40:47 +00:00
|
|
|
|
{ "publish:Runtime", context.RuntimeIdentifier },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
RunScripts(context, ScriptNames.PrePublish, contextVariables);
|
|
|
|
|
|
2016-01-26 14:39:13 +00:00
|
|
|
|
if (!Directory.Exists(outputPath))
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
2016-01-26 14:39:13 +00:00
|
|
|
|
Directory.CreateDirectory(outputPath);
|
2015-12-11 07:00:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compile the project (and transitively, all it's dependencies)
|
2016-03-29 21:15:26 +00:00
|
|
|
|
if (ShouldBuild && !InvokeBuildOnProject(context, buildBasePath, configuration))
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use a library exporter to collect publish assets
|
2016-04-12 19:36:26 +00:00
|
|
|
|
var exporter = context.CreateExporter(configuration, buildBasePath);
|
2015-12-11 07:00:35 +00:00
|
|
|
|
|
2016-03-25 22:40:16 +00:00
|
|
|
|
// Collect all exports and organize them
|
2016-04-02 04:52:08 +00:00
|
|
|
|
var packageExports = exporter.GetAllExports()
|
2016-03-25 22:40:16 +00:00
|
|
|
|
.Where(e => e.Library.Identity.Type.Equals(LibraryType.Package))
|
|
|
|
|
.ToDictionary(e => e.Library.Identity.Name);
|
2016-04-08 15:51:51 +00:00
|
|
|
|
var collectExclusionList = context.IsPortable ? GetExclusionList(context, packageExports) : new HashSet<string>();
|
2016-03-25 22:40:16 +00:00
|
|
|
|
|
2016-04-13 17:19:38 +00:00
|
|
|
|
// Get the output paths used by the call to `dotnet build` above (since we didn't pass `--output`, they will be different from
|
|
|
|
|
// our current output paths)
|
|
|
|
|
var buildOutputPaths = context.GetOutputPaths(configuration, buildBasePath);
|
|
|
|
|
|
2016-04-02 04:52:08 +00:00
|
|
|
|
var exports = exporter.GetAllExports();
|
|
|
|
|
foreach (var export in exports.Where(e => !collectExclusionList.Contains(e.Library.Identity.Name)))
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
2016-04-02 01:49:36 +00:00
|
|
|
|
Reporter.Verbose.WriteLine($"publish: Publishing {export.Library.Identity.ToString().Green().Bold()} ...");
|
2015-12-11 07:00:35 +00:00
|
|
|
|
|
2016-04-08 15:51:51 +00:00
|
|
|
|
PublishAssetGroups(export.RuntimeAssemblyGroups, outputPath, nativeSubdirectories: false, includeRuntimeGroups: context.IsPortable);
|
|
|
|
|
PublishAssetGroups(export.NativeLibraryGroups, outputPath, nativeSubdirectories, includeRuntimeGroups: context.IsPortable);
|
2016-04-13 17:19:38 +00:00
|
|
|
|
|
|
|
|
|
var runtimeAssetsToCopy = export.RuntimeAssets.Where(a => ShouldCopyExportRuntimeAsset(context, buildOutputPaths, export, a));
|
|
|
|
|
runtimeAssetsToCopy.StructuredCopyTo(outputPath, outputPaths.IntermediateOutputDirectoryPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.ProjectFile.HasRuntimeOutput(configuration) && !context.TargetFramework.IsDesktop())
|
|
|
|
|
{
|
|
|
|
|
PublishFiles(
|
|
|
|
|
new[] {
|
|
|
|
|
buildOutputPaths.RuntimeFiles.DepsJson,
|
|
|
|
|
buildOutputPaths.RuntimeFiles.RuntimeConfigJson
|
|
|
|
|
},
|
|
|
|
|
outputPath);
|
2016-04-02 04:52:08 +00:00
|
|
|
|
}
|
2016-01-22 00:40:33 +00:00
|
|
|
|
|
2016-04-02 04:52:08 +00:00
|
|
|
|
if (options.PreserveCompilationContext.GetValueOrDefault())
|
|
|
|
|
{
|
|
|
|
|
foreach (var export in exports)
|
2016-01-22 00:40:33 +00:00
|
|
|
|
{
|
2016-04-02 04:52:08 +00:00
|
|
|
|
PublishRefs(export, outputPath, !collectExclusionList.Contains(export.Library.Identity.Name));
|
2016-01-22 00:40:33 +00:00
|
|
|
|
}
|
2015-12-11 07:00:35 +00:00
|
|
|
|
}
|
2016-04-08 15:51:51 +00:00
|
|
|
|
|
2016-02-02 18:50:59 +00:00
|
|
|
|
var contentFiles = new ContentFiles(context);
|
|
|
|
|
contentFiles.StructuredCopyTo(outputPath);
|
2016-01-09 00:20:39 +00:00
|
|
|
|
|
2015-12-11 07:00:35 +00:00
|
|
|
|
// Publish a host if this is an application
|
2016-03-09 19:36:16 +00:00
|
|
|
|
if (options.EmitEntryPoint.GetValueOrDefault() && !string.IsNullOrEmpty(context.RuntimeIdentifier))
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
2016-04-02 01:49:36 +00:00
|
|
|
|
Reporter.Verbose.WriteLine($"publish: Renaming native host in output to create fully standalone output.");
|
|
|
|
|
RenamePublishedHost(context, outputPath, options);
|
2015-12-11 07:00:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 21:40:47 +00:00
|
|
|
|
RunScripts(context, ScriptNames.PostPublish, contextVariables);
|
|
|
|
|
|
2016-04-02 01:49:36 +00:00
|
|
|
|
Reporter.Output.WriteLine($"publish: Published to {outputPath}".Green().Bold());
|
2016-01-11 21:40:47 +00:00
|
|
|
|
|
2015-12-11 07:00:35 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-13 17:19:38 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Filters which export's RuntimeAssets should get copied to the output path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// True if the asset should be copied to the output path; otherwise, false.
|
|
|
|
|
/// </returns>
|
|
|
|
|
private static bool ShouldCopyExportRuntimeAsset(ProjectContext context, OutputPaths buildOutputPaths, LibraryExport export, LibraryAsset asset)
|
2016-04-02 01:49:36 +00:00
|
|
|
|
{
|
2016-04-13 17:19:38 +00:00
|
|
|
|
// The current project has the host .exe in its runtime assets, but it shouldn't be copied
|
|
|
|
|
// to the output path during publish. The host will come from the export that has the real host in it.
|
2016-04-02 01:49:36 +00:00
|
|
|
|
|
2016-04-13 17:19:38 +00:00
|
|
|
|
if (context.RootProject.Identity == export.Library.Identity)
|
2016-04-02 01:49:36 +00:00
|
|
|
|
{
|
2016-04-13 17:19:38 +00:00
|
|
|
|
if (asset.ResolvedPath == buildOutputPaths.RuntimeFiles.Executable)
|
2016-04-02 01:49:36 +00:00
|
|
|
|
{
|
2016-04-13 17:19:38 +00:00
|
|
|
|
return false;
|
2016-04-02 01:49:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-13 17:19:38 +00:00
|
|
|
|
return true;
|
2016-04-02 01:49:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 18:09:28 +00:00
|
|
|
|
private bool InvokeBuildOnProject(ProjectContext context, string buildBasePath, string configuration)
|
2016-03-29 21:15:26 +00:00
|
|
|
|
{
|
|
|
|
|
var args = new List<string>()
|
|
|
|
|
{
|
|
|
|
|
"--framework",
|
|
|
|
|
$"{context.TargetFramework.DotNetFrameworkName}",
|
|
|
|
|
"--configuration",
|
|
|
|
|
configuration,
|
|
|
|
|
context.ProjectFile.ProjectDirectory
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(context.RuntimeIdentifier))
|
|
|
|
|
{
|
|
|
|
|
args.Insert(0, context.RuntimeIdentifier);
|
|
|
|
|
args.Insert(0, "--runtime");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(VersionSuffix))
|
|
|
|
|
{
|
|
|
|
|
args.Add("--version-suffix");
|
|
|
|
|
args.Add(VersionSuffix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(buildBasePath))
|
|
|
|
|
{
|
|
|
|
|
args.Add("--build-base-path");
|
|
|
|
|
args.Add(buildBasePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = Build.BuildCommand.Run(args.ToArray());
|
|
|
|
|
|
|
|
|
|
return result == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 22:40:16 +00:00
|
|
|
|
private HashSet<string> GetExclusionList(ProjectContext context, Dictionary<string, LibraryExport> exports)
|
|
|
|
|
{
|
|
|
|
|
var exclusionList = new HashSet<string>();
|
|
|
|
|
var redistPackages = context.RootProject.Dependencies
|
|
|
|
|
.Where(r => r.Type.Equals(LibraryDependencyType.Platform))
|
|
|
|
|
.ToList();
|
|
|
|
|
if (redistPackages.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return exclusionList;
|
|
|
|
|
}
|
|
|
|
|
else if (redistPackages.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Multiple packages with type: \"platform\" were specified!");
|
|
|
|
|
}
|
|
|
|
|
var redistExport = exports[redistPackages[0].Name];
|
|
|
|
|
|
|
|
|
|
exclusionList.Add(redistExport.Library.Identity.Name);
|
|
|
|
|
CollectDependencies(exports, redistExport.Library.Dependencies, exclusionList);
|
|
|
|
|
return exclusionList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CollectDependencies(Dictionary<string, LibraryExport> exports, IEnumerable<LibraryRange> dependencies, HashSet<string> exclusionList)
|
|
|
|
|
{
|
|
|
|
|
foreach (var dependency in dependencies)
|
|
|
|
|
{
|
|
|
|
|
var export = exports[dependency.Name];
|
|
|
|
|
if(export.Library.Identity.Version.Equals(dependency.VersionRange.MinVersion))
|
|
|
|
|
{
|
|
|
|
|
exclusionList.Add(export.Library.Identity.Name);
|
|
|
|
|
CollectDependencies(exports, export.Library.Dependencies, exclusionList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 04:52:08 +00:00
|
|
|
|
private static void PublishRefs(LibraryExport export, string outputPath, bool deduplicate)
|
2016-01-22 00:40:33 +00:00
|
|
|
|
{
|
|
|
|
|
var refsPath = Path.Combine(outputPath, "refs");
|
|
|
|
|
if (!Directory.Exists(refsPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(refsPath);
|
|
|
|
|
}
|
2016-04-02 04:52:08 +00:00
|
|
|
|
|
2016-01-22 00:40:33 +00:00
|
|
|
|
// Do not copy compilation assembly if it's in runtime assemblies
|
2016-03-17 18:56:57 +00:00
|
|
|
|
var runtimeAssemblies = new HashSet<LibraryAsset>(export.RuntimeAssemblyGroups.GetDefaultAssets());
|
2016-01-22 00:40:33 +00:00
|
|
|
|
foreach (var compilationAssembly in export.CompilationAssemblies)
|
|
|
|
|
{
|
2016-04-02 04:52:08 +00:00
|
|
|
|
if (!deduplicate || !runtimeAssemblies.Contains(compilationAssembly))
|
2016-01-22 00:40:33 +00:00
|
|
|
|
{
|
|
|
|
|
var destFileName = Path.Combine(refsPath, Path.GetFileName(compilationAssembly.ResolvedPath));
|
|
|
|
|
File.Copy(compilationAssembly.ResolvedPath, destFileName, overwrite: true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 01:49:36 +00:00
|
|
|
|
private static int RenamePublishedHost(ProjectContext context, string outputPath, CommonCompilerOptions compilationOptions)
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
|
|
|
|
if (context.TargetFramework.IsDesktop())
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 01:49:36 +00:00
|
|
|
|
var publishedHostFile = ResolvePublishedHostFile(outputPath);
|
|
|
|
|
if (publishedHostFile == null)
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
2016-04-02 01:49:36 +00:00
|
|
|
|
Reporter.Output.WriteLine($"publish: warning: host executable not available in dependencies, using host for current platform");
|
|
|
|
|
// TODO should this be an error?
|
2015-12-11 07:00:35 +00:00
|
|
|
|
|
2016-04-02 01:49:36 +00:00
|
|
|
|
CoreHost.CopyTo(outputPath, compilationOptions.OutputName + Constants.ExeSuffix);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2015-12-11 07:00:35 +00:00
|
|
|
|
|
2016-04-02 01:49:36 +00:00
|
|
|
|
var publishedHostExtension = Path.GetExtension(publishedHostFile);
|
|
|
|
|
var renamedHostName = compilationOptions.OutputName + publishedHostExtension;
|
|
|
|
|
var renamedHostFile = Path.Combine(outputPath, renamedHostName);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Reporter.Verbose.WriteLine($"publish: renaming published host {publishedHostFile} to {renamedHostFile}");
|
|
|
|
|
File.Copy(publishedHostFile, renamedHostFile, true);
|
|
|
|
|
File.Delete(publishedHostFile);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Error.WriteLine($"publish: Failed to rename {publishedHostFile} to {renamedHostFile}: {e.Message}");
|
|
|
|
|
return 1;
|
2016-01-10 07:33:22 +00:00
|
|
|
|
}
|
2015-12-11 07:00:35 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-02-03 18:57:25 +00:00
|
|
|
|
|
2016-04-02 01:49:36 +00:00
|
|
|
|
private static string ResolvePublishedHostFile(string outputPath)
|
|
|
|
|
{
|
|
|
|
|
var tryExtensions = new string[] { "", ".exe" };
|
|
|
|
|
|
|
|
|
|
foreach (var extension in tryExtensions)
|
|
|
|
|
{
|
|
|
|
|
var hostFile = Path.Combine(outputPath, Constants.PublishedHostExecutableName + extension);
|
|
|
|
|
if (File.Exists(hostFile))
|
|
|
|
|
{
|
|
|
|
|
Reporter.Verbose.WriteLine($"resolved published host: {hostFile}");
|
|
|
|
|
return hostFile;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Reporter.Verbose.WriteLine($"failed to resolve published host in: {outputPath}");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 04:35:43 +00:00
|
|
|
|
private static void PublishFiles(IEnumerable<string> files, string outputPath)
|
|
|
|
|
{
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
var targetPath = Path.Combine(outputPath, Path.GetFileName(file));
|
|
|
|
|
File.Copy(file, targetPath, overwrite: true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-11 07:00:35 +00:00
|
|
|
|
|
2016-03-17 18:56:57 +00:00
|
|
|
|
private void PublishAssetGroups(IEnumerable<LibraryAssetGroup> groups, string outputPath, bool nativeSubdirectories, bool includeRuntimeGroups)
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
2016-03-17 18:56:57 +00:00
|
|
|
|
foreach (var group in groups.Where(g => includeRuntimeGroups || string.IsNullOrEmpty(g.Runtime)))
|
2015-12-11 07:00:35 +00:00
|
|
|
|
{
|
2016-03-17 18:56:57 +00:00
|
|
|
|
foreach (var file in group.Assets)
|
2016-03-09 19:36:16 +00:00
|
|
|
|
{
|
2016-03-17 18:56:57 +00:00
|
|
|
|
var destinationDirectory = DetermineFileDestinationDirectory(file, outputPath, nativeSubdirectories);
|
2016-03-09 19:36:16 +00:00
|
|
|
|
|
2016-03-17 18:56:57 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(group.Runtime))
|
|
|
|
|
{
|
|
|
|
|
destinationDirectory = Path.Combine(destinationDirectory, Path.GetDirectoryName(file.RelativePath));
|
|
|
|
|
}
|
2016-02-18 09:09:23 +00:00
|
|
|
|
|
2016-03-17 18:56:57 +00:00
|
|
|
|
if (!Directory.Exists(destinationDirectory))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(destinationDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 01:49:36 +00:00
|
|
|
|
Reporter.Verbose.WriteLine($"Publishing file {Path.GetFileName(file.RelativePath)} to {destinationDirectory}");
|
2016-03-17 18:56:57 +00:00
|
|
|
|
File.Copy(file.ResolvedPath, Path.Combine(destinationDirectory, file.FileName), overwrite: true);
|
|
|
|
|
}
|
2015-12-21 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string DetermineFileDestinationDirectory(LibraryAsset file, string outputPath, bool nativeSubdirectories)
|
|
|
|
|
{
|
|
|
|
|
var destinationDirectory = outputPath;
|
|
|
|
|
|
|
|
|
|
if (nativeSubdirectories)
|
|
|
|
|
{
|
|
|
|
|
destinationDirectory = Path.Combine(outputPath, GetNativeRelativeSubdirectory(file.RelativePath));
|
2015-12-11 07:00:35 +00:00
|
|
|
|
}
|
2015-12-21 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
return destinationDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetNativeRelativeSubdirectory(string filepath)
|
|
|
|
|
{
|
|
|
|
|
string directoryPath = Path.GetDirectoryName(filepath);
|
|
|
|
|
|
|
|
|
|
string[] parts = directoryPath.Split(new string[] { "native" }, 2, StringSplitOptions.None);
|
|
|
|
|
|
|
|
|
|
if (parts.Length != 2)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Unrecognized Native Directory Format: " + filepath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string candidate = parts[1];
|
|
|
|
|
candidate = candidate.TrimStart(new char[] { '/', '\\' });
|
|
|
|
|
|
|
|
|
|
return candidate;
|
2015-12-11 07:00:35 +00:00
|
|
|
|
}
|
2015-12-19 00:39:43 +00:00
|
|
|
|
|
2016-03-09 19:36:16 +00:00
|
|
|
|
private IEnumerable<ProjectContext> SelectContexts(string projectPath, NuGetFramework framework, string runtime)
|
2015-12-19 00:39:43 +00:00
|
|
|
|
{
|
2016-04-02 04:52:08 +00:00
|
|
|
|
var allContexts = framework == null ?
|
|
|
|
|
ProjectContext.CreateContextForEachFramework(projectPath) :
|
|
|
|
|
new[] { ProjectContext.Create(projectPath, framework) };
|
|
|
|
|
|
|
|
|
|
var runtimes = !string.IsNullOrEmpty(runtime) ?
|
|
|
|
|
new [] {runtime} :
|
|
|
|
|
PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers();
|
|
|
|
|
return allContexts.Select(c => c.CreateRuntimeContext(runtimes));
|
2015-12-19 00:39:43 +00:00
|
|
|
|
}
|
2016-04-02 04:52:08 +00:00
|
|
|
|
|
2016-01-09 00:20:39 +00:00
|
|
|
|
private static void CopyContents(ProjectContext context, string outputPath)
|
|
|
|
|
{
|
|
|
|
|
var contentFiles = context.ProjectFile.Files.GetContentFiles();
|
|
|
|
|
Copy(contentFiles, context.ProjectDirectory, outputPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Copy(IEnumerable<string> contentFiles, string sourceDirectory, string targetDirectory)
|
|
|
|
|
{
|
|
|
|
|
if (contentFiles == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(contentFiles));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sourceDirectory = PathUtility.EnsureTrailingSlash(sourceDirectory);
|
|
|
|
|
targetDirectory = PathUtility.EnsureTrailingSlash(targetDirectory);
|
|
|
|
|
|
|
|
|
|
foreach (var contentFilePath in contentFiles)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Verbose.WriteLine($"Publishing {contentFilePath.Green().Bold()} ...");
|
|
|
|
|
|
|
|
|
|
var fileName = Path.GetFileName(contentFilePath);
|
|
|
|
|
|
|
|
|
|
var targetFilePath = contentFilePath.Replace(sourceDirectory, targetDirectory);
|
|
|
|
|
var targetFileParentFolder = Path.GetDirectoryName(targetFilePath);
|
|
|
|
|
|
|
|
|
|
// Create directory before copying a file
|
|
|
|
|
if (!Directory.Exists(targetFileParentFolder))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(targetFileParentFolder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.Copy(
|
|
|
|
|
contentFilePath,
|
|
|
|
|
targetFilePath,
|
|
|
|
|
overwrite: true);
|
|
|
|
|
|
|
|
|
|
// clear read-only bit if set
|
|
|
|
|
var fileAttributes = File.GetAttributes(targetFilePath);
|
|
|
|
|
if ((fileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
|
|
|
|
|
{
|
|
|
|
|
File.SetAttributes(targetFilePath, fileAttributes & ~FileAttributes.ReadOnly);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-11 21:40:47 +00:00
|
|
|
|
|
|
|
|
|
private static void RunScripts(ProjectContext context, string name, Dictionary<string, string> contextVariables)
|
|
|
|
|
{
|
|
|
|
|
foreach (var script in context.ProjectFile.Scripts.GetOrEmpty(name))
|
|
|
|
|
{
|
|
|
|
|
ScriptExecutor.CreateCommandForScript(context.ProjectFile, script, contextVariables)
|
|
|
|
|
.ForwardStdErr()
|
|
|
|
|
.ForwardStdOut()
|
|
|
|
|
.Execute();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-11 07:00:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|