Visual Studio Code, rebuilt in Rust
Rust
#!/usr/bin/env bun
// Packages the Linux build as VS Code's Linux archive lays it out: VSRustium-linux-<arch>/ with the release binary
// (named applicationName, as code.sh starts it) and bun, bin/<applicationName> from upstream's linux code.sh, and the
// app files in resources/app (port/tools/app-files.ts; product.rs BUNDLED_APP). The packages' native modules, built
// for macOS by the reference's pnpm install, are rebuilt for Linux with node-gyp. Then it writes
// VSRustium-<display version>-linux-<arch>.tar.gz and the folder as VSRustium-<display version>-linux-<arch>.AppImage
// (port/tools/linux/appimage) next to the folder, and the feed entries of port/notes.md "Update feed":
// linux-<arch>-prerelease.json and, unless PRERELEASE=1, linux-<arch>.json.
// Run on Linux after `cargo gc build --release -p app` and port/tools/build-exthost, for the host's arch:
// port/tools/linux-build 'port/tools/package-linux' env VSCODE_GPUI_EXTENSIONS (default the VS Code tree's
// extensions), OUT (default <target dir>/VSRustium-linux-<arch>), VERSION, PRERELEASE=1, TAG, PRODUCT as package-app.
import { cpSync, existsSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
import { basename, dirname, join, relative } from 'node:path';
import { buildProduct, copyAppFiles, displayVersion, rebuildNativeModule, writeFeedEntries } from './app-files.ts';
import { vscodeSrc } from './vscode-src.ts';
if (process.platform !== 'linux') {
throw new Error('package-linux runs on Linux (port/tools/linux-build); macOS builds are port/tools/package-app');
}
const platform = 'linux';
const arch = process.arch === 'arm64' ? 'arm64' : 'x64';
// `file -b` of an ELF for this arch, e.g. "ELF 64-bit LSB shared object, ARM aarch64, ...".
const elfMachine = arch === 'arm64' ? 'ARM aarch64' : 'x86-64';
const targetDir = process.env.CARGO_TARGET_DIR ?? join(process.cwd(), 'native/target');
const extensionsDir = process.env.VSCODE_GPUI_EXTENSIONS ?? join(vscodeSrc, 'extensions');
const out = process.env.OUT ?? join(targetDir, `VSRustium-${platform}-${arch}`);
const product = await buildProduct();
const name = product.applicationName;
rmSync(out, { recursive: true, force: true });
const app = join(out, 'resources/app');
mkdirSync(app, { recursive: true });
cpSync(join(targetDir, 'release/vsrustium'), join(out, name));
// profile.release `debug = "limited"` stays in the ELF on Linux (macOS splits it out): 571 MB with it, the symbols
// for backtraces stay.
await Bun.$`strip --strip-debug ${join(out, name)}`;
cpSync(realpathSync(Bun.which('bun')!), join(out, 'bun'));
// gulpfile.vscode-linux.ts: the CLI script, run by the binary as Electron (cli.rs takes the CLI part).
const shellCommand = readFileSync(join(vscodeSrc, 'resources/linux/bin/code.sh'), 'utf8').replaceAll('@@APPNAME@@', name).replaceAll('@@PRODNAME@@', product.nameLong);
mkdirSync(join(out, 'bin'));
writeFileSync(join(out, 'bin', name), shellCommand, { mode: 0o755 });
// resources/linux/code.desktop filled as gulpfile.vscode.linux.ts fills it for the deb, but Exec is the command on
// PATH (bin/<applicationName> linked there): an archive does not know where it is unpacked. The icon is
// port/AppIcon.png, ictool's 512 px render of port/AppIcon.icon.
const desktop = readFileSync(join(vscodeSrc, 'resources/linux/code.desktop'), 'utf8')
.replaceAll('@@NAME_LONG@@', product.nameLong)
.replaceAll('@@DESKTOP_NAME@@', product.linuxDesktopName)
.replaceAll('@@NAME@@', name)
.replaceAll('@@EXEC@@', name)
.replaceAll('@@ICON@@', product.linuxIconName);
writeFileSync(join(out, `${product.linuxDesktopName}.desktop`), desktop);
cpSync('port/AppIcon.png', join(out, `${product.linuxIconName}.png`));
writeFileSync(join(app, 'product.json'), JSON.stringify(product, null, '\t'));
const copiedEntries = await copyAppFiles(app, extensionsDir, platform, arch);
// Every native binary must be an ELF of this arch: a .node of another system or arch is rebuilt from its source, any
// other binary fails the package.
const wrong = [];
for (const file of (await Bun.$`find ${out} -type f ${['(', '-name', '*.node', '-o', '-name', '*.so', '-o', '-name', '*.dylib', '-o', '-perm', '-u+x', ')']}`.text()).split('\n').filter(Boolean)) {
const type = (await Bun.$`file -b ${file}`.text()).trim();
const native = type.startsWith('ELF') || type.startsWith('Mach-O');
if (!native || (type.startsWith('ELF') && type.includes(elfMachine))) {
continue;
}
if (file.endsWith('.node')) {
await rebuildNativeModule(file, app, copiedEntries, []);
if (!existsSync(file) || (await Bun.$`file -b ${file}`.text()).includes(elfMachine)) {
continue;
}
}
wrong.push(`${relative(out, file)} (${type.split(',').slice(0, 2).join(',')})`);
}
if (wrong.length) {
throw new Error(`not a ${platform}-${arch} binary: ${wrong.join(', ')}`);
}
const archive = join(dirname(out), `VSRustium-${displayVersion(product)}-${platform}-${arch}.tar.gz`);
rmSync(archive, { force: true });
await Bun.$`tar -czf ${archive} -C ${dirname(out)} ${basename(out)}`;
await writeFeedEntries(archive, product, platform, arch);
const size = (await Bun.$`du -sh ${out}`.text()).split('\t')[0];
const appImage = join(dirname(out), `VSRustium-${displayVersion(product)}-${platform}-${arch}.AppImage`);
await Bun.$`port/tools/linux/appimage ${out} ${name} ${appImage}`;
const archiveSize = (await Bun.$`du -sh ${archive}`.text()).split('\t')[0];
const appImageSize = (await Bun.$`du -sh ${appImage}`.text()).split('\t')[0];
console.log(`${out}: ${size}, ${basename(archive)}: ${archiveSize}, ${basename(appImage)}: ${appImageSize}`);