From 397e703e64854ee96f73f611ad0a506fbf8ee830 Mon Sep 17 00:00:00 2001 From: segfault-magnet Date: Thu, 5 Dec 2024 11:50:27 +0100 Subject: [PATCH] read cargo toml instead of running cargo metadata --- Cargo.toml | 1 + scripts/fuel-core-version/Cargo.toml | 2 +- scripts/fuel-core-version/src/main.rs | 35 +++++++++++++++++++++------ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5360aeb629..7bfe275ba6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,6 +84,7 @@ which = { version = "6.0.0", default-features = false } zeroize = "1.7.0" octocrab = { version = "0.39", default-features = false } dotenv = { version = "0.15", default-features = false } +toml = { version = "0.8", default-features = false } # Dependencies from the `fuel-core` repository: fuel-core = { version = "0.40.0", default-features = false, features = [ diff --git a/scripts/fuel-core-version/Cargo.toml b/scripts/fuel-core-version/Cargo.toml index 54a21f015d..e5e231a888 100644 --- a/scripts/fuel-core-version/Cargo.toml +++ b/scripts/fuel-core-version/Cargo.toml @@ -14,4 +14,4 @@ clap = { version = "4.5.3", features = ["derive"] } color-eyre = "0.6.2" fuels-accounts = { workspace = true, features = ["std"] } semver = { workspace = true } -versions-replacer = { workspace = true } +toml = { workspace = true, features = ["parse"] } diff --git a/scripts/fuel-core-version/src/main.rs b/scripts/fuel-core-version/src/main.rs index c3d329f73f..2320edd46e 100644 --- a/scripts/fuel-core-version/src/main.rs +++ b/scripts/fuel-core-version/src/main.rs @@ -1,7 +1,9 @@ +use color_eyre::eyre::OptionExt; use std::{ fs, path::{Path, PathBuf}, }; +use toml::Value; use clap::{Parser, Subcommand}; use color_eyre::{ @@ -10,13 +12,6 @@ use color_eyre::{ }; use fuels_accounts::provider::SUPPORTED_FUEL_CORE_VERSION; use semver::Version; -use versions_replacer::metadata::collect_versions_from_cargo_toml; - -fn get_version_from_toml(manifest_path: impl AsRef) -> Result { - let versions = collect_versions_from_cargo_toml(manifest_path)?; - let version = versions["fuel-core-types"].parse::()?; - Ok(version) -} fn write_version_to_file(version: Version, version_file_path: impl AsRef) -> Result<()> { let Version { @@ -74,7 +69,7 @@ fn main() -> Result<()> { command, manifest_path, } = App::parse(); - let version = get_version_from_toml(&manifest_path)?; + let version = read_fuel_core_version(&manifest_path)?; let version_file_path = get_version_file_path(&manifest_path)?; match command { Command::Write => write_version_to_file(version, version_file_path)?, @@ -82,3 +77,27 @@ fn main() -> Result<()> { } Ok(()) } + +pub fn read_fuel_core_version(path: impl AsRef) -> color_eyre::Result { + let cargo_toml: Value = fs::read_to_string(path.as_ref())?.parse::()?; + + let str_version = + find_dependency_version(&cargo_toml).ok_or_eyre("could not find fuel-core version")?; + + Ok(str_version.parse()?) +} + +fn find_dependency_version(toml: &Value) -> Option { + match toml + .get("workspace")? + .get("dependencies")? + .get("fuel-core")? + { + Value::String(version) => Some(version.clone()), + Value::Table(table) => table + .get("version") + .and_then(|v| v.as_str()) + .map(String::from), + _ => None, + } +}