aev-vm 0.2.0

Manage QEMU virtual machines in Rust and open their consoles in Aen
Documentation
//! Embed the unpublished Aen window SDK in the Cargo package.

use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

fn main() -> io::Result<()> {
    let source = Path::new("vendor/aen-window/src");
    println!("cargo:rerun-if-changed={}", source.display());
    let out = PathBuf::from(env::var_os("OUT_DIR").expect("Cargo provides OUT_DIR"));
    let module = out.join("aen-window");
    embed_sdk(source, &module)?;
    fs::write(
        out.join("aen-window.rs"),
        "#[allow(dead_code, unused_imports, clippy::all, clippy::pedantic)]\n\
         #[path = \"aen-window/lib.rs\"]\nmod aen_window;\n",
    )
}

fn embed_sdk(source: &Path, destination: &Path) -> io::Result<()> {
    fs::create_dir_all(destination)?;
    for entry in fs::read_dir(source)? {
        let entry = entry?;
        let target = destination.join(entry.file_name());
        if entry.file_type()?.is_dir() {
            embed_sdk(&entry.path(), &target)?;
        } else if entry.path().extension().is_some_and(|ext| ext == "rs") {
            // Relocate crate-root references into the embedded module. The
            // server is compiled for the private socket tests only.
            let code = fs::read_to_string(entry.path())?
                .replace("crate::", "crate::aen_window::")
                .replace("#[cfg(feature = \"server\")]", "#[cfg(test)]")
                .replace("#[cfg(any(feature = \"server\", test))]", "#[cfg(test)]")
                .replace("use popup::PopupState;", "use self::popup::PopupState;")
                // serde_json adds cross-type comparisons to this crate.
                .replace("u16::MAX.into()", "u32::from(u16::MAX)");
            fs::write(target, code)?;
        }
    }
    Ok(())
}