forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResolveRuntimePackAssets.cs
166 lines (136 loc) · 8.29 KB
/
ResolveRuntimePackAssets.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.NET.Build.Tasks
{
public class ResolveRuntimePackAssets : TaskBase
{
public ITaskItem[] ResolvedRuntimePacks { get; set; }
public ITaskItem[] FrameworkReferences { get; set; } = Array.Empty<ITaskItem>();
public ITaskItem[] UnavailableRuntimePacks { get; set; } = Array.Empty<ITaskItem>();
public ITaskItem[] SatelliteResourceLanguages { get; set; } = Array.Empty<ITaskItem>();
[Output]
public ITaskItem[] RuntimePackAssets { get; set; }
protected override void ExecuteCore()
{
var runtimePackAssets = new List<ITaskItem>();
HashSet<string> frameworkReferenceNames = new HashSet<string>(FrameworkReferences.Select(item => item.ItemSpec), StringComparer.OrdinalIgnoreCase);
foreach (var unavailableRuntimePack in UnavailableRuntimePacks)
{
if (frameworkReferenceNames.Contains(unavailableRuntimePack.ItemSpec))
{
// This is a runtime pack that should be used, but wasn't available for the specified RuntimeIdentifier
// NETSDK1082: There was no runtime pack for {0} available for the specified RuntimeIdentifier '{1}'.
Log.LogError(Strings.NoRuntimePackAvailable, unavailableRuntimePack.ItemSpec,
unavailableRuntimePack.GetMetadata(MetadataKeys.RuntimeIdentifier));
}
}
HashSet<string> processedRuntimePackRoots = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var runtimePack in ResolvedRuntimePacks)
{
if (!frameworkReferenceNames.Contains(runtimePack.GetMetadata(MetadataKeys.FrameworkName)))
{
// This is a runtime pack for a shared framework that ultimately wasn't referenced, so don't include its assets
continue;
}
string runtimePackRoot = runtimePack.GetMetadata(MetadataKeys.PackageDirectory);
if (string.IsNullOrEmpty(runtimePackRoot) || !Directory.Exists(runtimePackRoot))
{
// If we do the work in https://github.com/dotnet/cli/issues/10528,
// then we should add a new error message here indicating that the runtime pack hasn't
// been downloaded, and that restore should be run with that runtime identifier.
Log.LogError(Strings.NoRuntimePackAvailable, runtimePack.ItemSpec,
runtimePack.GetMetadata(MetadataKeys.RuntimeIdentifier));
}
if (!processedRuntimePackRoots.Add(runtimePackRoot))
{
// We already added assets from this runtime pack (which can happen with FrameworkReferences to different
// profiles of the same shared framework)
continue;
}
string runtimeIdentifier = runtimePack.GetMetadata(MetadataKeys.RuntimeIdentifier);
// These hard-coded paths are temporary until we have "real" runtime packs, which will likely have a flattened
// folder structure and a manifest indicating how the files should be used: https://github.com/dotnet/cli/issues/10442
string runtimeAssetsPath = Path.Combine(runtimePackRoot, "runtimes", runtimeIdentifier, "lib", "netcoreapp3.0");
string nativeAssetsPath = Path.Combine(runtimePackRoot, "runtimes", runtimeIdentifier, "native");
var runtimeAssets = Directory.Exists(runtimeAssetsPath) ? Directory.GetFiles(runtimeAssetsPath) : Array.Empty<string>();
var nativeAssets = Directory.Exists(nativeAssetsPath) ? Directory.GetFiles(nativeAssetsPath) : Array.Empty<string>();
void AddAsset(string assetPath, string assetType)
{
if (assetPath.EndsWith(".pdb", StringComparison.OrdinalIgnoreCase) ||
assetPath.EndsWith(".map", StringComparison.OrdinalIgnoreCase) ||
assetPath.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) ||
assetPath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase) ||
assetPath.EndsWith(".json", StringComparison.OrdinalIgnoreCase) ||
assetPath.EndsWith("._", StringComparison.Ordinal))
{
// Don't add assets for these files (shouldn't be necessary if/once we have a manifest in the runtime pack
// https://github.com/dotnet/cli/issues/10442
return;
}
var assetItem = new TaskItem(assetPath);
assetItem.SetMetadata(MetadataKeys.CopyLocal, "true");
assetItem.SetMetadata(MetadataKeys.DestinationSubPath, Path.GetFileName(assetPath));
assetItem.SetMetadata(MetadataKeys.AssetType, assetType);
assetItem.SetMetadata(MetadataKeys.PackageName, runtimePack.GetMetadata(MetadataKeys.PackageName));
assetItem.SetMetadata(MetadataKeys.PackageVersion, runtimePack.GetMetadata(MetadataKeys.PackageVersion));
assetItem.SetMetadata(MetadataKeys.RuntimeIdentifier, runtimeIdentifier);
assetItem.SetMetadata(MetadataKeys.IsTrimmable, runtimePack.GetMetadata(MetadataKeys.IsTrimmable));
runtimePackAssets.Add(assetItem);
}
foreach (var asset in runtimeAssets)
{
AddAsset(asset, "runtime");
}
foreach (var asset in nativeAssets)
{
AddAsset(asset, "native");
}
runtimePackAssets.AddRange(EnumerateResourceAssets(runtimePackRoot, runtimeIdentifier, runtimePack));
}
RuntimePackAssets = runtimePackAssets.ToArray();
}
private IEnumerable<ITaskItem> EnumerateResourceAssets(string runtimePackRoot, string runtimeIdentifier, ITaskItem runtimePack)
{
// These hard-coded paths are temporary until we have "real" runtime packs, which will likely have a flattened structure
var directory = Path.Combine(runtimePackRoot, "runtimes", runtimeIdentifier, "lib", "netcoreapp3.0");
if (!Directory.Exists(directory))
{
yield break;
}
foreach (var subdir in Directory.EnumerateDirectories(directory))
{
foreach (var asset in EnumerateCultureAssets(subdir, runtimeIdentifier, runtimePack))
{
yield return asset;
}
}
}
private IEnumerable<ITaskItem> EnumerateCultureAssets(string cultureDirectory, string runtimeIdentifier, ITaskItem runtimePack)
{
var culture = Path.GetFileName(cultureDirectory);
if (this.SatelliteResourceLanguages.Length > 1 &&
!this.SatelliteResourceLanguages.Any(lang => string.Equals(lang.ItemSpec, culture, StringComparison.OrdinalIgnoreCase)))
{
yield break;
}
foreach (var file in Directory.EnumerateFiles(cultureDirectory, "*.resources.dll"))
{
var item = new TaskItem(file);
item.SetMetadata(MetadataKeys.CopyLocal, "true");
item.SetMetadata(MetadataKeys.DestinationSubDirectory, culture + Path.DirectorySeparatorChar);
item.SetMetadata(MetadataKeys.DestinationSubPath, Path.Combine(culture, Path.GetFileName(file)));
item.SetMetadata(MetadataKeys.AssetType, "resources");
item.SetMetadata(MetadataKeys.PackageName, runtimePack.GetMetadata(MetadataKeys.PackageName));
item.SetMetadata(MetadataKeys.PackageVersion, runtimePack.GetMetadata(MetadataKeys.PackageVersion));
item.SetMetadata(MetadataKeys.RuntimeIdentifier, runtimeIdentifier);
item.SetMetadata(MetadataKeys.Culture, culture);
yield return item;
}
}
}
}