magi-code 0.96.2

Repository-aware CLI coding agent for terminal work
Documentation
use std::{env, fs, path::PathBuf};

fn main() {
    let current = semver::Version::parse(&env::var("CARGO_PKG_VERSION").unwrap()).unwrap();
    let directory = PathBuf::from("docs/features/release-notes");
    println!("cargo:rerun-if-changed={}", directory.display());
    let mut releases = Vec::new();
    for entry in fs::read_dir(directory).expect("read release notes directory") {
        let path = entry.expect("read release notes entry").path();
        if path.extension().is_none_or(|extension| extension != "md") {
            continue;
        }
        let Some(version) = path
            .file_stem()
            .and_then(|name| name.to_str())
            .and_then(|name| semver::Version::parse(name).ok())
        else {
            continue;
        };
        if version > current {
            continue;
        }
        let notes = fs::read_to_string(&path)
            .expect("read approved release notes")
            .replace("\r\n", "\n");
        assert!(notes.len() <= 16 * 1024, "release notes exceed 16 KiB");
        assert!(
            !notes.chars().any(|c| c.is_control() && c != '\n'),
            "release notes contain control characters"
        );
        if !notes.trim().is_empty() {
            releases.push((version, notes));
        }
    }
    releases.sort_by(|a, b| a.0.cmp(&b.0));
    let mut text = String::new();
    let mut index = String::from("&[");
    for (version, notes) in releases {
        let start = text.len();
        text.push_str(&format!("## Version {version}\n\n{notes}\n\n"));
        index.push_str(&format!(
            "({:?}, {start}, {}),",
            version.to_string(),
            text.len()
        ));
    }
    assert!(
        text.len() <= 1024 * 1024,
        "bundled release notes exceed 1 MiB"
    );
    index.push(']');
    let output = PathBuf::from(env::var_os("OUT_DIR").unwrap());
    fs::write(output.join("welcome-release-notes.txt"), text).expect("write bundled notes");
    fs::write(output.join("welcome-release-notes-index.rs"), index).expect("write notes index");
}