-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathProgram.cs
203 lines (182 loc) · 5.64 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
using Mono.Options;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
const string AppName = "export-clearlydefined";
bool showHelp = false;
int verbosity = 0;
string? curatedDataPath = null;
string? manifestPath = null;
var groupIdPrefixes = new HashSet<string> {
"com.android",
"com.google",
"org.chromium",
"org.jetbrains",
"org.tensorflow",
"org.ow2.asm",
};
var mavenGoogleGroupIdPrefixes = new HashSet<string> {
"com.android",
"org.chromium",
"com.google",
};
var excludeGroupIdPrefixes = new HashSet<string> {
"androidx",
};
var options = new OptionSet {
$"Usage: {AppName} [OPTIONS]+",
"",
"Update clearlydefined/curated-data to contain bound packages and versions.",
"",
"Available Options:",
{ "clear-builtin-group-ids",
$"Do not process the group id prefixes:\n{string.Join (", ", groupIdPrefixes)}",
v => groupIdPrefixes.Clear () },
{ "g|group-id=",
"Process only groups starting with {GROUP_ID}",
v => groupIdPrefixes.Add (v) },
{ "m|manifest=",
"{FILE} path to cgmanifest.json to process",
v => manifestPath = v },
{ "o=",
"clearlydefined/curated-data checkout {DIRECTORY}",
v => curatedDataPath = v },
{ "v|verbose:",
"Increase verbosity of messages, or set verbosity to {LEVEL}",
(int? v) => verbosity = v.HasValue ? v.Value : verbosity + 1 },
{ "h|?|help",
"Show this help message and exit",
v => showHelp = v != null },
};
try {
options.Parse (args);
if (showHelp) {
options.WriteOptionDescriptions (Console.Out);
return;
}
if (ErrorMissingOption ("o", curatedDataPath) ||
ErrorMissingOption ("manifest", manifestPath)) {
return;
}
Directory.CreateDirectory (curatedDataPath);
using var manifest = ReadJsonFile (manifestPath);
foreach (var registration in manifest.RootElement.GetProperty ("registrations").EnumerateArray ()) {
if (!registration.TryGetProperty ("component", out var component) ||
!component.TryGetProperty ("type", out var type) || type.GetString () != "maven" ||
!component.TryGetProperty ("maven", out var maven)) {
continue;
}
var groupId = maven.GetProperty ("groupId").GetString ();
if (ShouldSkipGroupId (groupId)) {
continue;
}
var artifactId = maven.GetProperty ("artifactId").GetString ();
var version = maven.GetProperty ("version").GetString ();
if (string.IsNullOrWhiteSpace (artifactId) || string.IsNullOrWhiteSpace (version)) {
continue;
}
var license = registration.TryGetProperty ("license", out var licenseProp)
? licenseProp.GetString ()
: null;
AddOtherLicense (groupId, artifactId, version, GetLicenseId (license));
}
}
catch (OptionException e) {
Environment.ExitCode = 1;
Console.Error.WriteLine ($"{AppName}: {(verbosity > 0 ? e.ToString () : e.Message)}");
Console.Error.WriteLine ($"Try `{AppName} --help` for more information.");
return;
}
static bool ErrorMissingOption (string option, [NotNullWhen (false)] string? value)
{
if (!string.IsNullOrWhiteSpace (value)) {
return false;
}
Environment.ExitCode = 1;
Console.Error.WriteLine ($"{AppName}: Missing required option '{option}'.");
Console.Error.WriteLine ($"Try `{AppName} --help` for more information.");
return true;
}
static JsonDocument ReadJsonFile (string path)
{
var options = new JsonDocumentOptions {
};
using var file = File.OpenRead (path);
return JsonDocument.Parse (file, options);
}
bool ShouldSkipGroupId ([NotNullWhen (false)] string? groupId)
{
if (string.IsNullOrWhiteSpace (groupId)) {
return true;
}
foreach (var prefix in excludeGroupIdPrefixes) {
if (groupId.StartsWith (prefix, StringComparison.OrdinalIgnoreCase)) {
return true;
}
}
foreach (var prefix in groupIdPrefixes) {
if (groupId.StartsWith (prefix, StringComparison.OrdinalIgnoreCase)) {
return false;
}
}
return groupIdPrefixes.Count > 0;
}
string GetLicenseId (string? license)
{
return license?.ToLowerInvariant () switch {
"the apache software license, version 2.0"
=> "Apache-2.0",
"chromium and built-in dependencies"
=> "BSD-3-Clause",
_ => "OTHER",
};
}
void AddOtherLicense (string groupId, string artifactId, string version, string licenseId)
{
var provider = GetMavenDirectoryPart (groupId);
var dir = Path.Combine (curatedDataPath, "curations", "maven", provider, groupId);
Directory.CreateDirectory (dir);
var path = Path.Combine (dir, $"{artifactId}.yaml");
var newPath = !File.Exists (path);
if (!newPath && YamlHasVersion (path, version)) {
return;
}
using var o = File.AppendText (path);
if (newPath) {
o.WriteLine ($"coordinates:");
o.WriteLine ($" name: {artifactId}");
o.WriteLine ($" namespace: {groupId}");
o.WriteLine ($" provider: {provider}");
o.WriteLine ($" type: maven");
o.WriteLine ($"revisions:");
}
o.WriteLine ($" {version}:");
o.WriteLine ($" licensed:");
o.WriteLine ($" declared: {licenseId}");
}
string GetMavenDirectoryPart (string groupId)
{
foreach (var prefix in mavenGoogleGroupIdPrefixes) {
if (groupId.StartsWith (prefix, StringComparison.OrdinalIgnoreCase)) {
return "mavengoogle";
}
}
return "mavencentral";
}
static bool YamlHasVersion (string path, string version)
{
var expectedVersion = $" {version}:";
bool sawRevisions = false;
foreach (var line in File.ReadLines (path)) {
if (line == "revisions:") {
sawRevisions = true;
continue;
}
if (!sawRevisions) {
continue;
}
if (line == expectedVersion) {
return true;
}
}
return false;
}