Visual Studio Code, rebuilt in Rust
Rust
//! The NLS index: every `localize`/`localize2` call of src/vs with literal key and message, as
//! `module\tkey\tmessage` lines (the message's escapes kept), for the display language (src/nls.rs).
//! The binary's own Info.plist, for a run outside VSRustium.app (`cargo run`), and the Dock icons.
use std::path::Path;
include!("../../vscode_src.rs");
fn main() {
let src = emit_vscode_src().join("src");
println!("cargo::rerun-if-changed={}", src.join("vs").display());
let call = regex::Regex::new(r#"\blocalize2?\(\s*(?:'([^']+)'|"([^"]+)"|\{\s*key:\s*['"]([^'"]+)['"](?:[^{}]|\{[^{}]*\})*\})\s*,\s*(?:"((?:[^"\\\n]|\\.)*)"|'((?:[^'\\\n]|\\.)*)'|`((?:[^`\\]|\\.)*)`)"#).unwrap();
let mut index = String::new();
let mut files = Vec::new();
collect_ts(&src.join("vs"), &mut files);
files.sort();
for file in files {
let module = file.strip_prefix(&src).unwrap().with_extension("").to_string_lossy().replace('\\', "/");
let Ok(text) = std::fs::read_to_string(&file) else { continue };
for captures in call.captures_iter(&text) {
let key = captures.get(1).or(captures.get(2)).or(captures.get(3)).unwrap().as_str();
let message = captures.get(4).or(captures.get(5)).or(captures.get(6)).unwrap().as_str();
if message.contains('\t') || message.contains('\n') {
continue;
}
index.push_str(&format!("{module}\t{key}\t{message}\n"));
}
}
std::fs::write(Path::new(&std::env::var("OUT_DIR").unwrap()).join("nls_index.tsv"), index).unwrap();
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "macos" {
embed_info_plist();
render_app_icons();
}
}
/// An unbundled executable takes CFBundleName from its `__TEXT,__info_plist` section: the app menu and the Dock
/// name the product instead of the executable.
fn embed_info_plist() {
let product = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../port/product.json");
println!("cargo::rerun-if-changed={}", product.display());
let product: serde_json::Value = serde_json::from_slice(&std::fs::read(&product).unwrap()).unwrap();
let plist = format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\"><dict><key>CFBundleName</key><string>{}</string><key>CFBundleDisplayName</key><string>{}</string></dict></plist>\n",
product["nameShort"].as_str().unwrap(),
product["nameLong"].as_str().unwrap()
);
let path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("Info.plist");
std::fs::write(&path, plist).unwrap();
println!("cargo::rustc-link-arg-bins=-Wl,-sectcreate,__TEXT,__info_plist,{}", path.display());
}
/// port/AppIcon.icon, port/AppIconBars.icon and port/AppIconBarsCream.icon rendered by Icon Composer's ictool, light (`Default`) and `Dark`,
/// as `<icon>-<rendition>.png`: the Dock icons app_icon.rs sets. 412 px in a 512 px canvas, Apple's 824/1024 icon grid.
fn render_app_icons() {
let developer = std::process::Command::new("xcode-select").arg("-p").output().expect("xcode-select runs (Xcode installed)");
let developer = String::from_utf8(developer.stdout).unwrap();
let ictool = Path::new(developer.trim()).parent().unwrap().join("Applications/Icon Composer.app/Contents/Executables/ictool");
let out = std::env::var("OUT_DIR").unwrap();
for name in ["AppIcon", "AppIconBars", "AppIconBarsCream"] {
let icon = Path::new(env!("CARGO_MANIFEST_DIR")).join(format!("../../../port/{name}.icon"));
println!("cargo::rerun-if-changed={}", icon.display());
for rendition in ["Default", "Dark"] {
let png = Path::new(&out).join(format!("{name}-{rendition}.png"));
let rendered = std::process::Command::new(&ictool)
.arg(&icon)
.args(["--export-image", "--platform", "macOS", "--rendition", rendition, "--width", "412", "--height", "412", "--scale", "1", "--output-file"])
.arg(&png)
.stdout(std::process::Stdio::null())
.status()
.expect("ictool runs (Xcode 26 or later)");
let padded = std::process::Command::new("sips").args(["-p", "512", "512"]).arg(&png).stdout(std::process::Stdio::null()).status().unwrap();
assert!(rendered.success() && padded.success(), "rendering {} ({rendition}) failed", icon.display());
}
}
}
/// The .ts files of `dir`, without tests and type declarations.
fn collect_ts(dir: &Path, files: &mut Vec<std::path::PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else { return };
for entry in entries.flatten() {
let path = entry.path();
let name = entry.file_name().to_string_lossy().into_owned();
if path.is_dir() {
if name != "test" {
collect_ts(&path, files);
}
} else if name.ends_with(".ts") && !name.ends_with(".d.ts") {
files.push(path);
}
}
}