diff --git a/Cargo.toml b/Cargo.toml index b67306a..0fe46a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadow-rs" -version = "0.35.0" +version = "0.35.1" authors = ["baoyachi "] edition = "2021" description = "A build-time information stored in your rust project" diff --git a/example_shadow_hook/build.rs b/example_shadow_hook/build.rs index 86f4ae8..e559049 100644 --- a/example_shadow_hook/build.rs +++ b/example_shadow_hook/build.rs @@ -13,7 +13,8 @@ fn hook(file: &File) -> SdResult<()> { } fn append_write_const(mut file: &File) -> SdResult<()> { - let hook_const: &str = r#"pub const HOOK_CONST: &str = "hello hook const";"#; + let hook_const: &str = r#"#[allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)] +pub const HOOK_CONST: &str = "hello hook const";"#; writeln!(file, "{hook_const}")?; Ok(()) } @@ -21,7 +22,7 @@ fn append_write_const(mut file: &File) -> SdResult<()> { fn append_write_fn(mut file: &File) -> SdResult<()> { let hook_fn: &str = r#" pub fn hook_fn() -> &'static str{ - "hello hook bar fn" + HOOK_CONST }"#; writeln!(file, "{hook_fn}")?; Ok(()) diff --git a/example_shadow_hook/src/main.rs b/example_shadow_hook/src/main.rs index a6aa436..e62784c 100644 --- a/example_shadow_hook/src/main.rs +++ b/example_shadow_hook/src/main.rs @@ -6,4 +6,5 @@ fn main() { // how to use new_hook function: https://github.com/baoyachi/shadow-rs/blob/master/example_shadow_hook/build.rs println!("const:{}", build::HOOK_CONST); //expect:'hello hook const' println!("fn:{}", build::hook_fn()); //expect:'hello hook bar fn' + assert_eq!(build::hook_fn(), build::HOOK_CONST); } diff --git a/src/env.rs b/src/env.rs index 2d16e31..9a127ce 100644 --- a/src/env.rs +++ b/src/env.rs @@ -2,7 +2,7 @@ use crate::build::*; use crate::date_time::now_date_time; use crate::env::dep_source_replace::filter_cargo_tree; use crate::err::SdResult; -use crate::Format; +use crate::{Format, Shadow}; use is_debug::build_channel; use std::collections::BTreeMap; use std::env; @@ -85,7 +85,8 @@ The project's semver pre-release version, as determined by the Cargo.toml manife pub const PKG_VERSION_PRE: ShadowConst = "PKG_VERSION_PRE"; impl SystemEnv { - fn init(&mut self, std_env: &BTreeMap) -> SdResult<()> { + fn init(&mut self, shadow: &Shadow) -> SdResult<()> { + let std_env = &shadow.std_env; let mut update_val = |c: ShadowConst, v: String| { if let Some(val) = self.map.get_mut(c) { val.v = v; @@ -110,23 +111,42 @@ impl SystemEnv { ); } - if let Ok(out) = Command::new("cargo").arg("tree").output() { - let input = String::from_utf8(out.stdout)?; - if let Some(index) = input.find('\n') { - let lines = - filter_cargo_tree(input.get(index..).unwrap_or_default().split('\n').collect()); - update_val(CARGO_TREE, lines); + // If the build constant `CARGO_TREE` is not in the deny list, + // See discussions and issues related to this functionality: + // - https://github.com/baoyachi/shadow-rs/issues/184 + // - https://github.com/baoyachi/shadow-rs/issues/135 + // - https://github.com/rust-lang/cargo/issues/12195 + // - https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#lockfile-path + if !shadow.deny_contains(CARGO_TREE) { + if let Ok(out) = Command::new("cargo").arg("tree").output() { + let input = String::from_utf8(out.stdout)?; + if let Some(index) = input.find('\n') { + let lines = filter_cargo_tree( + input.get(index..).unwrap_or_default().split('\n').collect(), + ); + update_val(CARGO_TREE, lines); + } } } - if let Ok(out) = Command::new("cargo") - .args(["metadata", "--format-version", "1"]) - .output() - { - update_val( - CARGO_METADATA, - String::from_utf8(out.stdout)?.trim().to_string(), - ); + // If the build constant `CARGO_METADATA` is not in the deny list, + // See discussions and issues related to this functionality: + // - https://github.com/baoyachi/shadow-rs/issues/184 + // - https://github.com/baoyachi/shadow-rs/issues/135 + // - https://github.com/rust-lang/cargo/issues/12195 + // - https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#lockfile-path + if !shadow.deny_contains(CARGO_METADATA) { + // Attempt to run the `cargo metadata --format-version 1` command. + if let Ok(output) = Command::new("cargo") + .args(["metadata", "--format-version", "1"]) + .output() + { + // If successful, parse the output and update the value associated with `CARGO_METADATA`. + update_val( + CARGO_METADATA, + String::from_utf8(output.stdout)?.trim().to_string(), + ); + } } if let Some(v) = std_env.get("TARGET") { @@ -247,9 +267,7 @@ mod dep_source_replace { /// Create all `shadow-rs` constants which are determined by the build environment. /// The data for these constants is provided by the `std_env` argument. -pub(crate) fn new_system_env( - std_env: &BTreeMap, -) -> BTreeMap { +pub(crate) fn new_system_env(shadow: &Shadow) -> BTreeMap { let mut env = SystemEnv::default(); env.map.insert( BUILD_OS, @@ -271,11 +289,6 @@ pub(crate) fn new_system_env( env.map.insert(CARGO_TREE, ConstVal::new(CARGO_TREE_DOC)); - // env.map.insert( - // CARGO_METADATA, - // ConstVal::new("display build cargo dependencies by metadata.It's use by exec command `cargo metadata`"), - // ); - env.map .insert(BUILD_TARGET, ConstVal::new(BUILD_TARGET_DOC)); @@ -298,7 +311,7 @@ pub(crate) fn new_system_env( env.map .insert(CARGO_MANIFEST_DIR, ConstVal::new(CARGO_MANIFEST_DIR_DOC)); - if let Err(e) = env.init(std_env) { + if let Err(e) = env.init(shadow) { println!("{e}"); } env.map diff --git a/src/lib.rs b/src/lib.rs index 0feeef4..3f28b86 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -319,7 +319,7 @@ pub struct Shadow { pub map: BTreeMap, /// Build environment variables, obtained through [`std::env::vars`]. pub std_env: BTreeMap, - /// Constants in the denylist, passed through [`new_deny`] or [`Shadow::build`]. + /// Constants in the deny list, passed through [`new_deny`] or [`Shadow::build`]. pub deny_const: BTreeSet, } @@ -355,6 +355,17 @@ impl Shadow { CiType::None } + /// Checks if the specified build constant is in the deny list. + /// + /// # Arguments + /// * `deny_const` - A value of type `ShadowConst` representing the build constant to check. + /// + /// # Returns + /// * `true` if the build constant is present in the deny list; otherwise, `false`. + pub fn deny_contains(&self, deny_const: ShadowConst) -> bool { + self.deny_const.contains(&deny_const) + } + /// Create a new [`Shadow`] configuration with a provided denylist. /// The project source path and output file are automatically derived from Cargo build environment variables. pub fn build(deny_const: BTreeSet) -> SdResult { @@ -392,7 +403,7 @@ impl Shadow { for (k, v) in new_project(&shadow.std_env) { map.insert(k, v); } - for (k, v) in new_system_env(&shadow.std_env) { + for (k, v) in new_system_env(&shadow) { map.insert(k, v); } shadow.map = map;