Skip to content

Commit

Permalink
remove deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi committed Feb 23, 2025
1 parent 15441c7 commit f94b542
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 180 deletions.
23 changes: 0 additions & 23 deletions src/gen_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,6 @@ build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST
);"##,
);

const CLAP_VERSION_BRANCH_CONST: (&str, &str) = (
r#"#[deprecated = "Replaced with `CLAP_LONG_VERSION`"]"#,
r##"pub const CLAP_VERSION:&str = shadow_rs::formatcp!(r#"{}
branch:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##,
);

const CLAP_VERSION_TAG_CONST: (&str, &str) = (
r#"#[deprecated = "Replaced with `CLAP_LONG_VERSION`"]"#,
r##"pub const CLAP_VERSION:&str = shadow_rs::formatcp!(r#"{}
tag:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##,
);

const CLAP_LONG_VERSION_BRANCH_CONST: (&str, &str) = (
r#"/// A long version string describing the project.
/// The version string contains the package version, branch, commit hash, build time, and build environment on separate lines.
Expand All @@ -87,8 +67,6 @@ build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST

gen_const!(version_branch_const, VERSION_BRANCH_CONST);
gen_const!(version_tag_const, VERSION_TAG_CONST);
gen_const!(clap_version_branch_const, CLAP_VERSION_BRANCH_CONST);
gen_const!(clap_version_tag_const, CLAP_VERSION_TAG_CONST);
gen_const!(
clap_long_version_branch_const,
CLAP_LONG_VERSION_BRANCH_CONST
Expand Down Expand Up @@ -134,7 +112,6 @@ mod tests {
#[test]
fn test_version_fn() {
assert!(version_tag_const().contains(VERSION_TAG_CONST.0));
assert!(clap_version_branch_const().contains(CLAP_VERSION_BRANCH_CONST.1));
assert!(clap_long_version_branch_const().contains(CLAP_LONG_VERSION_BRANCH_CONST.1));
}
}
162 changes: 5 additions & 157 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
//! }
//! ```
//!
//! If you want to exclude some build constants, you can use [`new_deny`] instead of [`new`].
//!
//! ### 3) Integrate Shadow
//! In your main Rust file (usually `main.rs` or `lib.rs`), add this:
Expand Down Expand Up @@ -176,13 +175,11 @@ use std::path::Path;

use crate::gen_const::{
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,
version_branch_const, version_tag_const, BUILD_CONST_CLAP_LONG_VERSION, BUILD_CONST_VERSION,
};
pub use err::{SdResult, ShadowError};

pub use crate::build::{BuildPattern, ShadowBuilder};
use crate::hook::HookExt;
pub use {build::ShadowConst, env::*, git::*};

pub trait Format {
Expand Down Expand Up @@ -219,27 +216,6 @@ macro_rules! shadow {
};
}

/// Generates build information for the current project.
/// This function must be called from `build.rs`.
///
/// # Example
///
/// ```ignore
/// // build.rs
///
/// fn main() -> shadow_rs::SdResult<()> {
/// shadow_rs::new()
/// }
/// ```
#[deprecated(
since = "0.37.0",
note = "Please use [`ShadowBuilder::builder`] instead"
)]
pub fn new() -> SdResult<()> {
ShadowBuilder::builder().build()?;
Ok(())
}

/// Since [cargo metadata](https://crates.io/crates/cargo_metadata) details about workspace
/// 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.
Expand All @@ -252,72 +228,6 @@ pub fn default_deny() -> BTreeSet<ShadowConst> {
BTreeSet::from([CARGO_METADATA])
}

/// Identical to [`new`], but additionally accepts a build output denylist.
/// This list determines constants to be excluded in the build output.
///
/// Note that not all constants can be excluded independently, since some constants depend on others.
/// See [`ShadowConst`] for a list of constants that can be excluded.
///
/// # Example
///
/// ```ignore
/// // build.rs
///
/// use std::collections::BTreeSet;
///
/// fn main() -> shadow_rs::SdResult<()> {
/// let mut deny = BTreeSet::new();
/// deny.insert(shadow_rs::CARGO_TREE);
/// shadow_rs::new_deny(deny)
/// }
/// ```
#[deprecated(
since = "0.37.0",
note = "Please use [`ShadowBuilder::builder`] instead"
)]
pub fn new_deny(deny_const: BTreeSet<ShadowConst>) -> SdResult<()> {
ShadowBuilder::builder().deny_const(deny_const).build()?;
Ok(())
}

