Skip to content

Commit

Permalink
add deny const
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi committed Dec 28, 2022
1 parent 0ec3eaf commit 38ef356
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
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
37 changes: 33 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ 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 Down Expand Up @@ -261,6 +261,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 @@ -296,10 +297,14 @@ impl Shadow {
pub fn build() -> 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, Default::default())
}

fn build_inner(src_path: String, out_path: String) -> SdResult<Shadow> {
fn build_inner(
src_path: String,
out_path: String,
deny_info: BTreeSet<ShadowConst>,
) -> SdResult<Shadow> {
let out = {
let path = Path::new(out_path.as_str());
if !out_path.ends_with('/') {
Expand All @@ -313,6 +318,7 @@ impl Shadow {
f: File::create(out)?,
map: Default::default(),
std_env: Default::default(),
deny_const: deny_info,
};
shadow.std_env = get_std_env();

Expand All @@ -328,11 +334,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 +490,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 38ef356

Please sign in to comment.