-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathDotnetCorePlatform.cs
More file actions
531 lines (467 loc) · 22.8 KB
/
DotnetCorePlatform.cs
File metadata and controls
531 lines (467 loc) · 22.8 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// --------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// --------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.ApplicationInsights;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Oryx.BuildScriptGenerator.Exceptions;
using Microsoft.Oryx.Common.Extensions;
using Microsoft.Oryx.Detector;
using Microsoft.Oryx.Detector.DotNetCore;
namespace Microsoft.Oryx.BuildScriptGenerator.DotNetCore
{
/// <summary>
/// .NET Core platform.
/// </summary>
[BuildProperty(
DotNetCoreConstants.ProjectBuildPropertyKey,
DotNetCoreConstants.ProjectBuildPropertyKeyDocumentation)]
internal class DotNetCorePlatform : IProgrammingPlatform
{
private readonly IDotNetCoreVersionProvider versionProvider;
private readonly ILogger<DotNetCorePlatform> logger;
private readonly IDotNetCorePlatformDetector detector;
private readonly DotNetCoreScriptGeneratorOptions dotNetCoreScriptGeneratorOptions;
private readonly BuildScriptGeneratorOptions commonOptions;
private readonly DotNetCorePlatformInstaller platformInstaller;
private readonly GlobalJsonSdkResolver globalJsonSdkResolver;
private readonly IExternalSdkProvider externalSdkProvider;
private readonly IExternalAcrSdkProvider externalAcrSdkProvider;
private readonly TelemetryClient telemetryClient;
/// <summary>
/// Initializes a new instance of the <see cref="DotNetCorePlatform"/> class.
/// </summary>
/// <param name="versionProvider">The .NET version provider.</param>
/// <param name="logger">The logger of .NET platform.</param>
/// <param name="detector">The detector of .NET platform.</param>
/// <param name="commonOptions">The build options for BuildScriptGenerator.</param>
/// <param name="dotNetCoreScriptGeneratorOptions">The options if .NET platform.</param>
/// <param name="platformInstaller">The <see cref="DotNetCorePlatformInstaller"/>.</param>
/// <param name="globalJsonSdkResolver">The <see cref="GlobalJsonSdkResolver"/>.</param>
public DotNetCorePlatform(
IDotNetCoreVersionProvider versionProvider,
ILogger<DotNetCorePlatform> logger,
IDotNetCorePlatformDetector detector,
IOptions<BuildScriptGeneratorOptions> commonOptions,
IOptions<DotNetCoreScriptGeneratorOptions> dotNetCoreScriptGeneratorOptions,
DotNetCorePlatformInstaller platformInstaller,
GlobalJsonSdkResolver globalJsonSdkResolver,
IExternalSdkProvider externalSdkProvider,
IExternalAcrSdkProvider externalAcrSdkProvider,
TelemetryClient telemetryClient)
{
this.versionProvider = versionProvider;
this.logger = logger;
this.detector = detector;
this.dotNetCoreScriptGeneratorOptions = dotNetCoreScriptGeneratorOptions.Value;
this.commonOptions = commonOptions.Value;
this.platformInstaller = platformInstaller;
this.globalJsonSdkResolver = globalJsonSdkResolver;
this.externalSdkProvider = externalSdkProvider;
this.externalAcrSdkProvider = externalAcrSdkProvider;
this.telemetryClient = telemetryClient;
}
/// <inheritdoc/>
public string Name => DotNetCoreConstants.PlatformName;
/// <inheritdoc/>
public IEnumerable<string> SupportedVersions
{
get
{
var versionMap = this.versionProvider.GetSupportedVersions();
// Map is from runtime version => sdk version
return versionMap.Keys;
}
}
/// <inheritdoc/>
public PlatformDetectorResult Detect(RepositoryContext context)
{
try
{
var detectionResult = this.detector.Detect(new DetectorContext
{
SourceRepo = new Detector.LocalSourceRepo(context.SourceRepo.RootPath),
});
if (detectionResult == null)
{
return null;
}
this.ResolveVersions(context, detectionResult);
return detectionResult;
}
catch (InvalidProjectFileException e)
{
this.logger.LogError(e, "Error occurred while trying to detect for .Net Core application(s)");
throw new InvalidUsageException(e.Message);
}
}
/// <inheritdoc/>
public BuildScriptSnippet GenerateBashBuildScriptSnippet(
BuildScriptGeneratorContext context,
PlatformDetectorResult detectorResult)
{
var dotNetCorePlatformDetectorResult = detectorResult as DotNetCorePlatformDetectorResult;
if (dotNetCorePlatformDetectorResult == null)
{
throw new ArgumentException(
$"Expected '{nameof(detectorResult)}' argument to be of type " +
$"'{typeof(DotNetCorePlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
}
var manifestFileProperties = new Dictionary<string, string>();
manifestFileProperties[ManifestFilePropertyKeys.OperationId] = context.OperationId;
manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreRuntimeVersion]
= dotNetCorePlatformDetectorResult.PlatformVersion;
manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreSdkVersion]
= dotNetCorePlatformDetectorResult.SdkVersion;
// optional field
string outputType = dotNetCorePlatformDetectorResult.OutputType;
if (!string.IsNullOrEmpty(outputType))
{
manifestFileProperties[ManifestFilePropertyKeys.OutputType] = outputType;
}
var projectFile = dotNetCorePlatformDetectorResult.ProjectFile;
if (string.IsNullOrEmpty(projectFile))
{
return null;
}
string installBlazorWebAssemblyAOTWorkloadCommand = null;
if (dotNetCorePlatformDetectorResult.InstallAOTWorkloads)
{
installBlazorWebAssemblyAOTWorkloadCommand = DotNetCoreConstants.InstallBlazorWebAssemblyAOTWorkloadCommand;
manifestFileProperties[ManifestFilePropertyKeys.Frameworks] = "blazor";
this.logger.LogInformation("Detected the following frameworks: blazor");
Console.WriteLine("Detected the following frameworks: blazor");
}
var templateProperties = new DotNetCoreBashBuildSnippetProperties
{
ProjectFile = projectFile,
Configuration = this.GetBuildConfiguration(),
InstallBlazorWebAssemblyAOTWorkloadCommand = installBlazorWebAssemblyAOTWorkloadCommand,
};
var script = TemplateHelper.Render(
TemplateHelper.TemplateResource.DotNetCoreSnippet,
templateProperties,
this.logger,
this.telemetryClient);
SetStartupFileNameInfoInManifestFile(context, projectFile, manifestFileProperties);
return new BuildScriptSnippet
{
BashBuildScriptSnippet = script,
BuildProperties = manifestFileProperties,
// Setting this to false to avoid copying files like '.cs' to the destination
CopySourceDirectoryContentToDestinationDirectory = false,
};
}
/// <inheritdoc/>
public bool IsCleanRepo(ISourceRepo repo)
{
return true;
}
/// <inheritdoc/>
public string GenerateBashRunTimeInstallationScript(RunTimeInstallationScriptGeneratorOptions options)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public bool IsEnabled(RepositoryContext ctx)
{
return this.commonOptions.EnableDotNetCoreBuild;
}
/// <inheritdoc/>
public bool IsEnabledForMultiPlatformBuild(RepositoryContext ctx)
{
return true;
}
/// <inheritdoc/>
public IEnumerable<string> GetDirectoriesToExcludeFromCopyToBuildOutputDir(
BuildScriptGeneratorContext scriptGeneratorContext)
{
var dirs = new List<string>();
dirs.Add("obj");
dirs.Add("bin");
return dirs;
}
/// <inheritdoc/>
public IEnumerable<string> GetDirectoriesToExcludeFromCopyToIntermediateDir(
BuildScriptGeneratorContext scriptGeneratorContext)
{
var dirs = new List<string>();
dirs.Add(".git");
dirs.Add("obj");
dirs.Add("bin");
return dirs;
}
/// <inheritdoc/>
public string GetInstallerScriptSnippet(
BuildScriptGeneratorContext context,
PlatformDetectorResult detectorResult)
{
var dotNetCorePlatformDetectorResult = detectorResult as DotNetCorePlatformDetectorResult;
if (dotNetCorePlatformDetectorResult == null)
{
throw new ArgumentException(
$"Expected '{nameof(detectorResult)}' argument to be of type " +
$"'{typeof(DotNetCorePlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
}
if (!this.commonOptions.EnableDynamicInstall)
{
this.logger.LogDebug("Dynamic install is not enabled.");
return null;
}
this.logger.LogDebug("Dynamic install is enabled.");
var sdkVersion = dotNetCorePlatformDetectorResult.SdkVersion;
if (this.platformInstaller.IsVersionAlreadyInstalled(sdkVersion))
{
this.logger.LogDebug(
"DotNetCore SDK version {globalJsonSdkVersion} is already installed. So skipping installing it again.",
sdkVersion);
return null;
}
if (this.commonOptions.EnableExternalSdkProvider)
{
return this.TryInstallFromExternalSdkProvider(sdkVersion);
}
if (this.commonOptions.EnableAcrSdkProvider)
{
return this.TryInstallFromAcrSdkProvider(sdkVersion);
}
this.logger.LogDebug(
"DotNetCore SDK version {globalJsonSdkVersion} is not installed. So generating an installation script snippet for it.",
sdkVersion);
return this.platformInstaller.GetInstallerScriptSnippet(sdkVersion);
}
/// <inheritdoc/>
public void ResolveVersions(RepositoryContext context, PlatformDetectorResult detectorResult)
{
var dotNetCorePlatformDetectorResult = detectorResult as DotNetCorePlatformDetectorResult;
if (dotNetCorePlatformDetectorResult == null)
{
throw new ArgumentException(
$"Expected '{nameof(detectorResult)}' argument to be of type " +
$"'{typeof(DotNetCorePlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
}
// Get runtime version
var resolvedRuntimeVersion = this.GetRuntimeVersionUsingHierarchicalRules(
dotNetCorePlatformDetectorResult.PlatformVersion);
resolvedRuntimeVersion = this.GetMaxSatisfyingRuntimeVersionAndVerify(resolvedRuntimeVersion);
dotNetCorePlatformDetectorResult.PlatformVersion = resolvedRuntimeVersion;
var versionMap = this.versionProvider.GetSupportedVersions();
var sdkVersion = this.GetSdkVersion(context, dotNetCorePlatformDetectorResult.PlatformVersion, versionMap);
dotNetCorePlatformDetectorResult.SdkVersion = sdkVersion;
}
/// <inheritdoc/>
public IDictionary<string, string> GetToolsToBeSetInPath(
RepositoryContext context,
PlatformDetectorResult detectorResult)
{
var dotNetCorePlatformDetectorResult = detectorResult as DotNetCorePlatformDetectorResult;
if (dotNetCorePlatformDetectorResult == null)
{
throw new ArgumentException(
$"Expected '{nameof(detectorResult)}' argument to be of type " +
$"'{typeof(DotNetCorePlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
}
var tools = new Dictionary<string, string>();
tools[DotNetCoreConstants.PlatformName] = dotNetCorePlatformDetectorResult.SdkVersion;
return tools;
}
/// <summary>
/// Even though the runtime container has the logic of finding out the startup file based on
/// 'runtimeconfig.json' prefix, we still set the name in the manifest file because of the following
/// scenario: let's say output directory currently has 'foo.dll' and user made a change to the project
/// name or assembly name property to 'bar' which causes 'bar.dll' to be published. If the output
/// directory was NOT cleaned, then we would now be having both 'foo.runtimeconfig.json' and
/// 'bar.runtimeconfig.json' which causes a problem for runtime container as it cannot figure out the
/// right startup DLL. So, to help that scenario we always set the start-up file name in manifest file.
/// The runtime container will first look into manifest file to find the startup filename, if the
/// file name is not present or if a manifest file is not present at all(ex: in case of VS Publish where
/// the build does not happen with Oryx), then the runtime container's logic will fallback to looking at
/// runtimeconfig.json prefixes.
/// </summary>
private static void SetStartupFileNameInfoInManifestFile(
BuildScriptGeneratorContext context,
string projectFile,
IDictionary<string, string> buildProperties)
{
string startupDllFileName;
var projectFileContent = context.SourceRepo.ReadFile(projectFile);
var projFileDoc = XDocument.Load(new StringReader(projectFileContent));
var assemblyNameElement = projFileDoc.XPathSelectElement(DotNetCoreConstants.AssemblyNameXPathExpression);
if (assemblyNameElement == null)
{
var name = Path.GetFileNameWithoutExtension(projectFile);
startupDllFileName = $"{name}.dll";
}
else
{
startupDllFileName = $"{assemblyNameElement.Value}.dll";
}
buildProperties[DotNetCoreManifestFilePropertyKeys.StartupDllFileName] = startupDllFileName;
}
private string TryInstallFromAcrSdkProvider(string sdkVersion)
{
this.logger.LogDebug(
"DotNetCore SDK version {version} is not installed. ACR SDK provider is enabled, so trying to fetch SDK using it.",
sdkVersion);
try
{
if (this.externalAcrSdkProvider.RequestSdkFromAcrAsync(
this.Name, sdkVersion, this.commonOptions.DebianFlavor).Result)
{
this.logger.LogDebug(
"DotNetCore SDK version {version} is fetched successfully using ACR SDK provider. Skipping platform binary download.",
sdkVersion);
return this.platformInstaller.GetInstallerScriptSnippet(sdkVersion, skipSdkBinaryDownload: true);
}
this.logger.LogDebug(
"DotNetCore SDK version {version} is not fetched via ACR SDK provider. Falling back to CDN download.",
sdkVersion);
}
catch (Exception ex)
{
this.logger.LogError(
ex,
"Error while fetching DotNetCore SDK version {version} using ACR SDK provider. Falling back to CDN download.",
sdkVersion);
}
return this.platformInstaller.GetInstallerScriptSnippet(sdkVersion);
}
private string TryInstallFromExternalSdkProvider(string sdkVersion)
{
this.logger.LogDebug(
"DotNetCore SDK version {version} is not installed. External SDK provider is enabled so trying to fetch SDK using it.",
sdkVersion);
try
{
var blobName = BlobNameHelper.GetBlobNameForVersion(this.Name, sdkVersion, this.commonOptions.DebianFlavor);
if (this.externalSdkProvider.RequestBlobAsync(this.Name, blobName).Result)
{
this.logger.LogDebug(
"DotNetCore SDK version {version} is fetched successfully using external SDK provider. Skipping platform binary download.",
sdkVersion);
return this.platformInstaller.GetInstallerScriptSnippet(sdkVersion, skipSdkBinaryDownload: true);
}
this.logger.LogDebug(
"DotNetCore SDK version {version} is not fetched successfully using external SDK provider. Generating installation script snippet.",
sdkVersion);
}
catch (Exception ex)
{
this.logger.LogError(
ex,
"Error while fetching DotNetCore SDK version version {version} using external SDK provider.",
sdkVersion);
}
return this.platformInstaller.GetInstallerScriptSnippet(sdkVersion);
}
private string GetSdkVersion(
RepositoryContext context,
string runtimeVersion,
Dictionary<string, string> versionMap)
{
if (this.commonOptions.EnableDynamicInstall
&& context.SourceRepo.FileExists(DotNetCoreConstants.GlobalJsonFileName))
{
var availableSdks = versionMap.Values;
var globalJsonSdkVersion = this.globalJsonSdkResolver.GetSatisfyingSdkVersion(
context.SourceRepo,
runtimeVersion,
availableSdks);
return globalJsonSdkVersion;
}
return versionMap[runtimeVersion];
}
private string GetBuildConfiguration()
{
var configuration = this.dotNetCoreScriptGeneratorOptions.MSBuildConfiguration;
if (string.IsNullOrEmpty(configuration))
{
configuration = DotNetCoreConstants.DefaultMSBuildConfiguration;
}
return configuration;
}
private string GetMaxSatisfyingRuntimeVersionAndVerify(string runtimeVersion)
{
var versionMap = this.versionProvider.GetSupportedVersions();
// Since our semantic versioning library does not work with .NET Core preview version format, here
// we do some trivial way of finding the latest version which matches a given runtime version
// Runtime versions are usually like: 1.0, 2.1, 3.1, 5.0 etc.
// (these are constructed from netcoreapp21, netcoreapp31 etc.)
// Preview version of sdks also have preview versions of runtime versions and hence they
// have '-' in their names.
var nonPreviewRuntimeVersions = versionMap.Keys.Where(version => version.IndexOf("-") < 0);
var maxSatisfyingVersion = SemanticVersionResolver.GetMaxSatisfyingVersion(
runtimeVersion,
nonPreviewRuntimeVersions);
// Check if a preview version is available
if (string.IsNullOrEmpty(maxSatisfyingVersion))
{
// NOTE:
// Preview versions: 5.0.0-preview.3.20214.6, 5.0.0-preview.2.20160.6, 5.0.0-preview.1.20120.5
var previewRuntimeVersions = versionMap.Keys
.Where(version => version.Contains("-"))
.Where(version => version.StartsWith(runtimeVersion))
.OrderByDescending(version => version);
if (previewRuntimeVersions.Any())
{
maxSatisfyingVersion = previewRuntimeVersions.First();
}
}
if (string.IsNullOrEmpty(maxSatisfyingVersion))
{
var exception = new UnsupportedVersionException(
DotNetCoreConstants.PlatformName,
runtimeVersion,
versionMap.Keys);
this.logger.LogError(
exception,
$"Exception caught, the version '{runtimeVersion}' is not supported for the .NET Core platform.");
throw exception;
}
return maxSatisfyingVersion;
}
private string GetRuntimeVersionUsingHierarchicalRules(string detectedVersion)
{
// Explicitly specified version by user wins over detected version
if (!string.IsNullOrEmpty(this.dotNetCoreScriptGeneratorOptions.DotNetCoreRuntimeVersion))
{
return this.dotNetCoreScriptGeneratorOptions.DotNetCoreRuntimeVersion;
}
// If a version was detected, then use it.
if (!string.IsNullOrEmpty(detectedVersion))
{
return detectedVersion;
}
// Explicitly specified default version by user wins over detected default
if (!string.IsNullOrEmpty(this.dotNetCoreScriptGeneratorOptions.DefaultRuntimeVersion))
{
return this.dotNetCoreScriptGeneratorOptions.DefaultRuntimeVersion;
}
// Fallback to default version detection
var defaultVersion = this.versionProvider.GetDefaultRuntimeVersion();
return defaultVersion;
}
private bool TryGetExplicitVersion(out string explicitVersion)
{
explicitVersion = null;
var platformName = this.commonOptions.PlatformName;
if (platformName.EqualsIgnoreCase(DotNetCoreConstants.PlatformName))
{
if (string.IsNullOrWhiteSpace(this.dotNetCoreScriptGeneratorOptions.DotNetCoreRuntimeVersion))
{
return false;
}
explicitVersion = this.dotNetCoreScriptGeneratorOptions.DotNetCoreRuntimeVersion;
return true;
}
return false;
}
}
}