-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathBuildScriptGeneratorServiceCollectionExtensions.cs
More file actions
73 lines (67 loc) · 3.42 KB
/
BuildScriptGeneratorServiceCollectionExtensions.cs
File metadata and controls
73 lines (67 loc) · 3.42 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
// --------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// --------------------------------------------------------------------------------------------
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Oryx.Detector;
using Polly;
using Polly.Extensions.Http;
namespace Microsoft.Oryx.BuildScriptGenerator
{
public static class BuildScriptGeneratorServiceCollectionExtensions
{
public static IServiceCollection AddBuildScriptGeneratorServices(this IServiceCollection services)
{
services
.AddPlatformDetectorServices()
.AddNodeScriptGeneratorServices()
.AddHugoScriptGeneratorServices()
.AddPythonScriptGeneratorServices()
.AddDotNetCoreScriptGeneratorServices()
.AddPhpScriptGeneratorServices()
.AddScriptGeneratorServicesRuby()
.AddScriptGeneratorServicesGolang()
.AddScriptGeneratorServicesForJava();
services.AddSingleton<IBuildScriptGenerator, DefaultBuildScriptGenerator>();
services.AddSingleton<ICompatiblePlatformDetector, DefaultCompatiblePlatformDetector>();
services.AddSingleton<IDockerfileGenerator, DefaultDockerfileGenerator>();
services.AddSingleton<IEnvironment, DefaultEnvironment>();
services.AddSingleton<ISourceRepoProvider, DefaultSourceRepoProvider>();
services.AddSingleton<ITempDirectoryProvider, DefaulTempDirectoryProvider>();
services.AddSingleton<IScriptExecutor, DefaultScriptExecutor>();
services.AddSingleton<IRunScriptGenerator, DefaultRunScriptGenerator>();
services.AddSingleton<DefaultPlatformsInformationProvider>();
services.AddSingleton<PlatformsInstallationScriptProvider>();
services.AddSingleton<IExternalSdkProvider, ExternalSdkProvider>();
services.AddSingleton<IMcrSdkProvider, McrSdkProvider>();
services.AddHttpClient("general", httpClient =>
{
// NOTE: Setting user agent is required to avoid receiving 403 Forbidden response.
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("oryx", "1.0"));
}).AddPolicyHandler(GetRetryPolicy());
// Add all checkers (platform-dependent + platform-independent)
foreach (Type type in typeof(BuildScriptGeneratorServiceCollectionExtensions).Assembly.GetTypes())
{
if (type.GetCustomAttributes(typeof(CheckerAttribute), false).Length > 0)
{
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IChecker), type));
}
}
return services;
}
private static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.OrResult(msg => msg.StatusCode == HttpStatusCode.NotFound)
.WaitAndRetryAsync(
retryCount: 6,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
}
}
}