diff --git a/Cargo.toml b/Cargo.toml index 1acc094..cec6b6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadow-rs" -version = "0.19.0" +version = "0.20.0" authors = ["baoyachi "] edition = "2021" description = "A build-time information stored in your rust project" diff --git a/src/env.rs b/src/env.rs index daaf73f..be27b89 100644 --- a/src/env.rs +++ b/src/env.rs @@ -13,24 +13,24 @@ pub struct SystemEnv { map: BTreeMap, } -const BUILD_OS: ShadowConst = "BUILD_OS"; -const RUST_VERSION: ShadowConst = "RUST_VERSION"; -const RUST_CHANNEL: ShadowConst = "RUST_CHANNEL"; -const CARGO_VERSION: ShadowConst = "CARGO_VERSION"; -const CARGO_TREE: ShadowConst = "CARGO_TREE"; +pub const BUILD_OS: ShadowConst = "BUILD_OS"; +pub const RUST_VERSION: ShadowConst = "RUST_VERSION"; +pub const RUST_CHANNEL: ShadowConst = "RUST_CHANNEL"; +pub const CARGO_VERSION: ShadowConst = "CARGO_VERSION"; +pub const CARGO_TREE: ShadowConst = "CARGO_TREE"; -const BUILD_TARGET: ShadowConst = "BUILD_TARGET"; -const BUILD_TARGET_ARCH: ShadowConst = "BUILD_TARGET_ARCH"; +pub const BUILD_TARGET: ShadowConst = "BUILD_TARGET"; +pub const BUILD_TARGET_ARCH: ShadowConst = "BUILD_TARGET_ARCH"; -const CARGO_MANIFEST_DIR: ShadowConst = "CARGO_MANIFEST_DIR"; +pub const CARGO_MANIFEST_DIR: ShadowConst = "CARGO_MANIFEST_DIR"; // const CARGO_METADATA: ShadowConst = "CARGO_METADATA"; -const PKG_VERSION: ShadowConst = "PKG_VERSION"; -const PKG_DESCRIPTION: ShadowConst = "PKG_DESCRIPTION"; -const PKG_VERSION_MAJOR: ShadowConst = "PKG_VERSION_MAJOR"; -const PKG_VERSION_MINOR: ShadowConst = "PKG_VERSION_MINOR"; -const PKG_VERSION_PATCH: ShadowConst = "PKG_VERSION_PATCH"; -const PKG_VERSION_PRE: ShadowConst = "PKG_VERSION_PRE"; +pub const PKG_VERSION: ShadowConst = "PKG_VERSION"; +pub const PKG_DESCRIPTION: ShadowConst = "PKG_DESCRIPTION"; +pub const PKG_VERSION_MAJOR: ShadowConst = "PKG_VERSION_MAJOR"; +pub const PKG_VERSION_MINOR: ShadowConst = "PKG_VERSION_MINOR"; +pub const PKG_VERSION_PATCH: ShadowConst = "PKG_VERSION_PATCH"; +pub const PKG_VERSION_PRE: ShadowConst = "PKG_VERSION_PRE"; impl SystemEnv { fn init(&mut self, std_env: &BTreeMap) -> SdResult<()> { diff --git a/src/git.rs b/src/git.rs index ffbcbc6..f0747d2 100644 --- a/src/git.rs +++ b/src/git.rs @@ -9,16 +9,16 @@ use std::path::Path; use std::process::{Command, Stdio}; pub const BRANCH: ShadowConst = "BRANCH"; -pub(crate) const TAG: ShadowConst = "TAG"; -const SHORT_COMMIT: ShadowConst = "SHORT_COMMIT"; -const COMMIT_HASH: ShadowConst = "COMMIT_HASH"; -const COMMIT_DATE: ShadowConst = "COMMIT_DATE"; -const COMMIT_DATE_2822: ShadowConst = "COMMIT_DATE_2822"; -const COMMIT_DATE_3339: ShadowConst = "COMMIT_DATE_3339"; -const COMMIT_AUTHOR: ShadowConst = "COMMIT_AUTHOR"; -const COMMIT_EMAIL: ShadowConst = "COMMIT_EMAIL"; -const GIT_CLEAN: ShadowConst = "GIT_CLEAN"; -const GIT_STATUS_FILE: ShadowConst = "GIT_STATUS_FILE"; +pub const TAG: ShadowConst = "TAG"; +pub const SHORT_COMMIT: ShadowConst = "SHORT_COMMIT"; +pub const COMMIT_HASH: ShadowConst = "COMMIT_HASH"; +pub const COMMIT_DATE: ShadowConst = "COMMIT_DATE"; +pub const COMMIT_DATE_2822: ShadowConst = "COMMIT_DATE_2822"; +pub const COMMIT_DATE_3339: ShadowConst = "COMMIT_DATE_3339"; +pub const COMMIT_AUTHOR: ShadowConst = "COMMIT_AUTHOR"; +pub const COMMIT_EMAIL: ShadowConst = "COMMIT_EMAIL"; +pub const GIT_CLEAN: ShadowConst = "GIT_CLEAN"; +pub const GIT_STATUS_FILE: ShadowConst = "GIT_STATUS_FILE"; #[derive(Default, Debug)] pub struct Git { diff --git a/src/lib.rs b/src/lib.rs index 4bab0df..7a53c07 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,14 +163,11 @@ mod git; pub use is_debug::*; use build::*; -use env::*; - -use git::*; use crate::ci::CiType; pub use crate::date_time::DateTime; pub use const_format::*; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::env as std_env; use std::fs::File; use std::io::Write; @@ -182,7 +179,8 @@ use crate::gen_const::{ BUILD_CONST_VERSION, }; pub use err::{SdResult, ShadowError}; -pub use git::{branch, git_clean, git_status_file, tag}; + +pub use {build::ShadowConst, env::*, git::*}; pub trait Format { fn human_format(&self) -> String; @@ -217,7 +215,29 @@ macro_rules! shadow { /// } /// ``` pub fn new() -> SdResult<()> { - Shadow::build()?; + Shadow::build(Default::default())?; + Ok(()) +} + +/// It's shadow-rs Initialization entry, If deny const is configured, constants will not be generated. +/// +/// In build.rs `main()` function call for this function. +/// +/// # Examples +/// +/// +/// ```ignore +/// +/// use std::collections::BTreeSet; +/// +/// fn main() -> shadow_rs::SdResult<()> { +/// let mut deny = BTreeSet::new(); +/// deny.insert(shadow_rs::CARGO_TREE); +/// shadow_rs::new_deny(deny) +/// } +/// ``` +pub fn new_deny(deny_const: BTreeSet) -> SdResult<()> { + Shadow::build(deny_const)?; Ok(()) } @@ -243,7 +263,7 @@ pub fn new_hook(f: F) -> SdResult<()> where F: FnOnce(&File) -> SdResult<()>, { - let shadow = Shadow::build()?; + let shadow = Shadow::build(Default::default())?; shadow.hook(f) } @@ -261,6 +281,7 @@ pub struct Shadow { pub f: File, pub map: BTreeMap, pub std_env: BTreeMap, + pub deny_const: BTreeSet, } impl Shadow { @@ -293,13 +314,17 @@ impl Shadow { CiType::None } - pub fn build() -> SdResult { + pub fn build(deny_const: BTreeSet) -> SdResult { let src_path = std::env::var("CARGO_MANIFEST_DIR")?; let out_path = std::env::var("OUT_DIR")?; - Self::build_inner(src_path, out_path) + Self::build_inner(src_path, out_path, deny_const) } - fn build_inner(src_path: String, out_path: String) -> SdResult { + fn build_inner( + src_path: String, + out_path: String, + deny_const: BTreeSet, + ) -> SdResult { let out = { let path = Path::new(out_path.as_str()); if !out_path.ends_with('/') { @@ -313,6 +338,7 @@ impl Shadow { f: File::create(out)?, map: Default::default(), std_env: Default::default(), + deny_const, }; shadow.std_env = get_std_env(); @@ -328,11 +354,20 @@ impl Shadow { } shadow.map = map; + // deny const + shadow.filter_deny(); + shadow.write_all()?; Ok(shadow) } + fn filter_deny(&mut self) { + self.deny_const.iter().for_each(|x| { + self.map.remove(&**x); + }) + } + fn write_all(&mut self) -> SdResult<()> { self.gen_header()?; @@ -475,11 +510,25 @@ mod tests { #[test] fn test_build() -> SdResult<()> { - Shadow::build_inner("./".into(), "./".into())?; + Shadow::build_inner("./".into(), "./".into(), Default::default())?; + let shadow = fs::read_to_string("./shadow.rs")?; + assert!(!shadow.is_empty()); + assert!(shadow.lines().count() > 0); + println!("{shadow}"); + Ok(()) + } + + #[test] + fn test_build_deny() -> SdResult<()> { + let mut deny = BTreeSet::new(); + deny.insert(CARGO_TREE); + Shadow::build_inner("./".into(), "./".into(), deny)?; let shadow = fs::read_to_string("./shadow.rs")?; assert!(!shadow.is_empty()); assert!(shadow.lines().count() > 0); println!("{shadow}"); + let expect = "pub const CARGO_TREE :&str"; + assert!(!shadow.contains(expect)); Ok(()) }