Skip to content

Visual Studio Code, rebuilt in Rust

Rust
7.2 KB
Raw Blame
#!/usr/bin/env bun
// Writes the coverage block of README.md (between the `coverage` markers) from the ledger's Units table
// and the milestone work rows of its Work table (`| M23 Linux | ...`), so open platform and uplift work counts too.
// Run after a ledger recount: bun port/tools/readme-coverage.ts
import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { readLedger } from './ledger.ts';
import { vscodeSrc } from './vscode-src.ts';

const root = join(import.meta.dir, '../..');
const version = JSON.parse(readFileSync(join(vscodeSrc, 'package.json'), 'utf8')).version;
const areaNames: Record<string, string> = {
	'workbench contribution': 'Workbench features: debug, Git, terminal, search, settings, extensions view',
	'src/vs/platform': 'Platform services: files, configuration, keybindings, windows, updates',
	'built-in extension': 'Built-in extensions: languages, themes, Git, Markdown, npm',
	'MainThread* shape (Rust side)': 'Extension API: the calls extensions make into the editor',
	'workbench service': 'Workbench services: editors, views, layout, themes, storage',
	'src/vs/editor/contrib': 'Editor features: find, folding, suggest, hover, multi-cursor',
	'workbench part': 'Window parts: title bar, activity bar, side bar, panel, status bar',
	'extension host seam': 'Extension host: runs unmodified VS Code extensions',
	'cross-cutting check': 'Whole-app checks: look, memory, no telemetry, Open VSX gallery',
	'src/vs/editor': 'Text editor core: model, view, cursor',
	'src/vs/base': 'Base library',
	'port feature': 'Port-only features: settings import from VS Code',
};
const empty = () => ({ verified: 0, ported: 0, stub: 0, todo: 0, removed: 0, 'n/a': 0 });
const areas = new Map<string, ReturnType<typeof empty>>();
const openRows: { id: string; gaps: number }[] = [];
const workNames: Record<string, string> = {
	'M21 uplift': 'the VS Code 1.148.0 uplift',
	'M23 Linux': 'Linux',
	'M24 Windows': 'Windows',
};
const ledger = readLedger(root);
const workItems = new Map<string, { done: number; open: number }>();
for (const line of [...ledger.openWork, ...ledger.doneWork]) {
	const cells = line.split(' | ');
	if (!/^\| M\d+ /.test(line) || cells.length < 5) {
		continue;
	}
	const milestone = cells[0].slice(2);
	if (!(milestone in workNames)) {
		throw new Error(`readme-coverage.ts has no plain name for the ledger work rows "${milestone}"`);
	}
	const counts = workItems.get(milestone) ?? { done: 0, open: 0 };
	counts[cells[2] === 'done' ? 'done' : 'open']++;
	workItems.set(milestone, counts);
}
let workDone = 0;
let workOpen = 0;
for (const counts of workItems.values()) {
	workDone += counts.done;
	workOpen += counts.open;
}
for (const line of ledger.units) {
	const [id, area, , status, gaps] = line.split(' | ');
	if (!(area in areaNames)) {
		throw new Error(`readme-coverage.ts has no plain name for the ledger area "${area}"`);
	}
	const counts = areas.get(area) ?? empty();
	counts[status as keyof typeof counts]++;
	areas.set(area, counts);
	if (status !== 'verified' && status !== 'removed' && status !== 'n/a') {
		openRows.push({ id: id.slice(2), gaps: gaps.match(/\[[^\]]+ [SML]\]/g)?.length ?? 0 });
	}
}

const total = empty();
for (const counts of areas.values()) {
	for (const status in counts) {
		total[status as keyof typeof total] += counts[status as keyof typeof counts];
	}
}

const inScope = (c: typeof total) => c.verified + c.ported + c.stub + c.todo;
const leftOut = (c: typeof total) => c.removed + c['n/a'];
const percent = (c: typeof total) => Math.floor((c.verified / inScope(c)) * 100);
const bar = (c: typeof total) => {
	if (inScope(c) === 0) {
		return 'all left out';
	}
	const filled = Math.floor(percent(c) / 5);
	return `\`${'█'.repeat(filled)}${'░'.repeat(20 - filled)}\` ${percent(c)}%`;
};

