This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
/
PublishCommand.cs
445 lines (371 loc) · 18.2 KB
/
PublishCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// 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;
using Microsoft.DotNet.Cli.Compiler.Common;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Files;
using Microsoft.DotNet.InternalAbstractions;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Compilation;
using Microsoft.DotNet.ProjectModel.Files;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.DotNet.ProjectModel.Utilities;
using Microsoft.DotNet.Tools.Common;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Tools.Publish
{
public partial class PublishCommand
{
private const string PublishSubfolderName = "publish";
public string ProjectPath { get; set; }
public string Configuration { get; set; }
public string BuildBasePath { get; set; }
public string OutputPath { get; set; }
public string Framework { get; set; }
public string Runtime { get; set; }
public bool NativeSubdirectories { get; set; }
public NuGetFramework NugetFramework { get; set; }
public BuildWorkspace Workspace { get; set; }
public IList<ProjectContext> ProjectContexts { get; set; }
public string VersionSuffix { get; set; }
public int NumberOfProjects { get; private set; }
public int NumberOfPublishedProjects { get; private set; }
public bool ShouldBuild { get; set; }
public bool TryPrepareForPublish()
{
if (Framework != null)
{
NugetFramework = NuGetFramework.Parse(Framework);
if (NugetFramework.IsUnsupported)
{
Reporter.Output.WriteLine($"Unsupported framework {Framework}.".Red());
return false;
}
}
ProjectContexts = SelectContexts(ProjectPath, NugetFramework, Runtime).ToList();
if (!ProjectContexts.Any())
{
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, BuildBasePath, OutputPath, Configuration, NativeSubdirectories))
{
NumberOfPublishedProjects++;
}
NumberOfProjects++;
}
}
/// <summary>
/// Publish the project for given 'framework (ex - netcoreapp1.0)' and 'runtimeID (ex - win7-x64)'
/// </summary>
/// <param name="context">project that is to be published</param>
/// <param name="baseOutputPath">Location of published files</param>
/// <param name="configuration">Debug or Release</param>
/// <param name="nativeSubdirectories"></param>
/// <returns>Return 0 if successful else return non-zero</returns>
private bool PublishProjectContext(ProjectContext context, string buildBasePath, string outputPath, string configuration, bool nativeSubdirectories)
{
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()}");
var options = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
var outputPaths = context.GetOutputPaths(configuration, buildBasePath, outputPath);
if (string.IsNullOrEmpty(outputPath))
{
outputPath = Path.Combine(outputPaths.RuntimeOutputPath, PublishSubfolderName);
}
var contextVariables = new Dictionary<string, string>
{
{ "publish:ProjectPath", context.ProjectDirectory },
{ "publish:Configuration", configuration },
{ "publish:OutputPath", outputPath },
{ "publish:TargetFramework", context.TargetFramework.GetShortFolderName() },
{ "publish:FullTargetFramework", context.TargetFramework.DotNetFrameworkName },
{ "publish:Runtime", context.RuntimeIdentifier },
};
RunScripts(context, ScriptNames.PrePublish, contextVariables);
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
// Compile the project (and transitively, all it's dependencies)
if (ShouldBuild && !InvokeBuildOnProject(context, buildBasePath, configuration))
{
return false;
}
// Use a library exporter to collect publish assets
var exporter = context.CreateExporter(configuration, buildBasePath);
// 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);
var exports = exporter.GetAllExports();
var exportsLookup = exports.ToDictionary(e => e.Library.Identity.Name);
var platformExclusionList = context.GetPlatformExclusionList(exportsLookup);
var buildExclusionList = context.GetTypeBuildExclusionList(exportsLookup);
var allExclusionList = new HashSet<string>(platformExclusionList);
allExclusionList.UnionWith(buildExclusionList);
var filteredExports = exports.FilterExports(allExclusionList);
foreach (var export in filteredExports)
{
Reporter.Verbose.WriteLine($"publish: Publishing {export.Library.Identity.ToString().Green().Bold()} ...");
PublishAssetGroups(export.RuntimeAssemblyGroups, outputPath, nativeSubdirectories: false, includeRuntimeGroups: context.IsPortable);
PublishAssetGroups(export.NativeLibraryGroups, outputPath, nativeSubdirectories, includeRuntimeGroups: context.IsPortable);
var runtimeAssetsToCopy = export.RuntimeAssets.Where(a => ShouldCopyExportRuntimeAsset(context, buildOutputPaths, export, a));
runtimeAssetsToCopy.StructuredCopyTo(outputPath, outputPaths.IntermediateOutputDirectoryPath);
foreach (var resourceAsset in export.ResourceAssemblies)
{
var dir = Path.Combine(outputPath, resourceAsset.Locale);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.Copy(resourceAsset.Asset.ResolvedPath, Path.Combine(dir, resourceAsset.Asset.FileName), overwrite: true);
}
}
foreach (var export in exports)
{
if (options.PreserveCompilationContext.GetValueOrDefault())
{
PublishRefs(export, outputPath);
}
}
if (context.ProjectFile.HasRuntimeOutput(configuration) && !context.TargetFramework.IsDesktop())
{
// Make executable in the new location
var executable = new Executable(context, buildOutputPaths, outputPath, buildOutputPaths.IntermediateOutputDirectoryPath, exporter, configuration);
var runtimeExports = filteredExports;
var compilationExports = exports.FilterExports(buildExclusionList);
executable.WriteConfigurationFiles(exports, runtimeExports, compilationExports, includeDevConfig: false);
}
var contentFiles = new ContentFiles(context);
if (context.ProjectFile.PublishOptions != null)
{
var includeEntries = IncludeFilesResolver.GetIncludeFiles(
context.ProjectFile.PublishOptions,
PathUtility.EnsureTrailingSlash(outputPath),
diagnostics: null);
contentFiles.StructuredCopyTo(outputPath, includeEntries);
}
else
{
contentFiles.StructuredCopyTo(outputPath);
}
// Publish a host if this is an application
if (options.EmitEntryPoint.GetValueOrDefault() && !string.IsNullOrEmpty(context.RuntimeIdentifier))
{
Reporter.Verbose.WriteLine($"publish: Renaming native host in output to create fully standalone output.");
RenamePublishedHost(context, outputPath, options);
}
RunScripts(context, ScriptNames.PostPublish, contextVariables);
Reporter.Output.WriteLine($"publish: Published to {outputPath}".Green().Bold());
return true;
}
/// <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)
{
// 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.
if (context.RootProject.Identity == export.Library.Identity)
{
if (asset.ResolvedPath == buildOutputPaths.RuntimeFiles.Executable)
{
return false;
}
}
return true;
}
private bool InvokeBuildOnProject(ProjectContext context, string buildBasePath, string configuration)
{
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(), Workspace);
return result == 0;
}
private static void PublishRefs(LibraryExport export, string outputPath)
{
var refsPath = Path.Combine(outputPath, "refs");
if (!Directory.Exists(refsPath))
{
Directory.CreateDirectory(refsPath);
}
// Do not copy compilation assembly if it's in runtime assemblies
var runtimeAssemblies = new HashSet<LibraryAsset>(export.RuntimeAssemblyGroups.GetDefaultAssets());
foreach (var compilationAssembly in export.CompilationAssemblies)
{
if (runtimeAssemblies.Contains(compilationAssembly))
{
continue;
}
var destFileName = Path.Combine(refsPath, Path.GetFileName(compilationAssembly.ResolvedPath));
File.Copy(compilationAssembly.ResolvedPath, destFileName, overwrite: true);
}
}
private static int RenamePublishedHost(ProjectContext context, string outputPath, CommonCompilerOptions compilationOptions)
{
if (context.TargetFramework.IsDesktop())
{
return 0;
}
var publishedHostFile = ResolvePublishedHostFile(outputPath);
if (publishedHostFile == null)
{
Reporter.Output.WriteLine($"publish: warning: host executable not available in dependencies, using host for current platform");
// TODO should this be an error?
CoreHost.CopyTo(outputPath, compilationOptions.OutputName + Constants.ExeSuffix);
return 0;
}
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;
}
return 0;
}
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;
}
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);
}
}
private void PublishAssetGroups(IEnumerable<LibraryAssetGroup> groups, string outputPath, bool nativeSubdirectories, bool includeRuntimeGroups)
{
foreach (var group in groups.Where(g => includeRuntimeGroups || string.IsNullOrEmpty(g.Runtime)))
{
foreach (var file in group.Assets)
{
var destinationDirectory = DetermineFileDestinationDirectory(file, outputPath, nativeSubdirectories);
if (!string.IsNullOrEmpty(group.Runtime))
{
destinationDirectory = Path.Combine(destinationDirectory, Path.GetDirectoryName(file.RelativePath));
}
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
Reporter.Verbose.WriteLine($"Publishing file {Path.GetFileName(file.RelativePath)} to {destinationDirectory}");
File.Copy(file.ResolvedPath, Path.Combine(destinationDirectory, file.FileName), overwrite: true);
}
}
}
private static string DetermineFileDestinationDirectory(LibraryAsset file, string outputPath, bool nativeSubdirectories)
{
var destinationDirectory = outputPath;
if (nativeSubdirectories)
{
destinationDirectory = Path.Combine(outputPath, GetNativeRelativeSubdirectory(file.RelativePath));
}
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;
}
private IEnumerable<ProjectContext> SelectContexts(string projectPath, NuGetFramework framework, string runtime)
{
if (projectPath.EndsWith("project.json"))
{
if (File.Exists(projectPath) == false)
throw new InvalidProjectException($"'{projectPath}' does not exist");
}
else if (File.Exists(Path.Combine(projectPath, "project.json")) == false)
{
throw new InvalidProjectException($"'{projectPath}' does not contain a project.json file");
}
var contexts = Workspace.GetProjectContextCollection(projectPath)
.EnsureValid(projectPath)
.FrameworkOnlyContexts;
contexts = framework == null ?
contexts :
contexts.Where(c => Equals(c.TargetFramework, framework));
var rids = string.IsNullOrEmpty(runtime) ?
RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers() :
new[] { runtime };
return contexts.Select(c => Workspace.GetRuntimeContext(c, rids));
}
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();
}
}
}
}