Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make using the CARGO_METADATA object simpler #181

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>"]
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
Loading