Skip to content

Commit

Permalink
Merge pull request #165 from baoyachi/issue/153
Browse files Browse the repository at this point in the history
add CARGO_METADATA const
  • Loading branch information
baoyachi authored Jul 20, 2024
2 parents 4be7e70 + 0508f45 commit 42dc425
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 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.29.0"
version = "0.30.0-alpha.1"
authors = ["baoyachi <[email protected]>"]
edition = "2021"
description = "A build-time information stored in your rust project"
Expand Down
1 change: 1 addition & 0 deletions example_shadow/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub fn print_build() {
println!("rust_channel:{}", build::RUST_CHANNEL);
println!("cargo_version:{}", build::CARGO_VERSION);
println!("cargo_tree:{}", build::CARGO_TREE);
println!("cargo_metadata:{:?}", build::CARGO_METADATA);

println!("project_name:{}", build::PROJECT_NAME);
println!("build_time:{}", build::BUILD_TIME);
Expand Down
16 changes: 12 additions & 4 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl ConstVal {
ConstVal {
desc: desc.into(),
v: "".to_string(),
t: ConstType::OptStr,
t: ConstType::Str,
}
}

Expand All @@ -30,25 +30,33 @@ impl ConstVal {
t: ConstType::Bool,
}
}

pub fn new_slice<S: Into<String>>(desc: S) -> ConstVal {
ConstVal {
desc: desc.into(),
v: "".to_string(),
t: ConstType::Slice,
}
}
}

/// Supported types of build constants.
#[derive(Debug, Clone)]
pub enum ConstType {
/// [`Option<&str>`](`Option`).
OptStr,
/// [`&str`](`str`).
Str,
/// [`bool`].
Bool,
/// [`&[u8]`].
Slice,
}

impl Display for ConstType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ConstType::OptStr => write!(f, "Option<&str>"),
ConstType::Str => write!(f, "&str"),
ConstType::Bool => write!(f, "bool"),
ConstType::Slice => write!(f, "&[u8]"),
}
}
}
28 changes: 16 additions & 12 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Note that as per Rustup toolchain format, this variable may or may not contain h
but it will always contain [channel](https://rust-lang.github.io/rustup/concepts/channels.html) information (stable, beta or nightly)."#;
pub const RUST_CHANNEL: ShadowConst = "RUST_CHANNEL";

pub const CARGO_METADATA: ShadowConst = "CARGO_METADATA";
const CARGO_METADATA_DOC: ShadowConst = r#"
The information about the workspace members and resolved dependencies of the current package.
See the [cargo_metadata](https://crates.io/crates/cargo_metadata) crate for a Rust API for reading the metadata."#;

const CARGO_VERSION_DOC: &str = r#"
The cargo version which which the project was built, as output by `cargo --version`."#;
pub const CARGO_VERSION: ShadowConst = "CARGO_VERSION";
Expand Down Expand Up @@ -83,7 +88,6 @@ impl SystemEnv {
fn init(&mut self, std_env: &BTreeMap<String, String>) -> SdResult<()> {
let mut update_val = |c: ShadowConst, v: String| {
if let Some(val) = self.map.get_mut(c) {
val.t = ConstType::Str;
val.v = v;
}
};
Expand Down Expand Up @@ -115,17 +119,15 @@ impl SystemEnv {
}
}

// TODO completed

// 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 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 let Some(v) = std_env.get("TARGET") {
update_val(BUILD_TARGET, v.to_string());
Expand Down Expand Up @@ -258,6 +260,8 @@ pub fn new_system_env(std_env: &BTreeMap<String, String>) -> BTreeMap<ShadowCons

env.map
.insert(RUST_CHANNEL, ConstVal::new(RUST_CHANNEL_DOC));
env.map
.insert(CARGO_METADATA, ConstVal::new_slice(CARGO_METADATA_DOC));
env.map
.insert(RUST_VERSION, ConstVal::new(RUST_VERSION_DOC));
env.map
Expand Down
27 changes: 16 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,6 @@ impl Shadow {
let desc = format!("#[doc=r#\"{}\"#]", val.desc);

let define = match val.t {
ConstType::OptStr => format!(
"#[allow(dead_code)]\n\
#[allow(clippy::all)]\n\
pub const {} :{} = r#\"{}\"#;",
shadow_const.to_ascii_uppercase(),
ConstType::Str,
""
),
ConstType::Str => format!(
"#[allow(dead_code)]\n\
#[allow(clippy::all)]\n\
Expand All @@ -464,6 +456,13 @@ impl Shadow {
ConstType::Bool,
val.v.parse::<bool>().unwrap()
),
ConstType::Slice => format!(
"#[allow(dead_code)]\n\
pub const {} :{} = &{:?};",
shadow_const.to_ascii_uppercase(),
ConstType::Slice,
val.v.as_bytes()
),
};

writeln!(&self.f, "{desc}")?;
Expand Down Expand Up @@ -505,8 +504,15 @@ impl Shadow {
let mut print_val = String::from("\n");

// append gen const
for k in self.map.keys() {
let tmp = format!(r#"{}println!("{k}:{{{k}}}\n");{}"#, "\t", "\n");
for (k, v) in &self.map {
let tmp = match v.t {
ConstType::Str | ConstType::Bool => {
format!(r#"{}println!("{k}:{{{k}}}\n");{}"#, "\t", "\n")
}
ConstType::Slice => {
format!(r#"{}println!("{k}:{{:?}}\n",{});{}"#, "\t", k, "\n",)
}
};
print_val.push_str(tmp.as_str());
}

Expand Down Expand Up @@ -540,7 +546,6 @@ mod tests {
let shadow = fs::read_to_string("./shadow.rs")?;
assert!(!shadow.is_empty());
assert!(shadow.lines().count() > 0);
println!("{shadow}");
Ok(())
}

Expand Down

0 comments on commit 42dc425

Please sign in to comment.