Skip to content

Commit

Permalink
Merge pull request #126 from baoyachi/deny_const
Browse files Browse the repository at this point in the history
Deny const
  • Loading branch information
baoyachi authored Dec 30, 2022
2 parents 0ec3eaf + 68eda42 commit c4042cf
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadow-rs"
version = "0.19.0"
version = "0.20.0"
authors = ["baoyachi <[email protected]>"]
edition = "2021"
description = "A build-time information stored in your rust project"
Expand Down
28 changes: 14 additions & 14 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ pub struct SystemEnv {
map: BTreeMap<ShadowConst, ConstVal>,
}

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<String, String>) -> SdResult<()> {
Expand Down
20 changes: 10 additions & 10 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
71 changes: 60 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<ShadowConst>) -> SdResult<()> {
Shadow::build(deny_const)?;
Ok(())
}

Expand All @@ -243,7 +263,7 @@ pub fn new_hook<F>(f: F) -> SdResult<()>
where
F: FnOnce(&File) -> SdResult<()>,
{
let shadow = Shadow::build()?;
let shadow = Shadow::build(Default::default())?;
shadow.hook(f)
}

Expand All @@ -261,6 +281,7 @@ pub struct Shadow {
pub f: File,
pub map: BTreeMap<ShadowConst, ConstVal>,
pub std_env: BTreeMap<String, String>,
pub deny_const: BTreeSet<ShadowConst>,
}

impl Shadow {
Expand Down Expand Up @@ -293,13 +314,17 @@ impl Shadow {
CiType::None
}

pub fn build() -> SdResult<Shadow> {
pub fn build(deny_const: BTreeSet<ShadowConst>) -> SdResult<Shadow> {
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<Shadow> {
fn build_inner(
src_path: String,
out_path: String,
deny_const: BTreeSet<ShadowConst>,
) -> SdResult<Shadow> {
let out = {
let path = Path::new(out_path.as_str());
if !out_path.ends_with('/') {
Expand All @@ -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();

Expand All @@ -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()?;

Expand Down Expand Up @@ -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(())
}

Expand Down

0 comments on commit c4042cf

Please sign in to comment.