fn main()

in eng/scripts/update-pathversions.rs [17:57]


fn main() -> Result<(), Box<dyn std::error::Error>> {
    let add_mode = env::args().nth(1)
        .map(|arg| match arg.as_str() {
            "add" => true,
            "update" => false,
            _ => panic!("Invalid mode. Use 'add' or 'update'.")
        })
        .expect("requires 'add' or 'update' mode argument");

    let script_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
    let repo_root = script_root.join("../../..").canonicalize()?;

    // find all Cargo.toml files in the repo_root directory
    let exclude_dirs = vec![
        repo_root.join("eng"),
        repo_root.join("target")
    ];

    let toml_files = load_cargo_toml_files(&repo_root, &exclude_dirs)?;

    let package_versions = get_package_versions(&toml_files);

    for mut toml_file in toml_files {
        let should_add = add_mode && !toml_file.is_publish_disabled;

        update_package_versions(toml_file.document.as_table_mut(), &package_versions, should_add);

        // if the toml file has a workspace table, update the workspace table
        if let Some(workspace) = toml_file.document.get_mut("workspace") {
            if let Some(table) = workspace.as_table_mut() {
                update_package_versions(table, &package_versions, should_add);
            }
        }

        // write the updated document back to the file
        let mut file = fs::File::create(toml_file.path)?;
        fs::File::write_all(&mut file, toml_file.document.to_string().as_bytes())?;
    }

    Ok(())
}