-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathrust-scripts.mjs
More file actions
624 lines (497 loc) · 17.1 KB
/
Copy pathrust-scripts.mjs
File metadata and controls
624 lines (497 loc) · 17.1 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
import * as process from 'node:process';
import * as fs from 'node:fs';
import { execSync } from 'node:child_process';
import fetch from 'node-fetch';
import pkg from './package.json' with { type: 'json' };
const GH_OWNER = 'versatica';
const GH_REPO = 'mediasoup';
// Main Git branch is 'v' concatenated with the major SEMVER number of the
// "version" field in package.json.
const MAIN_BRANCH = `v${pkg.version.split('.')[0]}`;
// The three publishable mediasoup Rust crates. For each one: `manifest` is the
// Cargo.toml that holds its `[package].version`, and `dependents` lists the
// workspace manifests that declare it as a dependency (whose `version`
// requirement is bumped to the released version too). The `mediasoup` crate is
// special: releasing it creates a `rust-X.Y.Z` Git tag and a GitHub release
// built from its CHANGELOG; the other two are published without a tag or a
// GitHub release (the `mediasoup-crate-publish.yaml` workflow detects them from
// the commit message).
const CRATES = [
{ name: 'mediasoup', manifest: 'rust/Cargo.toml' },
{
name: 'mediasoup-sys',
manifest: 'worker/Cargo.toml',
dependents: ['rust/Cargo.toml'],
},
{
name: 'mediasoup-types',
manifest: 'rust/types/Cargo.toml',
dependents: ['rust/Cargo.toml'],
},
];
// The `mediasoup` crate, whose rust/CHANGELOG.md drives the GitHub release body.
const MEDIASOUP_CRATE = CRATES[0];
const MEDIASOUP_CRATE_CHANGELOG = 'rust/CHANGELOG.md';
const task = process.argv[2];
const taskArgs = process.argv.slice(3).join(' ');
void run();
async function run() {
logInfo(taskArgs ? `[args:"${taskArgs}"]` : '');
switch (task) {
case 'release:check': {
await checkRelease();
break;
}
case 'release': {
await release({ args: taskArgs });
break;
}
default: {
logError('unknown task');
exitWithError();
}
}
}
function lint() {
logInfo('lint()');
executeCmd('cargo fmt --all -- --check');
executeCmd('cargo clippy --all-targets -- -D warnings');
}
function test() {
logInfo('test()');
executeCmd('cargo test --verbose');
executeCmd('cargo test --release --verbose');
}
function doc() {
logInfo('doc()');
// Fail on broken/private intra-doc links, same as when building the docs for
// docs.rs.
process.env.DOCS_RS = '1';
process.env.RUSTDOCFLAGS =
'-D rustdoc::broken-intra-doc-links -D rustdoc::private_intra_doc_links';
executeCmd('cargo doc --locked --all --no-deps --lib');
}
/**
* Validates packaging of all mediasoup Rust crates without uploading anything.
* They are passed as a single group so cargo resolves the dependencies among
* them against the locally packaged copies instead of crates.io (works even if
* the new versions are not published yet).
*/
function publishDryRun() {
logInfo('publishDryRun()');
executeCmd(
'cargo publish --dry-run -p mediasoup-types -p mediasoup-sys -p mediasoup'
);
}
async function checkRelease(crate = MEDIASOUP_CRATE) {
logInfo(`checkRelease() [crate:${crate.name}]`);
const { version } = readCrate(crate.manifest);
// rust/CHANGELOG.md tracks only the `mediasoup` crate, so the CHANGELOG entry
// is verified (and grabbed) only when releasing that crate, before the slow
// build steps.
let versionChanges;
if (crate.name === 'mediasoup') {
try {
versionChanges = await getVersionChanges(version);
} catch (error) {
logError(`checkRelease() | ${error.message}`);
exitWithError();
}
}
// Ensure Cargo.lock is in sync before running any cargo command that could
// silently regenerate it.
checkCargoLock();
lint();
test();
doc();
// Validate packaging only when the crate version is not yet on crates.io (i.e.
// it has been bumped and is about to be published). Otherwise Cargo resolves
// the dependencies among the three crates against the already-published copies
// on crates.io and any schema/API change made since the last release fails
// verification spuriously even though nothing is being published (same gating
// as in `mediasoup-rust.yaml`).
let published;
try {
published = await isCratePublished(crate.name, version);
} catch (error) {
logError(`checkRelease() | ${error.message}`);
exitWithError();
}
if (published) {
logInfo(
`checkRelease() | ${crate.name} ${version} is already published on crates.io, skipping publish dry-run`
);
} else {
publishDryRun();
}
return { versionChanges };
}
async function release({ args = '' } = {}) {
logInfo('release()');
const [name, version] = args.trim().split(/\s+/);
const crate = CRATES.find(entry => entry.name === name);
if (!crate) {
logError(
`release() | first argument must be a crate name (one of: ${CRATES.map(
entry => `'${entry.name}'`
).join(', ')}), but got '${name}'`
);
exitWithError();
}
if (!version || !/^\d+\.\d+\.\d+$/.test(version)) {
logError(
`release() | a SEMVER 'x.y.z' version is required as second argument, but got '${version}'`
);
exitWithError();
}
// Must be on the main branch.
const branch = execSync('git rev-parse --abbrev-ref HEAD', {
encoding: 'utf-8',
}).trim();
if (branch !== MAIN_BRANCH) {
logError(
`release() | must be on '${MAIN_BRANCH}' branch, but it is on '${branch}' branch`
);
exitWithError();
}
// Clean working tree required before bumping the version.
checkGitClean();
// Lint, test, doc, publish dry-run, and (for the `mediasoup` crate) verify the
// CHANGELOG entry. Runs before the bump (the checked version is the previous
// one still in the manifest, which is harmless).
await checkRelease(crate);
// Bump the crate version in its Cargo.toml and reflect it in the (workspace)
// root Cargo.lock.
bumpCargoVersion(crate.manifest, version);
// Keep every workspace manifest that depends on this crate pointing at the
// just-released version (the `mediasoup` crate's `version` requirement on
// `mediasoup-sys` / `mediasoup-types` in rust/Cargo.toml). These are
// `path` + `version` dependencies, so the requirement must keep accepting the
// sibling's actual version (otherwise a breaking bump would fail the
// `cargo metadata` in syncCargoLock()); bumping it here also makes the next
// `mediasoup` release depend on the new version, and it is committed together
// with this release commit.
for (const dependent of crate.dependents ?? []) {
updateDependencyVersion(dependent, crate.name, version);
}
syncCargoLock();
if (crate.name === 'mediasoup') {
// The `mediasoup` crate also bumps rust/CHANGELOG.md and is released via a
// `rust-X.Y.Z` Git tag, whose push triggers `mediasoup-crate-publish.yaml`
// to create the GitHub release and publish the crate to crates.io. On its
// success `mediasoup-website-update.yaml updates the website.
await updateChangelog(version);
const tag = `rust-${version}`;
// Commit the bump, tag it, and push both.
//
// The commit message carries a "[no-ci]" marker so the regular branch CI
// workflows (node, worker, rust, fuzzer, codeql) skip this commit: it only
// bumps version/CHANGELOG (no code change) and its parent already passed CI,
// and the release is driven by the tag-triggered workflows instead.
//
// NOTE: "[no-ci]" (with a hyphen) is a custom marker, NOT GitHub's native
// "[skip ci]"/"[no ci]" (which would also skip mediasoup-crate-publish,
// since the tag push shares this same commit).
executeCmd(`git commit -am 'release ${tag} [no-ci]'`);
executeCmd(`git tag -a ${tag} -m '${tag}'`);
executeCmd(`git push origin ${MAIN_BRANCH}`);
executeCmd(`git push origin '${tag}'`);
} else {
// The `mediasoup-sys` / `mediasoup-types` crates are released without a Git
// tag or GitHub release. The commit message
// `<crate> <version> [crate-publish] [no-ci]` is the signal that
// `mediasoup-crate-publish.yaml` parses on the branch push: the
// "[crate-publish]" marker opts the commit into the workflow, and the crate
// name and version are read from the start of the message.
//
// "[no-ci]" keeps the regular branch CI workflows (node, worker, rust,
// fuzzer, codeql) from running on this version-only commit.
executeCmd(
`git commit -am '${crate.name} ${version} [crate-publish] [no-ci]'`
);
executeCmd(`git push origin ${MAIN_BRANCH}`);
}
}
function checkGitClean() {
logInfo('checkGitClean()');
const status = execSync('git status --porcelain', {
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'ignore'],
});
if (status.trim()) {
logError(
'checkGitClean() | Git working tree is not clean, commit or stash your changes first'
);
exitWithError();
}
}
function checkCargoLock() {
logInfo('checkCargoLock()');
// `--locked` makes cargo fail if Cargo.lock is out of date instead of
// regenerating it. We resolve metadata (no build) just to assert the lock is
// in sync, otherwise run `cargo build` and commit Cargo.lock first.
try {
execSync('cargo metadata --locked --format-version 1', {
stdio: ['ignore', 'ignore', process.stderr],
});
} catch (error) {
logError(
'checkCargoLock() | Cargo.lock is out of date, run `cargo build` and commit it'
);
exitWithError();
}
}
function syncCargoLock() {
logInfo('syncCargoLock()');
// Reflect the just-bumped crate version in the (workspace) root Cargo.lock.
// `cargo metadata` resolves the workspace and rewrites Cargo.lock, changing
// only the bumped member version while keeping every other pinned dependency
// intact (checkCargoLock() asserted the lock was in sync beforehand).
try {
execSync('cargo metadata --format-version 1', {
stdio: ['ignore', 'ignore', process.stderr],
});
} catch (error) {
logError(`syncCargoLock() failed, exiting: ${error}`);
exitWithError();
}
}
async function getVersionChanges(version) {
logInfo(`getVersionChanges() [version:${version}]`);
// NOTE: Load dep on demand since it's a devDependency.
const marked = await import('marked');
const changelog = fs.readFileSync(MEDIASOUP_CRATE_CHANGELOG, {
encoding: 'utf-8',
});
const entries = marked.lexer(changelog);
for (let idx = 0; idx < entries.length; ++idx) {
const entry = entries[idx];
if (entry.type === 'heading' && entry.text === version) {
// Collect every token after the matching heading until the next heading.
// NOTE: We cannot just use `entries[idx + 1].raw` because `marked`
// inserts a `space` token between the heading and its content.
let changes = '';
for (let next = idx + 1; next < entries.length; ++next) {
if (entries[next].type === 'heading') {
break;
}
changes += entries[next].raw;
}
changes = changes.trim();
if (changes) {
return changes;
}
break;
}
}
// This should not happen (unless author forgot to update the CHANGELOG).
throw new Error(
`no entry found in ${MEDIASOUP_CRATE_CHANGELOG} for version '${version}'`
);
}
async function updateChangelog(version) {
logInfo(`updateChangelog() [version:${version}]`);
// NOTE: Load dep on demand since it's a devDependency.
const marked = await import('marked');
const changelog = fs.readFileSync(MEDIASOUP_CRATE_CHANGELOG, {
encoding: 'utf-8',
});
const tokens = marked.lexer(changelog);
// Locate the top "### NEXT" heading.
const nextHeading = tokens.find(
token =>
token.type === 'heading' && token.depth === 3 && token.text === 'NEXT'
);
if (!nextHeading) {
throw new Error(
`no '### NEXT' heading found in ${MEDIASOUP_CRATE_CHANGELOG}`
);
}
// Insert "### <version>" right below "### NEXT" (keeping the empty "### NEXT"
// for future unreleased changes), preserving the heading's trailing newlines.
const updatedChangelog = changelog.replace(
nextHeading.raw,
`### NEXT\n\n### ${version}${nextHeading.raw.slice('### NEXT'.length)}`
);
fs.writeFileSync(MEDIASOUP_CRATE_CHANGELOG, updatedChangelog);
}
/**
* Rewrites the `version` key of the `[package]` section of the given Cargo.toml,
* leaving the `version` keys under `[dependencies.*]` sections untouched.
*/
function bumpCargoVersion(manifestPath, version) {
logInfo(`bumpCargoVersion() [manifest:${manifestPath}, version:${version}]`);
const content = fs.readFileSync(manifestPath, { encoding: 'utf-8' });
const lines = content.split('\n');
let inPackageSection = false;
let bumped = false;
for (let idx = 0; idx < lines.length; ++idx) {
const trimmed = lines[idx].trim();
if (trimmed.startsWith('[')) {
inPackageSection = trimmed === '[package]';
continue;
}
if (inPackageSection && /^version\s*=\s*"[^"]+"/.test(trimmed)) {
lines[idx] = lines[idx].replace(
/version\s*=\s*"[^"]+"/,
`version = "${version}"`
);
bumped = true;
break;
}
}
if (!bumped) {
logError(
`bumpCargoVersion() | no 'version' key found in [package] section of '${manifestPath}'`
);
exitWithError();
}
fs.writeFileSync(manifestPath, lines.join('\n'));
}
/**
* Rewrites the `version` key of the `[dependencies.<depName>]` table of the
* given Cargo.toml (the table form used across the workspace, e.g.
* `[dependencies.mediasoup-sys]`), leaving its `path` and any other keys
* untouched. Used to keep the `mediasoup` crate's requirement on a sibling crate
* in sync when that sibling is released.
*/
function updateDependencyVersion(manifestPath, depName, version) {
logInfo(
`updateDependencyVersion() [manifest:${manifestPath}, dep:${depName}, version:${version}]`
);
const content = fs.readFileSync(manifestPath, { encoding: 'utf-8' });
const lines = content.split('\n');
const sectionHeader = `[dependencies.${depName}]`;
let inDepSection = false;
let bumped = false;
for (let idx = 0; idx < lines.length; ++idx) {
const trimmed = lines[idx].trim();
if (trimmed.startsWith('[')) {
inDepSection = trimmed === sectionHeader;
continue;
}
if (inDepSection && /^version\s*=\s*"[^"]+"/.test(trimmed)) {
lines[idx] = lines[idx].replace(
/version\s*=\s*"[^"]+"/,
`version = "${version}"`
);
bumped = true;
break;
}
}
if (!bumped) {
logError(
`updateDependencyVersion() | no 'version' key found in '${sectionHeader}' section of '${manifestPath}'`
);
exitWithError();
}
fs.writeFileSync(manifestPath, lines.join('\n'));
}
function readCrate(manifestPath) {
const content = fs.readFileSync(manifestPath, { encoding: 'utf-8' });
return parseManifest(content);
}
/**
* Extracts `name` and `version` from the `[package]` section of a Cargo.toml,
* ignoring the `version` keys under `[dependencies.*]` sections.
*/
function parseManifest(content) {
let inPackageSection = false;
let name;
let version;
for (const line of content.split('\n')) {
const trimmed = line.trim();
// A new section starts. We only care about `[package]`.
if (trimmed.startsWith('[')) {
inPackageSection = trimmed === '[package]';
continue;
}
if (!inPackageSection) {
continue;
}
const nameMatch = trimmed.match(/^name\s*=\s*"([^"]+)"/);
if (nameMatch) {
name = nameMatch[1];
continue;
}
const versionMatch = trimmed.match(/^version\s*=\s*"([^"]+)"/);
if (versionMatch) {
version = versionMatch[1];
}
}
if (!name || !version) {
throw new Error(
"failed to parse 'name'/'version' from Cargo.toml [package] section"
);
}
return { name, version };
}
/**
* Tells whether the given crate version is already published on crates.io by
* querying its API. Throws if crates.io cannot be reached or replies with an
* unexpected status (so we never publish or skip based on a wrong guess).
*/
async function isCratePublished(name, version) {
const url = `https://crates.io/api/v1/crates/${name}/${version}`;
let res;
try {
res = await fetch(url, {
// NOTE: crates.io requires a meaningful User-Agent.
headers: {
'User-Agent': `${GH_OWNER}/${GH_REPO} (https://github.com/${GH_OWNER}/${GH_REPO})`,
},
});
} catch (error) {
// eslint-disable-next-line preserve-caught-error
throw new Error(
`failed to reach crates.io for '${name}@${version}': ${error}`
);
}
// 200 means the version exists, 404 means it does not.
if (res.status === 200) {
return true;
} else if (res.status === 404) {
return false;
}
throw new Error(
`unexpected crates.io response for '${name}@${version}': ${res.status} ${res.statusText}`
);
}
function executeCmd(command, { cwd } = {}) {
logInfo(`executeCmd(): ${command}${cwd ? ` [cwd:${cwd}]` : ''}`);
try {
execSync(command, {
cwd,
stdio: ['ignore', process.stdout, process.stderr],
});
} catch (error) {
logError(`executeCmd() failed, exiting: ${error}`);
exitWithError();
}
}
// eslint-disable-next-line no-unused-vars
function executeInteractiveCmd(command, { cwd } = {}) {
logInfo(`executeInteractiveCmd(): ${command}${cwd ? ` [cwd:${cwd}]` : ''}`);
try {
execSync(command, { cwd, stdio: 'inherit', env: process.env });
} catch (error) {
logError(`executeInteractiveCmd() failed, exiting: ${error}`);
exitWithError();
}
}
function logInfo(...args) {
// eslint-disable-next-line no-console
console.log(`rust-scripts.mjs \x1b[36m[INFO] [${task}]\x1b[0m`, ...args);
}
// eslint-disable-next-line no-unused-vars
function logWarn(...args) {
// eslint-disable-next-line no-console
console.warn(`rust-scripts.mjs \x1b[33m[WARN] [${task}]\x1b[0m`, ...args);
}
function logError(...args) {
// eslint-disable-next-line no-console
console.error(`rust-scripts.mjs \x1b[31m[ERROR] [${task}]\x1b[0m`, ...args);
}
function exitWithError() {
process.exit(1);
}