-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
303 lines (259 loc) · 8.84 KB
/
Program.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
using DotnetRelease;
using EndOfLifeDate;
using MarkdownHelpers;
if (args.Length is 0 || !int.TryParse(args[0], out int ver))
{
ReportInvalidArgs();
return;
}
string version = $"{ver}.0";
string baseDefaultURL = "https://raw.githubusercontent.com/dotnet/core/main/release-notes/";
string baseUrl = args.Length > 1 ? args[1] : baseDefaultURL;
bool preferWeb = baseUrl.StartsWith("https");
string supportJson = baseUrl;
if (!supportJson.EndsWith(".json"))
{
supportJson = preferWeb ?
$"{baseUrl}/{version}/supported-os.json" :
Path.Combine(baseUrl, version,"supported-os.json");
}
string template = $"supported-os-template{ver}.md";
string file = $"supported-os{ver}.md";
string placeholder = "PLACEHOLDER-";
HttpClient client = new();
FileStream stream = File.Open(file, FileMode.Create);
StreamWriter writer = new(stream);
SupportedOSMatrix? matrix = null;
Link pageLinks = new();
if (preferWeb)
{
matrix = await ReleaseNotes.GetSupportedOSes(client, supportJson) ?? throw new();
}
else
{
matrix = await ReleaseNotes.GetSupportedOSes(File.OpenRead(supportJson)) ?? throw new();
}
foreach (string line in File.ReadLines(template))
{
if (!line.StartsWith(placeholder))
{
writer.WriteLine(line);
continue;
}
if (line.StartsWith("PLACEHOLDER-LASTUPDATED"))
{
WriteLastUpdatedSection(writer, matrix.LastUpdated);
}
else if (line.StartsWith("PLACEHOLDER-FAMILIES"))
{
WriteFamiliesSection(writer, matrix.Families, pageLinks);
}
else if (line.StartsWith("PLACEHOLDER-LIBC") && matrix.Libc is {})
{
WriteLibcSection(writer, matrix.Libc);
}
else if (line.StartsWith("PLACEHOLDER-NOTES") && matrix.Notes is {})
{
WriteNotesSection(writer, matrix.Notes);
}
else if (line.StartsWith("PLACEHOLDER-UNSUPPORTED"))
{
await WriteUnSupportedSection(writer, matrix.Families, client);
}
}
if (pageLinks.Count > 0)
{
writer.WriteLine();
foreach (string link in pageLinks.GetReferenceLinkAnchors())
{
writer.WriteLine(link);
}
}
writer.Close();
var writtenFile = File.OpenRead(file);
long length = writtenFile.Length;
string path = writtenFile.Name;
writtenFile.Close();
Console.WriteLine($"Generated {length} bytes");
Console.WriteLine(path);
static void ReportInvalidArgs()
{
Console.WriteLine("Invalid args.");
Console.WriteLine("Expected: version [URL or Path, absolute or root location]");
}
static void WriteLastUpdatedSection(StreamWriter writer, DateOnly date)
{
writer.WriteLine($"Last updated: {date.ToString("yyyy-MM-dd")}");
}
static void WriteFamiliesSection(StreamWriter writer, IList<SupportFamily> families, Link links)
{
string[] labels = [ "OS", "Versions", "Architectures", "Lifecycle" ];
int[] lengths = [32, 30, 24, 24];
Table defaultTable = new(Writer.GetWriter(writer), lengths);
int linkCount = 0;
foreach (SupportFamily family in families)
{
Table table = defaultTable;
Link familyLinks = new(linkCount);
writer.WriteLine($"## {family.Name}");
writer.WriteLine();
if (family.Name is "Apple")
{
int[] appleLengths = [32, 30, 24];
table = new(Writer.GetWriter(writer), appleLengths);
table.WriteHeader(labels.AsSpan(0, 3));
}
else
{
table.WriteHeader(labels);
}
List<string> notes = [];
for (int i = 0; i < family.Distributions.Count; i++)
{
SupportDistribution distro = family.Distributions[i];
IList<string> distroVersions = distro.SupportedVersions;
bool hasLifecycle = distro.Lifecycle is not null;
if (distro.Name is "Windows")
{
distroVersions = SupportedOS.SimplifyWindowsVersions(distro.SupportedVersions);
}
string versions = "";
if (distroVersions.Count is 0)
{
var noneLink = links.AddReferenceLink("None", "#out-of-support-os-versions");
versions = noneLink;
}
else
{
versions = Join(distroVersions);
}
var distroLink = familyLinks.AddIndexReferenceLink(distro.Name, distro.Link);
table.WriteColumn(distroLink);
table.WriteColumn(versions);
table.WriteColumn(Join(distro.Architectures));
if (distro.Lifecycle is not null)
{
var lifecycleLink = familyLinks.AddIndexReferenceLink("Lifecycle", distro.Lifecycle);
table.WriteColumn(lifecycleLink);
}
table.EndRow();
linkCount = familyLinks.Index;
if (distro.Notes is {Count: > 0})
{
foreach (string note in distro.Notes)
{
notes.Add($"{distro.Name}: {note}");
}
}
}
if (notes.Count > 0)
{
writer.WriteLine();
writer.WriteLine("Notes:");
writer.WriteLine();
foreach (string note in notes)
{
writer.WriteLine($"* {note}");
}
}
writer.WriteLine();
foreach (var refLink in familyLinks.GetReferenceLinkAnchors())
{
writer.WriteLine(refLink);
}
writer.WriteLine();
}
}
static void WriteLibcSection(StreamWriter writer, IList<SupportLibc> supportedLibc)
{
string[] columnLabels = [ "Libc", "Version", "Architectures", "Source"];
int[] columnLengths = [16, 10, 24, 16 ];
Table table = new(Writer.GetWriter(writer), columnLengths);
table.WriteHeader(columnLabels);
foreach (SupportLibc libc in supportedLibc)
{
table.WriteColumn(libc.Name);
table.WriteColumn(libc.Version);
table.WriteColumn(Join(libc.Architectures));
table.WriteColumn(libc.Source);
table.EndRow();
}
}
static void WriteNotesSection(StreamWriter writer, IList<string> notes)
{
foreach (string note in notes)
{
writer.WriteLine($"* {note}");
}
}
static async Task WriteUnSupportedSection(StreamWriter writer, IList<SupportFamily> families, HttpClient client)
{
// Get all unsupported cycles in parallel.
var eolCycles = await Task.WhenAll(families
.SelectMany(f => f.Distributions
.SelectMany(d => (d.UnsupportedVersions ?? [])
.Select(async v => new
{
Distribution = d,
Version = v,
Cycle = await GetProductCycle(client, d, v)
}))));
// Order the list of cycles by their EoL date.
var orderedEolCycles = eolCycles
.OrderBy(entry => entry.Distribution.Name)
.ThenByDescending(entry => GetEolDateForCycle(entry.Cycle))
.ToArray();
if (eolCycles.Length == 0)
{
writer.WriteLine("None currently.");
return;
}
string[] labels = [ "OS", "Version", "Date" ];
int[] lengths = [24, 16, 24];
Table table = new(Writer.GetWriter(writer), lengths);
table.WriteHeader(labels);
foreach (var entry in orderedEolCycles)
{
var eol = GetEolTextForCycle(entry.Cycle);
var distroName = entry.Distribution.Name;
var distroVersion = entry.Version;
if (distroName is "Windows")
{
distroVersion = SupportedOS.PrettyifyWindowsVersion(distroVersion);
}
table.WriteColumn(distroName);
table.WriteColumn(distroVersion);
table.WriteColumn(eol);
table.EndRow();
}
}
static string GetEolTextForCycle(SupportCycle? cycle)
{
var eolDate = GetEolDateForCycle(cycle);
if (cycle == null || eolDate == DateOnly.MinValue) return "-";
var result = eolDate.ToString("yyyy-MM-dd");
var link = cycle.Link;
if (link != null)
{
result = $"[{result}]({link})";
}
return result;
}
static async Task<SupportCycle?> GetProductCycle(HttpClient client, SupportDistribution distro, string unsupportedVersion)
{
try
{
return await EndOfLifeDate.Product.GetProductCycle(client, distro.Id, unsupportedVersion);
}
catch (HttpRequestException)
{
Console.WriteLine($"No data found at endoflife.date for: {distro.Id} {unsupportedVersion}");
Console.WriteLine();
return null;
}
}
static DateOnly GetEolDateForCycle(SupportCycle? supportCycle)
{
return supportCycle?.GetSupportInfo().EolDate ?? DateOnly.MinValue;
}
static string Join(IEnumerable<string>? strings) => strings is null ? "" : string.Join(", ", strings);