From 33946f500794dea7c65b139c1ef442abd48bcac3 Mon Sep 17 00:00:00 2001 From: Dmitri Makarov Date: Sat, 24 Dec 2022 11:54:55 -0500 Subject: [PATCH] Add an option to reinstall sbf-tool binaries by cargo-build-sbf (#29410) --- sdk/cargo-build-sbf/src/main.rs | 23 +++++++++++++++++++++++ sdk/cargo-build-sbf/tests/crates.rs | 9 ++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/sdk/cargo-build-sbf/src/main.rs b/sdk/cargo-build-sbf/src/main.rs index 602afce9b..5c76692f6 100644 --- a/sdk/cargo-build-sbf/src/main.rs +++ b/sdk/cargo-build-sbf/src/main.rs @@ -27,6 +27,7 @@ struct Config<'a> { sbf_tools_version: &'a str, dump: bool, features: Vec, + force_tools_install: bool, generate_child_script_on_failure: bool, no_default_features: bool, offline: bool, @@ -53,6 +54,7 @@ impl Default for Config<'_> { sbf_tools_version: "(unknown)", dump: false, features: vec![], + force_tools_install: false, generate_child_script_on_failure: false, no_default_features: false, offline: false, @@ -130,6 +132,18 @@ fn install_if_missing( download_file_name: &str, target_path: &Path, ) -> Result<(), String> { + if config.force_tools_install { + if target_path.is_dir() { + debug!("Remove directory {:?}", target_path); + fs::remove_dir_all(target_path).map_err(|err| err.to_string())?; + } + let source_base = config.sbf_sdk.join("dependencies"); + if source_base.exists() { + let source_path = source_base.join(package); + debug!("Remove file {:?}", source_path); + fs::remove_file(source_path).map_err(|err| err.to_string())?; + } + } // Check whether the target path is an empty directory. This can // happen if package download failed on previous run of // cargo-build-sbf. Remove the target_path directory in this @@ -141,6 +155,7 @@ fn install_if_missing( .next() .is_none() { + debug!("Remove directory {:?}", target_path); fs::remove_dir(target_path).map_err(|err| err.to_string())?; } @@ -153,6 +168,7 @@ fn install_if_missing( .unwrap_or(false) { if target_path.exists() { + debug!("Remove file {:?}", target_path); fs::remove_file(target_path).map_err(|err| err.to_string())?; } fs::create_dir_all(target_path).map_err(|err| err.to_string())?; @@ -886,6 +902,12 @@ fn main() { .multiple_values(true) .help("Space-separated list of features to activate"), ) + .arg( + Arg::new("force_tools_install") + .long("force-tools-install") + .takes_value(false) + .help("Download and install sbf-tools even when existing tools are located"), + ) .arg( Arg::new("generate_child_script_on_failure") .long("generate-child-script-on-failure") @@ -970,6 +992,7 @@ fn main() { sbf_tools_version, dump: matches.is_present("dump"), features: matches.values_of_t("features").ok().unwrap_or_default(), + force_tools_install: matches.is_present("force_tools_install"), generate_child_script_on_failure: matches.is_present("generate_child_script_on_failure"), no_default_features: matches.is_present("no_default_features"), remap_cwd: !matches.is_present("remap_cwd"), diff --git a/sdk/cargo-build-sbf/tests/crates.rs b/sdk/cargo-build-sbf/tests/crates.rs index 08dc82006..8b2fd4bcd 100644 --- a/sdk/cargo-build-sbf/tests/crates.rs +++ b/sdk/cargo-build-sbf/tests/crates.rs @@ -1,11 +1,15 @@ use { predicates::prelude::*, - std::{env, fs}, + std::{ + env, fs, + sync::atomic::{AtomicBool, Ordering}, + }, }; #[macro_use] extern crate serial_test; +static SBF_TOOLS_INSTALL: AtomicBool = AtomicBool::new(true); fn run_cargo_build(crate_name: &str, extra_args: &[&str], fail: bool) { let cwd = env::current_dir().expect("Unable to get current working directory"); let toml = cwd @@ -15,6 +19,9 @@ fn run_cargo_build(crate_name: &str, extra_args: &[&str], fail: bool) { .join("Cargo.toml"); let toml = format!("{}", toml.display()); let mut args = vec!["-v", "--sbf-sdk", "../sbf", "--manifest-path", &toml]; + if SBF_TOOLS_INSTALL.fetch_and(false, Ordering::SeqCst) { + args.push("--force-tools-install"); + } for arg in extra_args { args.push(arg); }