forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateBundle.cs
81 lines (69 loc) · 3.51 KB
/
GenerateBundle.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
// 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.Build.Framework;
using Microsoft.NET.HostModel.Bundle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace Microsoft.NET.Build.Tasks
{
public class GenerateBundle : TaskBase
{
[Required]
public ITaskItem[] FilesToBundle { get; set; }
[Required]
public string AppHostName { get; set; }
[Required]
public bool IncludeSymbols { get; set; }
[Required]
public bool IncludeNativeLibraries { get; set; }
[Required]
public bool IncludeAllContent { get; set; }
[Required]
public string TargetFrameworkVersion { get; set; }
[Required]
public string RuntimeIdentifier { get; set; }
[Required]
public string OutputDir { get; set; }
[Required]
public bool ShowDiagnosticOutput { get; set; }
[Output]
public ITaskItem[] ExcludedFiles { get; set; }
protected override void ExecuteCore()
{
OSPlatform targetOS = RuntimeIdentifier.StartsWith("win") ? OSPlatform.Windows :
RuntimeIdentifier.StartsWith("osx") ? OSPlatform.OSX : OSPlatform.Linux;
Architecture targetArch = RuntimeIdentifier.EndsWith("-x64") || RuntimeIdentifier.Contains("-x64-") ? Architecture.X64 :
RuntimeIdentifier.EndsWith("-x86") || RuntimeIdentifier.Contains("-x86-") ? Architecture.X86 :
RuntimeIdentifier.EndsWith("-arm64") || RuntimeIdentifier.Contains("-arm64-") ? Architecture.Arm64 :
RuntimeIdentifier.EndsWith("-arm") || RuntimeIdentifier.Contains("-arm-") ? Architecture.Arm :
throw new ArgumentException(nameof (RuntimeIdentifier));
BundleOptions options = BundleOptions.None;
options |= IncludeNativeLibraries ? BundleOptions.BundleNativeBinaries : BundleOptions.None;
options |= IncludeAllContent ? BundleOptions.BundleAllContent : BundleOptions.None;
options |= IncludeSymbols ? BundleOptions.BundleSymbolFiles : BundleOptions.None;
var bundler = new Bundler(
AppHostName,
OutputDir,
options,
targetOS,
targetArch,
new Version(TargetFrameworkVersion),
ShowDiagnosticOutput);
var fileSpec = new List<FileSpec>(FilesToBundle.Length);
foreach (var item in FilesToBundle)
{
fileSpec.Add(new FileSpec(sourcePath: item.ItemSpec,
bundleRelativePath:item.GetMetadata(MetadataKeys.RelativePath)));
}
bundler.GenerateBundle(fileSpec);
// Certain files are excluded from the bundle, based on BundleOptions.
// For example:
// Native files and contents files are excluded by default.
// hostfxr and hostpolicy are excluded until singlefilehost is available.
// Return the set of excluded files in ExcludedFiles, so that they can be placed in the publish directory.
ExcludedFiles = FilesToBundle.Zip(fileSpec, (item, spec) => (spec.Excluded) ? item : null).Where(x => x != null).ToArray();
}
}
}