Skip to content

Visual Studio Code, rebuilt in Rust

Rust
3.0 KB
Raw Blame
import { createHash } from 'node:crypto';
import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, statSync } from 'node:fs';
import { join } from 'node:path';

// Bump when ui-pair drives, shoots or sets up the reference differently: cached reference outputs are then retaken.
const referenceDrivingVersion = 2;
const cacheRoot = join(process.env.HOME!, 'Library/Caches/vsrustium-ui-pair-ref');

/** A hash of what the reference's outputs depend on: steps, theme, run env, opened folder, reference build. Not the port. */
export function referenceCacheKey(root: string, stepsFile: string, theme: string, env: Record<string, string | undefined>, ref: string) {
	const folder = env.FOLDER ?? join(root, 'port/fixtures/workspace');
	const electron = join(ref, '.build/electron/Code - OSS.app/Contents/MacOS/Code - OSS');
	const git = (args: string[]) => Bun.spawnSync(['git', '-C', ref, ...args]).stdout.toString();
	const parts = [
		referenceDrivingVersion,
		fileHash(stepsFile),
		theme,
		...['FOLDER', 'FILE', 'ARGS', 'SETTINGS'].map(name => env[name] ?? null),
		env.USER_TASKS ? fileHash(env.USER_TASKS) : null,
		fileHash(join(root, 'port/tools/ref-user-data.ts')),
		folder === '' ? '' : folderHash(folder),
		statSync(electron).size,
		...['.build/electron/version', 'product.json', 'package.json'].map(file => fileHash(join(ref, file))),
		git(['rev-parse', 'HEAD']),
		git(['status', '--porcelain']),
	];
	return createHash('sha256').update(JSON.stringify(parts)).digest('hex').slice(0, 32);
}

/** Copies the cached reference outputs into `outDir`; false when there are none. */
export function restoreReference(key: string, outDir: string) {
	const entry = join(cacheRoot, key);
	if (!existsSync(entry)) {
		return false;
	}
	cpSync(entry, outDir, { recursive: true });
	return true;
}

/** Stores the reference-* files written to `outDir` since `since`; the entry appears whole, by rename. */
export function storeReference(key: string, outDir: string, since: number) {
	mkdirSync(cacheRoot, { recursive: true });
	const temp = mkdtempSync(join(cacheRoot, '.tmp-'));
	for (const name of readdirSync(outDir)) {
		if (name.startsWith('reference-') && statSync(join(outDir, name)).mtimeMs >= since) {
			cpSync(join(outDir, name), join(temp, name));
		}
	}
	const entry = join(cacheRoot, key);
	if (existsSync(entry)) {
		renameSync(entry, `${temp}.old`);
	}
	try {
		renameSync(temp, entry);
	} catch {
		// Another session stored this key between the two renames: its entry stays.
	}
	rmSync(temp, { recursive: true, force: true });
	rmSync(`${temp}.old`, { recursive: true, force: true });
}

function fileHash(file: string) {
	return createHash('sha256').update(readFileSync(file)).digest('hex');
}

function folderHash(folder: string) {
	const hash = createHash('sha256');
	for (const name of readdirSync(folder, { recursive: true, encoding: 'utf8' }).sort()) {
		const path = join(folder, name);
		hash.update(name).update(statSync(path).isFile() ? readFileSync(path) : 'dir');
	}
	return hash.digest('hex');
}