From 998d000023c4e91c8dcfad0b0cc7838961c98219 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Fri, 6 Sep 2024 17:28:42 +0800 Subject: [PATCH 1/2] Make using the CARGO_METADATA object simpler --- Cargo.toml | 10 +++++-- cliff.toml | 60 -------------------------------------- example_shadow/Cargo.toml | 2 +- example_shadow/build.rs | 4 ++- example_shadow/src/main.rs | 2 ++ src/gen_const.rs | 31 ++++++++++++++++++++ src/lib.rs | 14 ++++++--- 7 files changed, 54 insertions(+), 69 deletions(-) delete mode 100644 cliff.toml diff --git a/Cargo.toml b/Cargo.toml index fb4b581..440c292 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadow-rs" -version = "0.33.0" +version = "0.34.0" authors = ["baoyachi "] edition = "2021" description = "A build-time information stored in your rust project" @@ -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: @@ -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" diff --git a/cliff.toml b/cliff.toml deleted file mode 100644 index 7a85fd1..0000000 --- a/cliff.toml +++ /dev/null @@ -1,60 +0,0 @@ -# configuration file for git-cliff (0.1.0) - -[changelog] -# changelog header -header = """ -# Changelog\n -All notable changes to this project will be documented in this file.\n -""" -# template for the changelog body -# https://tera.netlify.app/docs/#introduction -body = """ -{% if version %}\ - ## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }} -{% else %}\ - ## [unreleased] -{% endif %}\ -{% for group, commits in commits | group_by(attribute="group") %} - ### {{ group | upper_first }} - {% for commit in commits %} - - {% if commit.breaking %}[**breaking**] {% endif %}{{ commit.message | upper_first }}\ - {% endfor %} -{% endfor %}\n -""" -# remove the leading and trailing whitespace from the template -trim = true -# changelog footer -footer = """ - -""" - -[git] -# parse the commits based on https://www.conventionalcommits.org -conventional_commits = true -# filter out the commits that are not conventional -filter_unconventional = true -# regex for parsing and grouping commits -commit_parsers = [ - { message = "^feat", group = "Features"}, - { message = "^fix", group = "Bug Fixes"}, - { message = "^doc", group = "Documentation"}, - { message = "^perf", group = "Performance"}, - { message = "^refactor", group = "Refactor"}, - { message = "^style", group = "Styling"}, - { message = "^test", group = "Testing"}, - { message = "^chore\\(release\\): prepare for", skip = true}, - { message = "^chore", group = "Miscellaneous Tasks"}, - { body = ".*security", group = "Security"}, -] -# filter out the commits that are not matched by commit parsers -filter_commits = false -# glob pattern for matching git tags -tag_pattern = "v[0-9]*" -# regex for skipping tags -skip_tags = "v0.1.0-beta.1" -# regex for ignoring tags -ignore_tags = "" -# sort the tags chronologically -date_order = false -# sort the commits inside sections by oldest/newest order -sort_commits = "oldest" diff --git a/example_shadow/Cargo.toml b/example_shadow/Cargo.toml index 7434ac6..26e0719 100644 --- a/example_shadow/Cargo.toml +++ b/example_shadow/Cargo.toml @@ -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 = "../" } diff --git a/example_shadow/build.rs b/example_shadow/build.rs index 4a0dfc4..51e5960 100644 --- a/example_shadow/build.rs +++ b/example_shadow/build.rs @@ -1,3 +1,5 @@ fn main() -> shadow_rs::SdResult<()> { - shadow_rs::new() + // shadow_rs::new() + + shadow_rs::new_deny(Default::default()) } diff --git a/example_shadow/src/main.rs b/example_shadow/src/main.rs index 1cfb3f9..c0b79f2 100644 --- a/example_shadow/src/main.rs +++ b/example_shadow/src/main.rs @@ -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()) } diff --git a/src/gen_const.rs b/src/gen_const.rs index d6aed16..d433da4 100644 --- a/src/gen_const.rs +++ b/src/gen_const.rs @@ -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 { @@ -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 {{ + 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::*; diff --git a/src/lib.rs b/src/lib.rs index 8bb3017..aec78d2 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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}; @@ -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`. @@ -549,6 +553,8 @@ impl Shadow { ); writeln!(&self.f, "{everything_define}")?; + writeln!(&self.f, "{}", cargo_metadata_fn(self))?; + Ok(()) } } From 65c56630da5d9d28d4dfe320c436eab6a69c23dc Mon Sep 17 00:00:00 2001 From: baoyachi Date: Fri, 6 Sep 2024 18:11:40 +0800 Subject: [PATCH 2/2] fix cargo clippy check --- src/lib.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index aec78d2..0feeef4 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -235,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 { BTreeSet::from([CARGO_METADATA]) } @@ -266,7 +268,9 @@ pub fn new_deny(deny_const: BTreeSet) -> 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 @@ -285,6 +289,7 @@ pub fn new_deny(deny_const: BTreeSet) -> SdResult<()> { /// } /// /// ``` +/// pub fn new_hook(f: F) -> SdResult<()> where F: FnOnce(&File) -> SdResult<()>,