const itemsDone = total.verified + workDone;
const items = inScope(total) + workDone + workOpen;
const lines = [
	`**${itemsDone} of ${items} items done (${Math.floor((itemsDone / items) * 100)}%).** ${total.verified} of the ${inScope(total)} parts of VS Code ${version} the port covers match upstream 1:1. ${otherParts(inScope(total) - total.verified)}${openWork()}${leftOut(total)} more parts are left out on purpose. A part is one upstream folder: a feature, a service or a built-in extension; a work item is one row of a milestone's work (Linux, Windows, the uplift). Both are counted from the port's ledger, [port/ledger.md](port/ledger.md) and [port/ledger/](port/ledger).`,
	'',
	'```mermaid',
	`%%{init: {"theme": "base", "themeVariables": {"pie1": "#2da44e", "pie2": "#d4a72c", "pie3": "#cf222e", "pie4": "#8c959f", "pieStrokeColor": "#ffffff", "pieOuterStrokeColor": "#ffffff", "pieSectionTextColor": "#ffffff", "pieLegendTextColor": "#8c959f", "pieTitleTextColor": "#8c959f"}}}%%`,
	'pie showData',
	`    "Matches VS Code 1:1" : ${total.verified}`,
	`    "Works, with open gaps" : ${inScope(total) - total.verified}`,
	`    "Work items still open" : ${workOpen}`,
	`    "Left out on purpose" : ${leftOut(total)}`,
	'```',
	'',
	'<details><summary>By area</summary>',
	'',
	'| What | Matches 1:1 | With gaps | Left out | Done |',
	'|---|---:|---:|---:|---|',
];
for (const [area, c] of [...areas].sort((a, b) => inScope(b[1]) + leftOut(b[1]) - inScope(a[1]) - leftOut(a[1]))) {
	lines.push(`| ${areaNames[area]} | ${c.verified} | ${inScope(c) - c.verified} | ${leftOut(c)} | ${bar(c)} |`);
}
lines.push(`| **All** | **${total.verified}** | **${inScope(total) - total.verified}** | **${leftOut(total)}** | ${bar(total)} |`);
lines.push('', '"Matches 1:1" means upstream\'s tests or a named check against VS Code pass. ' + (openRows.length === 0 ? 'No part works with open gaps.' : 'Parts that work with open gaps, by gap count:'));
openRows.sort((a, b) => b.gaps - a.gaps || a.id.localeCompare(b.id));
if (openRows.length > 0) {
	lines.push('', openRows.map(row => `\`${row.id}\` ${row.gaps}`).join(', ') + '.');
}
lines.push('', '| Milestone work | Done | Open | Progress |', '|---|---:|---:|---|');
for (const [milestone, c] of [...workItems].sort((a, b) => b[1].open - a[1].open)) {
	lines.push(`| ${milestone} | ${c.done} | ${c.open} | ${workBar(c.done, c.done + c.open)} |`);
}
lines.push('', '</details>');

const readmePath = join(root, 'README.md');
const readme = readFileSync(readmePath, 'utf8');
const start = '<!-- coverage:start -->';
const end = '<!-- coverage:end -->';
if (!readme.includes(start) || !readme.includes(end)) {
	throw new Error(`README.md has no ${start} ... ${end} block`);
}
writeFileSync(readmePath, readme.slice(0, readme.indexOf(start) + start.length) + '\n' + lines.join('\n') + '\n' + readme.slice(readme.indexOf(end)));

function workBar(done: number, all: number) {
	const percent = Math.floor((done / all) * 100);
	const filled = Math.floor(percent / 5);
	return `\`${'█'.repeat(filled)}${'░'.repeat(20 - filled)}\` ${percent}%`;
}

function openWork() {
	if (workOpen === 0) {
		return '';
	}
	const parts = [...workItems].filter(([, c]) => c.open > 0).map(([milestone, c]) => `${c.open} for ${workNames[milestone]}`);
	return `${workOpen} work items are still open: ${parts.join(', ')}. `;
}

function otherParts(count: number) {
	if (count === 0) {
		return '';
	}
	return `${count === 1 ? 'The other one works' : `The other ${count} work`}, with open gaps listed below. `;
}