Skip to content

Commit

Permalink
Merge pull request #181 from baoyachi/issue/179
Browse files Browse the repository at this point in the history
Make using the CARGO_METADATA object simpler
  • Loading branch information
baoyachi authored Sep 6, 2024
2 parents deff1f4 + 65c5663 commit ccb09f1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 72 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadow-rs"
version = "0.33.0"
version = "0.34.0"
authors = ["baoyachi <liaoymxsdl@gmail.com>"]
edition = "2021"
description = "A build-time information stored in your rust project"
Expand All @@ -19,8 +19,8 @@ all-features = true

[dependencies]
is_debug = "1.0.1"
const_format = "0.2.22"
time = { version = "0.3.11", features = ["formatting", "local-offset", "parsing"] }
const_format = { version = "0.2.22", default-features = false }
time = { version = "0.3.11", features = ["formatting", "local-offset", "parsing"], default-features = false }


#! Optional Dependencies:
Expand All @@ -33,8 +33,12 @@ tzdb = { version = "0.6.0", optional = true, default-features = false, features

document-features = { version = "0.2", optional = true }

cargo_metadata = { version = "0.18.1", optional = true, default-features = false }
serde_json = { version = "1", default-features = false, optional = true }

[features]
default = ["git2", "tzdb"]
metadata = ["cargo_metadata", "serde_json"]

[dev-dependencies]
winnow = "0.6"
Expand Down
60 changes: 0 additions & 60 deletions cliff.toml

This file was deleted.

2 changes: 1 addition & 1 deletion example_shadow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build = "build.rs"

[dependencies]
clap = "4.0.1"
shadow-rs = { path = "../" }
shadow-rs = { path = "../",features = ["metadata"] }

[build-dependencies]
shadow-rs = { path = "../" }
Expand Down
4 changes: 3 additions & 1 deletion example_shadow/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fn main() -> shadow_rs::SdResult<()> {
shadow_rs::new()
// shadow_rs::new()

shadow_rs::new_deny(Default::default())
}
2 changes: 2 additions & 0 deletions example_shadow/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ pub fn print_build() {
println!("build_time_2822:{}", build::BUILD_TIME_2822);
println!("build_time_3339:{}", build::BUILD_TIME_3339);
println!("build_rust_channel:{}", build::BUILD_RUST_CHANNEL);

println!("{:?}", build::cargo_metadata().unwrap())
}
31 changes: 31 additions & 0 deletions src/gen_const.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::{Shadow, CARGO_CLIPPY_ALLOW_ALL, CARGO_METADATA};

macro_rules! gen_const {
($fn_name:ident, $fn_body:expr) => {
pub fn $fn_name() -> String {
Expand Down Expand Up @@ -96,6 +98,35 @@ gen_const!(clap_long_version_tag_const, CLAP_LONG_VERSION_TAG_CONST);
pub(crate) const BUILD_CONST_VERSION: &str = "VERSION";
pub(crate) const BUILD_CONST_CLAP_LONG_VERSION: &str = "CLAP_LONG_VERSION";

pub(crate) fn cargo_metadata_fn(shadow: &Shadow) -> String {
if !shadow.map.contains_key(CARGO_METADATA) {
return "".to_string();
}
format!(
r#"
use std::str::from_utf8;
use shadow_rs::cargo_metadata::Metadata;
use shadow_rs::serde_json;
/// Attempts to parse the Cargo package metadata from the generated constant CARGO_METADATA.
///
/// Returns a `Metadata` struct containing information about the Cargo workspace,
/// such as details about the packages and their dependencies.
///
/// # Return Values
/// - `Ok(Metadata)`: Contains the parsed metadata if successful.
/// - `Err(String)`: Returns an error message if converting the environment variable to a UTF-8 string or parsing JSON fails.
#[allow(dead_code)]
{}
pub fn cargo_metadata() -> Result<Metadata, String> {{
let metadata_json = from_utf8(CARGO_METADATA.as_ref()).map_err(|err| format!("generate 'CARGO_METADATA' value from UTF8 error:{{}}",err))?;
let meta: Metadata = serde_json::from_str(metadata_json).map_err(|err| err.to_string())?;
Ok(meta)
}}"#,
CARGO_CLIPPY_ALLOW_ALL
)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
25 changes: 18 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@
//! pub const GIT_STATUS_FILE: &str = "* src/lib.rs (dirty)";
//! ```
//!
#[cfg(feature = "metadata")]
pub extern crate cargo_metadata;
#[cfg(feature = "metadata")]
pub extern crate serde_json;

mod build;
mod ci;
Expand All @@ -168,9 +172,9 @@ use std::io::Write;
use std::path::Path;

use crate::gen_const::{
clap_long_version_branch_const, clap_long_version_tag_const, clap_version_branch_const,
clap_version_tag_const, version_branch_const, version_tag_const, BUILD_CONST_CLAP_LONG_VERSION,
BUILD_CONST_VERSION,
cargo_metadata_fn, clap_long_version_branch_const, clap_long_version_tag_const,
clap_version_branch_const, clap_version_tag_const, version_branch_const, version_tag_const,
BUILD_CONST_CLAP_LONG_VERSION, BUILD_CONST_VERSION,
};
pub use err::{SdResult, ShadowError};

Expand All @@ -182,7 +186,7 @@ pub trait Format {

const SHADOW_RS: &str = "shadow.rs";

const CARGO_CLIPPY_ALLOW_ALL: &str =
pub(crate) const CARGO_CLIPPY_ALLOW_ALL: &str =
"#[allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)]";

/// Add a module with the provided name which contains the build information generated by `shadow-rs`.
Expand Down Expand Up @@ -231,8 +235,10 @@ pub fn new() -> SdResult<()> {
/// membership and resolved dependencies for the current package, storing this data can result in
/// significantly larger crate sizes. As such, the CARGO_METADATA const is disabled by default.
///
/// Should you choose to retain this information, you have the option to customize a deny_const object
/// and override the `new_deny` method parameters accordingly.
/// Should you choose to retain this information, you have the option to customize a deny_const
/// object and override the `new_deny` method parameters accordingly.
///
#[allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)]
pub fn default_deny() -> BTreeSet<ShadowConst> {
BTreeSet::from([CARGO_METADATA])
}
Expand Down Expand Up @@ -262,7 +268,9 @@ pub fn new_deny(deny_const: BTreeSet<ShadowConst>) -> SdResult<()> {
}

/// Identical to [`new`], but additionally accepts an output hook.
/// The hook receives the output file of `shadow-rs`, and it can add additional entries to the output of `shadow-rs` by writing to this file.
///
/// The hook receives the output file of `shadow-rs`, and it can add additional entries to the
/// output of `shadow-rs` by writing to this file.
/// Note that since the output will be compiled as a Rust module, inserting invalid Rust code will lead to a compile error later on.
///
/// # Example
Expand All @@ -281,6 +289,7 @@ pub fn new_deny(deny_const: BTreeSet<ShadowConst>) -> SdResult<()> {
/// }
///
/// ```
///
pub fn new_hook<F>(f: F) -> SdResult<()>
where
F: FnOnce(&File) -> SdResult<()>,
Expand Down Expand Up @@ -549,6 +558,8 @@ impl Shadow {
);
writeln!(&self.f, "{everything_define}")?;

writeln!(&self.f, "{}", cargo_metadata_fn(self))?;

Ok(())
}
}
Expand Down

0 comments on commit ccb09f1

Please sign in to comment.