-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathProgram.cs
109 lines (89 loc) · 3.23 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
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using ConsoleAppFramework;
using Zx;
using static Zx.Env;
var app = ConsoleApp.Create();
app.Add("", async Task (Cancel _) =>
{
await "dotnet tool restore";
await "dotnet build -c Release --verbosity minimal";
await "dotnet test --configuration Release --logger GitHubActions -- RunConfiguration.CollectSourceInformation=true";
});
// this is manual for now and quite hacky.
// this ensures we download the actual LICENSE files in the repositories.
// NOT the SPDX html from licenses.nuget.org
app.Add("notices", async Task<int> (Cancel ctx) =>
{
var packages = await "dotnet thirdlicense --project src/docs-builder/docs-builder.csproj --output NOTICE.txt";
var packageLines = packages.Split(Environment.NewLine).Where(l => l.StartsWith("+"));
await File.WriteAllTextAsync("NOTICE.txt",
$"""
Elastic Documentation Tooling
Copyright 2024-{DateTime.UtcNow.Year} Elasticsearch B.V.
""", ctx);
Console.WriteLine("Package lines:");
foreach (var line in packageLines)
{
var package = line.Split('+', '(')[1].ToLowerInvariant().Trim();
var version = line.Split('(', ')')[1].TrimStart('v').Trim();
if (package.StartsWith("microsoft.") || package.StartsWith("system"))
continue;
var text = await fetchText($"https://api.nuget.org/v3-flatcontainer/{package}/{version}/{package}.nuspec");
var xml = XDocument.Load(new StringReader(text));
var projectUrl = xml.XPathSelectElement("//*[local-name()='projectUrl']")?.Value;
var id = xml.XPathSelectElement("//*[local-name()='id']")?.Value;
projectUrl = projectUrl?.Replace("/wiki", string.Empty);
if (projectUrl is null || projectUrl.Contains(".github.com"))
throw new Exception($"Can not download license for {id}: {projectUrl}");
var rawUrl = projectUrl.Replace("github.com", "raw.githubusercontent.com");
string[] targets =
[
rawUrl + $"/refs/heads/master/" + "LICENSE.txt",
rawUrl + $"/refs/heads/master/" + "license.txt",
rawUrl + $"/refs/heads/master/" + "LICENSE",
rawUrl + $"/refs/heads/master/" + "LICENSE.md",
rawUrl + $"/refs/heads/main/" + "LICENSE.txt",
rawUrl + $"/refs/heads/main/" + "license.txt",
rawUrl + $"/refs/heads/main/" + "LICENSE",
rawUrl + $"/refs/heads/main/" + "LICENSE.md",
];
var license = string.Empty;
foreach (var target in targets)
{
Console.WriteLine($"Downloading license for {id}: {target}");
try
{
license = await fetchText(target);
}
catch { }
if (license.Length > 0)
break;
}
if (string.IsNullOrWhiteSpace(license))
throw new Exception($"Can not download license for {id}: {projectUrl}");
await File.AppendAllTextAsync("NOTICE.txt",
$"""
License notice for {id} (v{version})
------------------------------------
{license}
""", ctx);
}
try
{
await "git status --porcelain";
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine("The build left unchecked artifacts in the source folder");
await "git diff NOTICE.txt";
return 1;
}
return 0;
});
await app.RunAsync(args);