-
Notifications
You must be signed in to change notification settings - Fork 353
/
FindDotNetCliPackage.cs
152 lines (133 loc) · 5.57 KB
/
FindDotNetCliPackage.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
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
namespace Microsoft.DotNet.Helix.Sdk
{
public class FindDotNetCliPackage : BaseTask
{
private static readonly HttpClient _client = new HttpClient(new HttpClientHandler { CheckCertificateRevocationList = true });
private const string DotNetCliAzureFeed = "https://dotnetcli.azureedge.net/dotnet";
/// <summary>
/// 'LTS' or 'Current'
/// </summary>
[Required]
public string Channel { get; set; }
/// <summary>
/// 'latest' or specific version
/// </summary>
[Required]
public string Version { get; set; }
/// <summary>
/// RID of dotnet cli to get
/// </summary>
[Required]
public string Runtime { get; set; }
/// <summary>
/// 'sdk', 'runtime' or 'aspnetcore-runtime'
/// </summary>
[Required]
public string PackageType { get; set; }
[Output]
public string PackageUri { get; set; }
public override bool Execute()
{
ExecuteAsync().GetAwaiter().GetResult();
return !Log.HasLoggedErrors;
}
private async Task ExecuteAsync()
{
NormalizeParameters();
await ResolveVersionAsync();
string downloadUrl = GetDownloadUrl();
Log.LogMessage($"Retrieved dotnet cli {PackageType} version {Version} package uri {downloadUrl}, testing...");
try
{
using var req = new HttpRequestMessage(HttpMethod.Head, downloadUrl);
using HttpResponseMessage res = await _client.SendAsync(req);
if (res.StatusCode == HttpStatusCode.NotFound)
{
// 404 means that we successfully hit the server, and it returned 404. This cannot be a network hiccup
Log.LogError(FailureCategory.Build, $"Unable to find dotnet cli {PackageType} version {Version}, tried {downloadUrl}");
}
else
{
res.EnsureSuccessStatusCode();
}
}
catch (Exception ex)
{
Log.LogError(FailureCategory.Build, $"Unable to access dotnet cli {PackageType} version {Version} at {downloadUrl}, {ex.Message}");
}
if (!Log.HasLoggedErrors)
{
Log.LogMessage($"Url {downloadUrl} is valid.");
PackageUri = downloadUrl;
}
}
private string GetDownloadUrl()
{
string extension = Runtime.StartsWith("win") ? "zip" : "tar.gz";
return PackageType switch
{
"sdk" => $"{DotNetCliAzureFeed}/Sdk/{Version}/dotnet-sdk-{Version}-{Runtime}.{extension}",
"aspnetcore-runtime" => $"{DotNetCliAzureFeed}/aspnetcore/Runtime/{Version}/aspnetcore-runtime-{Version}-{Runtime}.{extension}",
_ => $"{DotNetCliAzureFeed}/Runtime/{Version}/dotnet-runtime-{Version}-{Runtime}.{extension}"
};
}
private void NormalizeParameters()
{
if (string.Equals(Channel, "lts", StringComparison.OrdinalIgnoreCase))
{
Channel = "LTS";
}
else if (string.Equals(Channel, "current", StringComparison.OrdinalIgnoreCase))
{
Channel = "Current";
}
else
{
throw new ArgumentException($"Invalid value '{Channel}' for parameter {nameof(Channel)}");
}
if (string.Equals(Version, "latest", StringComparison.OrdinalIgnoreCase))
{
Version = "latest";
}
if (string.Equals(PackageType, "sdk", StringComparison.OrdinalIgnoreCase))
{
PackageType = "sdk";
}
else if (string.Equals(PackageType, "aspnetcore-runtime", StringComparison.OrdinalIgnoreCase))
{
PackageType = "aspnetcore-runtime";
}
else if (string.Equals(PackageType, "runtime", StringComparison.OrdinalIgnoreCase))
{
PackageType = "runtime";
}
else
{
throw new ArgumentException($"Invalid value '{PackageType}' for parameter {nameof(PackageType)}");
}
}
private async Task ResolveVersionAsync()
{
if (Version == "latest")
{
Log.LogMessage(MessageImportance.Low, "Resolving latest dotnet cli version.");
string latestVersionUrl = PackageType switch
{
"sdk" => $"{DotNetCliAzureFeed}/Sdk/{Channel}/latest.version",
"aspnetcore-runtime" => $"{DotNetCliAzureFeed}/aspnetcore/Runtime/{Channel}/latest.version",
_ => $"{DotNetCliAzureFeed}/Runtime/{Channel}/latest.version"
};
Log.LogMessage(MessageImportance.Low, $"Resolving latest version from url {latestVersionUrl}");
string latestVersionContent = await _client.GetStringAsync(latestVersionUrl);
string[] versionData = latestVersionContent.Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries);
Version = versionData[1];
Log.LogMessage(MessageImportance.Low, $"Got latest dotnet cli version {Version}");
}
}
}
}