This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
DependencyContextWriter.cs
342 lines (303 loc) · 15.8 KB
/
DependencyContextWriter.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
// 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.Diagnostics;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.Extensions.DependencyModel
{
public class DependencyContextWriter
{
public void Write(DependencyContext context, Stream stream)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
using (var writer = new StreamWriter(stream))
{
using (var jsonWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented })
{
Write(context).WriteTo(jsonWriter);
}
}
}
private JObject Write(DependencyContext context)
{
var contextObject = new JObject(
new JProperty(DependencyContextStrings.RuntimeTargetPropertyName, WriteRuntimeTargetInfo(context)),
new JProperty(DependencyContextStrings.CompilationOptionsPropertName, WriteCompilationOptions(context.CompilationOptions)),
new JProperty(DependencyContextStrings.TargetsPropertyName, WriteTargets(context)),
new JProperty(DependencyContextStrings.LibrariesPropertyName, WriteLibraries(context))
);
if (context.RuntimeGraph.Any())
{
contextObject.Add(new JProperty(DependencyContextStrings.RuntimesPropertyName, WriteRuntimeGraph(context)));
}
return contextObject;
}
private JObject WriteRuntimeTargetInfo(DependencyContext context)
{
return new JObject(
new JProperty(DependencyContextStrings.RuntimeTargetNamePropertyName,
context.Target.IsPortable ?
context.Target.Framework :
context.Target.Framework + DependencyContextStrings.VersionSeperator + context.Target.Runtime
),
new JProperty(DependencyContextStrings.RuntimeTargetSignaturePropertyName,
context.Target.RuntimeSignature
)
);
}
private JObject WriteRuntimeGraph(DependencyContext context)
{
return new JObject(
context.RuntimeGraph.Select(g => new JProperty(g.Runtime, new JArray(g.Fallbacks)))
);
}
private JObject WriteCompilationOptions(CompilationOptions compilationOptions)
{
var o = new JObject();
if (compilationOptions.Defines?.Any() == true)
{
o[DependencyContextStrings.DefinesPropertyName] = new JArray(compilationOptions.Defines);
}
AddPropertyIfNotNull(o, DependencyContextStrings.LanguageVersionPropertyName, compilationOptions.LanguageVersion);
AddPropertyIfNotNull(o, DependencyContextStrings.PlatformPropertyName, compilationOptions.Platform);
AddPropertyIfNotNull(o, DependencyContextStrings.AllowUnsafePropertyName, compilationOptions.AllowUnsafe);
AddPropertyIfNotNull(o, DependencyContextStrings.WarningsAsErrorsPropertyName, compilationOptions.WarningsAsErrors);
AddPropertyIfNotNull(o, DependencyContextStrings.OptimizePropertyName, compilationOptions.Optimize);
AddPropertyIfNotNull(o, DependencyContextStrings.KeyFilePropertyName, compilationOptions.KeyFile);
AddPropertyIfNotNull(o, DependencyContextStrings.DelaySignPropertyName, compilationOptions.DelaySign);
AddPropertyIfNotNull(o, DependencyContextStrings.PublicSignPropertyName, compilationOptions.PublicSign);
AddPropertyIfNotNull(o, DependencyContextStrings.EmitEntryPointPropertyName, compilationOptions.EmitEntryPoint);
AddPropertyIfNotNull(o, DependencyContextStrings.GenerateXmlDocumentationPropertyName, compilationOptions.GenerateXmlDocumentation);
AddPropertyIfNotNull(o, DependencyContextStrings.DebugTypePropertyName, compilationOptions.DebugType);
return o;
}
private void AddPropertyIfNotNull<T>(JObject o, string name, T value)
{
if (value != null)
{
o.Add(new JProperty(name, value));
}
}
private JObject WriteTargets(DependencyContext context)
{
if (context.Target.IsPortable)
{
return new JObject(
new JProperty(context.Target.Framework, WritePortableTarget(context.RuntimeLibraries, context.CompileLibraries))
);
}
return new JObject(
new JProperty(context.Target.Framework, WriteTarget(context.CompileLibraries)),
new JProperty(context.Target.Framework + DependencyContextStrings.VersionSeperator + context.Target.Runtime,
WriteTarget(context.RuntimeLibraries))
);
}
private JObject WriteTarget(IReadOnlyList<Library> libraries)
{
return new JObject(
libraries.Select(library =>
// TODO: remove the hack of calling "ToLowerInvariant()" on library Name and Version once
// https://github.com/dotnet/core-setup/pull/64/ is merged and taken into the CLI
new JProperty(library.Name.ToLowerInvariant() + DependencyContextStrings.VersionSeperator + library.Version.ToLowerInvariant(), WriteTargetLibrary(library))));
}
private JObject WritePortableTarget(IReadOnlyList<RuntimeLibrary> runtimeLibraries, IReadOnlyList<CompilationLibrary> compilationLibraries)
{
var runtimeLookup = runtimeLibraries.ToDictionary(l => l.Name, StringComparer.OrdinalIgnoreCase);
var compileLookup = compilationLibraries.ToDictionary(l => l.Name, StringComparer.OrdinalIgnoreCase);
var targetObject = new JObject();
foreach (var packageName in runtimeLookup.Keys.Concat(compileLookup.Keys).Distinct())
{
RuntimeLibrary runtimeLibrary;
runtimeLookup.TryGetValue(packageName, out runtimeLibrary);
CompilationLibrary compilationLibrary;
compileLookup.TryGetValue(packageName, out compilationLibrary);
if (compilationLibrary != null && runtimeLibrary != null)
{
Debug.Assert(compilationLibrary.Serviceable == runtimeLibrary.Serviceable);
Debug.Assert(compilationLibrary.Version == runtimeLibrary.Version);
Debug.Assert(compilationLibrary.Hash == runtimeLibrary.Hash);
Debug.Assert(compilationLibrary.Type == runtimeLibrary.Type);
Debug.Assert(compilationLibrary.Path == runtimeLibrary.Path);
}
var library = (Library)compilationLibrary ?? (Library)runtimeLibrary;
targetObject.Add(
// TODO: remove the hack of calling "ToLowerInvariant()" on library Name and Version once
// https://github.com/dotnet/core-setup/pull/64/ is merged and taken into the CLI
new JProperty(library.Name.ToLowerInvariant() + DependencyContextStrings.VersionSeperator + library.Version.ToLowerInvariant(),
WritePortableTargetLibrary(runtimeLibrary, compilationLibrary)
)
);
}
return targetObject;
}
private void AddCompilationAssemblies(JObject libraryObject, IEnumerable<string> compilationAssemblies)
{
if (!compilationAssemblies.Any())
{
return;
}
libraryObject.Add(new JProperty(DependencyContextStrings.CompileTimeAssembliesKey,
WriteAssetList(compilationAssemblies))
);
}
private void AddAssets(JObject libraryObject, string key, RuntimeAssetGroup group)
{
if (group == null || !group.AssetPaths.Any())
{
return;
}
libraryObject.Add(new JProperty(key,
WriteAssetList(group.AssetPaths))
);
}
private void AddDependencies(JObject libraryObject, IEnumerable<Dependency> dependencies)
{
if (!dependencies.Any())
{
return;
}
libraryObject.AddFirst(
new JProperty(DependencyContextStrings.DependenciesPropertyName,
new JObject(
dependencies.Select(dependency => new JProperty(dependency.Name, dependency.Version))))
);
}
private void AddResourceAssemblies(JObject libraryObject, IEnumerable<ResourceAssembly> resourceAssemblies)
{
if (!resourceAssemblies.Any())
{
return;
}
libraryObject.Add(DependencyContextStrings.ResourceAssembliesPropertyName,
new JObject(resourceAssemblies.Select(a =>
new JProperty(NormalizePath(a.Path), new JObject(new JProperty(DependencyContextStrings.LocalePropertyName, a.Locale))))
)
);
}
private JObject WriteTargetLibrary(Library library)
{
var runtimeLibrary = library as RuntimeLibrary;
if (runtimeLibrary != null)
{
var libraryObject = new JObject();
AddDependencies(libraryObject, runtimeLibrary.Dependencies);
// Add runtime-agnostic assets
AddAssets(libraryObject, DependencyContextStrings.RuntimeAssembliesKey, runtimeLibrary.RuntimeAssemblyGroups.GetDefaultGroup());
AddAssets(libraryObject, DependencyContextStrings.NativeLibrariesKey, runtimeLibrary.NativeLibraryGroups.GetDefaultGroup());
AddResourceAssemblies(libraryObject, runtimeLibrary.ResourceAssemblies);
return libraryObject;
}
var compilationLibrary = library as CompilationLibrary;
if (compilationLibrary != null)
{
var libraryObject = new JObject();
AddDependencies(libraryObject, compilationLibrary.Dependencies);
AddCompilationAssemblies(libraryObject, compilationLibrary.Assemblies);
return libraryObject;
}
throw new NotSupportedException();
}
private JObject WritePortableTargetLibrary(RuntimeLibrary runtimeLibrary, CompilationLibrary compilationLibrary)
{
var libraryObject = new JObject();
var dependencies = new HashSet<Dependency>();
if (runtimeLibrary != null)
{
// Add runtime-agnostic assets
AddAssets(libraryObject, DependencyContextStrings.RuntimeAssembliesKey, runtimeLibrary.RuntimeAssemblyGroups.GetDefaultGroup());
AddAssets(libraryObject, DependencyContextStrings.NativeLibrariesKey, runtimeLibrary.NativeLibraryGroups.GetDefaultGroup());
AddResourceAssemblies(libraryObject, runtimeLibrary.ResourceAssemblies);
// Add runtime-specific assets
var runtimeTargets = new JObject();
AddRuntimeSpecificAssetGroups(runtimeTargets, DependencyContextStrings.RuntimeAssetType, runtimeLibrary.RuntimeAssemblyGroups);
AddRuntimeSpecificAssetGroups(runtimeTargets, DependencyContextStrings.NativeAssetType, runtimeLibrary.NativeLibraryGroups);
if (runtimeTargets.Count > 0)
{
libraryObject.Add(DependencyContextStrings.RuntimeTargetsPropertyName, runtimeTargets);
}
dependencies.UnionWith(runtimeLibrary.Dependencies);
}
if (compilationLibrary != null)
{
AddCompilationAssemblies(libraryObject, compilationLibrary.Assemblies);
dependencies.UnionWith(compilationLibrary.Dependencies);
}
AddDependencies(libraryObject, dependencies);
if (compilationLibrary != null && runtimeLibrary == null)
{
libraryObject.Add(DependencyContextStrings.CompilationOnlyPropertyName, true);
}
return libraryObject;
}
private void AddRuntimeSpecificAssetGroups(JObject runtimeTargets, string assetType, IEnumerable<RuntimeAssetGroup> assetGroups)
{
foreach (var group in assetGroups.Where(g => !string.IsNullOrEmpty(g.Runtime)))
{
if (group.AssetPaths.Any())
{
AddRuntimeSpecificAssets(runtimeTargets, group.AssetPaths, group.Runtime, assetType);
}
else
{
// Add a placeholder item
// We need to generate a pseudo-path because there could be multiple different asset groups with placeholders
// Only the last path segment matters, the rest is basically just a GUID.
var pseudoPathFolder = assetType == DependencyContextStrings.RuntimeAssetType ?
"lib" :
"native";
runtimeTargets[$"runtime/{group.Runtime}/{pseudoPathFolder}/_._"] = new JObject(
new JProperty(DependencyContextStrings.RidPropertyName, group.Runtime),
new JProperty(DependencyContextStrings.AssetTypePropertyName, assetType));
}
}
}
private void AddRuntimeSpecificAssets(JObject target, IEnumerable<string> assets, string runtime, string assetType)
{
foreach (var asset in assets)
{
target.Add(new JProperty(NormalizePath(asset),
new JObject(
new JProperty(DependencyContextStrings.RidPropertyName, runtime),
new JProperty(DependencyContextStrings.AssetTypePropertyName, assetType)
)
));
}
}
private JObject WriteAssetList(IEnumerable<string> assetPaths)
{
return new JObject(assetPaths.Select(assembly => new JProperty(NormalizePath(assembly), new JObject())));
}
private JObject WriteLibraries(DependencyContext context)
{
var allLibraries =
context.RuntimeLibraries.Cast<Library>().Concat(context.CompileLibraries)
// TODO: remove the hack of calling "ToLowerInvariant()" on library Name and Version once
// https://github.com/dotnet/core-setup/pull/64/ is merged and taken into the CLI
.GroupBy(library => library.Name.ToLowerInvariant() + DependencyContextStrings.VersionSeperator + library.Version.ToLowerInvariant());
return new JObject(allLibraries.Select(libraries => new JProperty(libraries.Key, WriteLibrary(libraries.First()))));
}
private JObject WriteLibrary(Library library)
{
return new JObject(
new JProperty(DependencyContextStrings.TypePropertyName, library.Type),
new JProperty(DependencyContextStrings.ServiceablePropertyName, library.Serviceable),
new JProperty(DependencyContextStrings.Sha512PropertyName, library.Hash),
new JProperty(DependencyContextStrings.PathPropertyName, library.Path));
}
private string NormalizePath(string path)
{
return path.Replace('\\', '/');
}
}
}