/// 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.
/// 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
///
/// ```ignore
/// // build.rs
///
/// fn main() -> shadow_rs::SdResult<()> {
/// shadow_rs::new_hook(append_write_const)
/// }
///
/// fn append_write_const(mut file: &File) -> SdResult<()> {
/// let foo: &str = r#"pub const foo: &str = "foo";"#;
/// writeln!(file, "{}", foo)?;
/// Ok(())
/// }
///
/// ```
///
#[deprecated(
since = "0.37.0",
note = "Please use [`ShadowBuilder::builder`] instead"
)]
pub fn new_hook<F>(f: F) -> SdResult<()>
where
F: HookExt,
{
ShadowBuilder::builder()
.deny_const(f.default_deny())
.hook(f.hook_inner())
.build()?;
Ok(())
}

/// Returns the contents of [`std::env::vars`] as an ordered map.
pub(crate) fn get_std_env() -> BTreeMap<String, String> {
let mut env_map = BTreeMap::new();
Expand Down Expand Up @@ -434,32 +344,6 @@ impl Shadow {
self.deny_const.contains(&deny_const)
}

/// Create a new [`Shadow`] configuration with a provided denylist.
/// The project source path and output file are automatically derived from Cargo build environment variables.
#[deprecated(
since = "0.37.0",
note = "Please use [`ShadowBuilder::builder`] instead"
)]
pub fn build(deny_const: BTreeSet<ShadowConst>) -> SdResult<Shadow> {
ShadowBuilder::builder().deny_const(deny_const).build()
}

#[deprecated(
since = "0.37.0",
note = "Please use [`ShadowBuilder::builder`] instead"
)]
pub fn build_with(
src_path: String,
out_path: String,
deny_const: BTreeSet<ShadowConst>,
) -> SdResult<Shadow> {
ShadowBuilder::builder()
.deny_const(deny_const)
.src_path(src_path)
.out_path(out_path)
.build()
}

fn build_inner(builder: ShadowBuilder) -> SdResult<Shadow> {
let out_path = builder.get_out_path()?;
let src_path = builder.get_src_path()?;
Expand Down Expand Up @@ -529,29 +413,6 @@ impl Shadow {
Ok(())
}

/// Request Cargo to re-run the build script if any environment variable observed by this [`Shadow`] configuration changes.
#[deprecated(
since = "0.37.0",
note = "Please use [`ShadowBuilder::build_pattern`] instead"
)]
pub fn cargo_rerun_if_env_changed(&self) {
for k in self.std_env.keys() {
println!("cargo:rerun-if-env-changed={k}");
}
}

/// Request Cargo to re-run the build script if any of the specified environment variables change.
/// This function is not influenced by this [`Shadow`] configuration.
#[deprecated(
since = "0.37.0",
note = "Please use [`ShadowBuilder::build_pattern`] instead"
)]
pub fn cargo_rerun_env_inject(&self, env: &[&str]) {
for k in env {
println!("cargo:rerun-if-env-changed={}", *k);
}
}

fn gen_const(&mut self) -> SdResult<()> {
let out_dir = &self.out_path;
self.build_pattern.rerun_if(self.map.keys(), out_dir);
Expand Down Expand Up @@ -612,30 +473,17 @@ impl Shadow {
}

fn gen_version(&mut self) -> SdResult<Vec<&'static str>> {
let (ver_fn, clap_ver_fn, clap_long_ver_fn) = match self.map.get(TAG) {
None => (
version_branch_const(),
clap_version_branch_const(),
clap_long_version_branch_const(),
),
let (ver_fn, clap_long_ver_fn) = match self.map.get(TAG) {
None => (version_branch_const(), clap_long_version_branch_const()),
Some(tag) => {
if !tag.v.is_empty() {
(
version_tag_const(),
clap_version_tag_const(),
clap_long_version_tag_const(),
)
(version_tag_const(), clap_long_version_tag_const())
} else {
(
version_branch_const(),
clap_version_branch_const(),
clap_long_version_branch_const(),
)
(version_branch_const(), clap_long_version_branch_const())
}
}
};
writeln!(&self.f, "{ver_fn}\n")?;
writeln!(&self.f, "{clap_ver_fn}\n")?;
writeln!(&self.f, "{clap_long_ver_fn}\n")?;

Ok(vec![BUILD_CONST_VERSION, BUILD_CONST_CLAP_LONG_VERSION])
Expand Down

0 comments on commit f94b542

Please sign in to comment.