rxf-cli 0.1.3

CLI tool for the RXF SDK
use std::fs;
use std::path::Path;

fn main() {
    let manifest_dir = env!("CARGO_MANIFEST_DIR");

    let flutter_version = read_metadata_version(manifest_dir, "flutter_version");
    let rust_version = read_metadata_version(manifest_dir, "rust_version");

    println!("cargo:rustc-env=RXF_FLUTTER_VERSION={}", flutter_version);
    println!("cargo:rustc-env=RXF_RUST_VERSION={}", rust_version);

    println!(
        "cargo:rerun-if-changed={}",
        Path::new(manifest_dir)
            .join("../rxf_flutter/pubspec.yaml")
            .display()
    );
    println!(
        "cargo:rerun-if-changed={}",
        Path::new(manifest_dir)
            .join("../rxf_rust/Cargo.toml")
            .display()
    );
    println!(
        "cargo:rerun-if-changed={}",
        Path::new(manifest_dir).join("Cargo.toml").display()
    );
}

/// Read version from `[package.metadata.rxf]` in this package's own Cargo.toml.
/// Used as fallback when sibling package files are unavailable (e.g. during `cargo publish`).
fn read_metadata_version(manifest_dir: &str, key: &str) -> String {
    let cargo_toml = Path::new(manifest_dir).join("Cargo.toml");
    let content = fs::read_to_string(&cargo_toml)
        .unwrap_or_else(|_| panic!("Failed to read {}", cargo_toml.display()));

    let mut in_section = false;
    for line in content.lines() {
        let line = line.trim();
        if line == "[package.metadata.rxf]" {
            in_section = true;
            continue;
        }
        if in_section && line.starts_with('[') {
            break;
        }
        if in_section {
            if let Some(rest) = line.strip_prefix(key) {
                let rest = rest.trim().strip_prefix('=').unwrap_or("").trim();
                return rest.trim_matches('"').to_string();
            }
        }
    }
    panic!("'{key}' not found in [package.metadata.rxf] section of Cargo.toml");
}