-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-dependencies.ts
executable file
·314 lines (290 loc) · 9.38 KB
/
update-dependencies.ts
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
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env -S deno run --ext ts
// Copyright Sebastian Wiesner <sebastian@swsnr.de>
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
// Regenerate the manifest containing all maven dependencies, the hacky way.
//
// Maven doesn't seem to have any way to just dump all resolved dependencies in
// a structured data format, so we manually resolve things and then grep the
// log file for all downloaded artifacts.
import * as path from "jsr:@std/path@1";
import * as yaml from "jsr:@std/yaml@1";
import * as fs from "jsr:@std/fs@1";
type SourceType = "archive" | "file" | "patch";
interface FileSource {
readonly type: SourceType;
readonly path: string;
readonly url: undefined;
}
interface UrlSource {
readonly type: SourceType;
readonly url: string;
readonly sha512: string;
readonly dest?: string;
readonly "dest-filename"?: string;
}
interface GitSource {
readonly type: "git";
readonly url: string;
readonly tag: string;
readonly commit: string;
}
type Source = FileSource | UrlSource | GitSource;
/**
* Run a command and return its output.
*
* If the command returns a non-zero exit code, throw an error.
*
* @param command The command to run
* @param options Additional options for the command
* @returns The output of the command
*/
const checkedRun = async (
command: readonly string[],
options?: Deno.CommandOptions,
): Promise<Deno.CommandOutput> => {
const output = await new Deno.Command(command[0], {
args: command.slice(1),
...options,
}).output();
if (output.code !== 0) {
throw new Error(`Failed to run command ${command.join(" ")}`);
}
return output;
};
/**
* Determine whether the given flatpak module source refers to Git.
*
* @param source The source
* @returns Whether `source` is a git source
*/
const isGitSource = (source: string | Source): source is GitSource =>
typeof source !== "string" && source.type === "git";
interface Module {
readonly name: string;
readonly sources: (string | Source)[];
}
interface Manifest {
readonly modules: readonly Module[];
}
/**
* Get the main module source for Mediathekview from the manifest.
*
* Read the manifest and find the one module source pointing to the source
* tarball of Mediathekview.
*
* @param manifestDirectory The directory containing the manifest
* @returns The main source
*/
const getMainSource = async (manifestDirectory: string): Promise<GitSource> => {
const manifestPath = path.join(
manifestDirectory,
"de.mediathekview.MediathekView.yaml",
);
const manifest = yaml
.parse(await Deno.readTextFile(manifestPath)) as Manifest;
const module = manifest.modules.find((m) => m.name === "mediathekview");
if (!module) {
throw new Error("Failed to find mediathekview module");
}
const source = module.sources
.filter(isGitSource)
.find((source) => source.url.includes("mediathekview"));
if (!source) {
throw new Error("No archive source found for MediathekView");
}
return source;
};
/**
* Calculate the SHA512 digest of a file.
*
* @param file The file to compute the checksum of
* @returns The sha512 digest as hexadecimal string
*/
const sha512sum = async (file: string): Promise<string> => {
const digest = await crypto.subtle
.digest("SHA-512", await Deno.readFile(file));
return Array.from(new Uint8Array(digest))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
};
interface ArtifactURL {
/**
* The full URL of an artifact.
*/
readonly url: string;
/**
* The base url of the repository the artifact was downloaded from.
*/
readonly repoBaseUrl: string;
/**
* The URL path relative to `repoBaseUrl`.
*/
readonly relpath: string;
}
/**
* Known base URLs of Maven repositories.
*/
const REPO_BASES = [
"https://repo.maven.apache.org/maven2/",
"https://oss.sonatype.org/content/repositories/snapshots/",
"https://maven.ej-technologies.com/repository/",
];
/**
* Parse a URL point to an artifact in a maven repository.
*
* @param url The URL of an artifact.
* @returns The parsed URL.
*/
const parseUrl = (url: string): ArtifactURL => {
for (const base of REPO_BASES) {
if (url.startsWith(base)) {
return {
url,
repoBaseUrl: base,
relpath: url.slice(base.length),
};
}
}
throw new Error(`Failed to determine repository base of ${url}`);
};
/**
* Extracts all downloaded artifacts from a build log of a Maven build.
*
* @param mavenOutput The full output of a Maven build
* @returns A list of all artifacts that were downloaded during thebuild.
*/
const extractDownloadedArtifacts = (mavenOutput: string): ArtifactURL[] => {
const urls = [];
for (const line of mavenOutput.split("\n")) {
const match = line.match(/Downloaded from .*: (https?:\/\/[^ ]+)/);
if (!match) {
continue;
}
urls.push(parseUrl(match[1]));
}
return urls;
};
/**
* Create a flatpak source object from an artifact URL.
*
* Calculate the SHA 512 checksum of the artifact, and define a file structure
* to reconstruct its place in a maven repository.
*
* @param repoDirectory The repository directory
* @param url The artifact URL
* @returns A corresponding flatpak URL
*/
const artifactUrlToFlatpakSource = async (
repoDirectory: string,
url: ArtifactURL,
): Promise<UrlSource> => {
const dest = path.dirname(path.join(".m2/repository", url.relpath));
let destname = path.basename(url.relpath);
const dir = path.dirname(path.join(repoDirectory, url.relpath));
if (destname === "maven-metadata.xml") {
const candidates = fs.expandGlob(path.join(dir, "maven-metadata*.xml"));
for await (const entry of candidates) {
destname = entry.name;
}
}
const sha512 = await sha512sum(path.join(dir, destname));
return {
type: "file",
url: url.url,
dest,
"dest-filename": destname,
sha512,
};
};
/**
* Run a block with a temporary directory.
*
* Create a temporary directory, pass it to `block`, and then delete the
* temporary directory afterwards.
*
* @param block The block to run
* @returns The return value of `block`
*/
const withTempDir = async <T>(
block: (tempDir: string) => T | Promise<T>,
): Promise<T> => {
const tempDir = await Deno
.makeTempDir({ prefix: "mediathekview-dependencies-" });
try {
return await block(tempDir);
} finally {
try {
await fs.emptyDir(tempDir);
await Deno.remove(tempDir);
} catch (error) {
console.error("Failed to delete", tempDir, error);
}
}
};
const main = () =>
withTempDir(async (workingDirectory) => {
const manifestDirectory = import.meta.dirname;
if (!manifestDirectory) {
throw new Error("Not running as local module?");
}
const source = await getMainSource(manifestDirectory);
const repoDirectory = path.join(workingDirectory, "repo");
const sourceDirectory = path.join(workingDirectory, "mediathekview");
await Promise
.all([sourceDirectory, repoDirectory].map((d) => Deno.mkdir(d)));
await checkedRun(["git", "init"], { cwd: sourceDirectory });
await checkedRun([
"git",
"fetch",
"--depth=1",
source.url,
source.commit,
], { cwd: sourceDirectory });
await checkedRun(["git", "reset", "--hard", source.commit], {
cwd: sourceDirectory,
});
// TODO: Do we need to run a full "install"?
const output = (await checkedRun(
[
path.join(sourceDirectory, "mvnw"),
// Enable batch mode for non-interactive builds, which
// implicitly disables coloured output and thus makes parsing a
// lot easier.
"-B",
// Make maven use a fresh local repo in our temporary directory.
// This makes sure that we definitely download all dependencies.
`-Dmaven.repo.local=${repoDirectory}`,
"clean",
"install",
],
{
cwd: sourceDirectory,
stdout: "piped",
},
)).stdout;
const urls = extractDownloadedArtifacts(
new TextDecoder().decode(output),
);
const sources = await Promise
.all(urls.map((u) => artifactUrlToFlatpakSource(repoDirectory, u)));
const collator = Intl.Collator();
sources.sort((a, b) => collator.compare(a.url, b.url));
Deno.writeTextFile(
path.join(manifestDirectory, "maven-dependencies.json"),
JSON.stringify(sources, undefined, 2),
);
});
if (import.meta.main) {
main();
}