|
| 1 | +//! Exports the packaged Agave runtime version for fixture report metadata. |
| 2 | +
|
| 3 | +use std::{ |
| 4 | + env, fs, |
| 5 | + path::{Path, PathBuf}, |
| 6 | +}; |
| 7 | + |
| 8 | +const AGAVE_FEATURE_SET_DEPENDENCY: &str = "agave-feature-set"; |
| 9 | +const VERSION_ENV: &str = "HPSVM_AGAVE_FEATURE_SET_VERSION"; |
| 10 | + |
| 11 | +fn main() { |
| 12 | + let manifest_dir = |
| 13 | + PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set")); |
| 14 | + let crate_manifest = manifest_dir.join("Cargo.toml"); |
| 15 | + let workspace_manifest = manifest_dir.join("..").join("..").join("Cargo.toml"); |
| 16 | + |
| 17 | + println!("cargo:rerun-if-changed={}", crate_manifest.display()); |
| 18 | + if workspace_manifest.exists() { |
| 19 | + println!("cargo:rerun-if-changed={}", workspace_manifest.display()); |
| 20 | + } |
| 21 | + |
| 22 | + let version = read_dependency_version(&crate_manifest) |
| 23 | + .or_else(|| read_dependency_version(&workspace_manifest)) |
| 24 | + .unwrap_or_else(|| String::from("unknown")); |
| 25 | + |
| 26 | + println!("cargo:rustc-env={VERSION_ENV}={version}"); |
| 27 | +} |
| 28 | + |
| 29 | +fn read_dependency_version(manifest_path: &Path) -> Option<String> { |
| 30 | + fs::read_to_string(manifest_path) |
| 31 | + .ok() |
| 32 | + .and_then(|manifest| extract_dependency_version(&manifest, AGAVE_FEATURE_SET_DEPENDENCY)) |
| 33 | +} |
| 34 | + |
| 35 | +fn extract_dependency_version(manifest: &str, dependency_name: &str) -> Option<String> { |
| 36 | + let quoted_prefix = format!("{dependency_name} = \""); |
| 37 | + let inline_table_prefix = format!("{dependency_name} = {{"); |
| 38 | + |
| 39 | + manifest |
| 40 | + .lines() |
| 41 | + .map(str::trim) |
| 42 | + .filter(|line| !line.is_empty() && !line.starts_with('#')) |
| 43 | + .find_map(|line| { |
| 44 | + line.strip_prefix("ed_prefix) |
| 45 | + .and_then(|value| value.split('"').next()) |
| 46 | + .map(str::to_owned) |
| 47 | + .or_else(|| { |
| 48 | + line.strip_prefix(&inline_table_prefix) |
| 49 | + .and_then(|_| line.split("version = \"").nth(1)) |
| 50 | + .and_then(|value| value.split('"').next()) |
| 51 | + .map(str::to_owned) |
| 52 | + }) |
| 53 | + }) |
| 54 | +} |
0 commit comments