Skip to content

Commit

Permalink
Added ability to disable output even if feature is enabled (#208)
Browse files Browse the repository at this point in the history
* Added ability to disable output even if feature
is enabled

* Fixed git output

* Skip warnings if output is disabled
  • Loading branch information
CraZySacX committed May 20, 2023
1 parent e802dab commit 5cf8f34
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 29 deletions.
2 changes: 1 addition & 1 deletion vergen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "MIT OR Apache-2.0"
name = "vergen"
readme = "README.md"
repository = "https://github.com/rustyhorde/vergen"
version = "8.1.3"
version = "8.2.0"

[package.metadata.cargo-all-features]
denylist = [
Expand Down
231 changes: 203 additions & 28 deletions vergen/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ impl Emitter {
fn add_build_entries(&mut self, builder: &EmitBuilder) -> Result<()> {
let idem = builder.idempotent;
let fail_on_error = builder.fail_on_error;
let mut empty = BTreeMap::new();
let cargo_rustc_env_map = if builder.disable_build {
&mut empty
} else {
&mut self.cargo_rustc_env_map
};
builder
.add_build_map_entries(idem, &mut self.cargo_rustc_env_map, &mut self.warnings)
.add_build_map_entries(idem, cargo_rustc_env_map, &mut self.warnings)
.or_else(|e| {
builder.add_build_default(
e,
fail_on_error,
&mut self.cargo_rustc_env_map,
&mut self.warnings,
)
builder.add_build_default(e, fail_on_error, cargo_rustc_env_map, &mut self.warnings)
})
}

Expand All @@ -104,15 +105,16 @@ impl Emitter {
#[cfg(feature = "cargo")]
fn add_cargo_entries(&mut self, builder: &EmitBuilder) -> Result<()> {
let fail_on_error = builder.fail_on_error;
let mut empty = BTreeMap::new();
let cargo_rustc_env_map = if builder.disable_cargo {
&mut empty
} else {
&mut self.cargo_rustc_env_map
};
builder
.add_cargo_map_entries(&mut self.cargo_rustc_env_map)
.add_cargo_map_entries(cargo_rustc_env_map)
.or_else(|e| {
builder.add_cargo_default(
e,
fail_on_error,
&mut self.cargo_rustc_env_map,
&mut self.warnings,
)
builder.add_cargo_default(e, fail_on_error, cargo_rustc_env_map, &mut self.warnings)
})
}

Expand All @@ -133,22 +135,38 @@ impl Emitter {
fn add_git_entries(&mut self, builder: &EmitBuilder, repo_path: Option<PathBuf>) -> Result<()> {
let idem = builder.idempotent;
let fail_on_error = builder.fail_on_error;
let mut empty_cargo_rustc_env_map = BTreeMap::new();
let mut empty_rerun_if_changed = vec![];
let mut empty_warnings = vec![];
let (cargo_rustc_env_map, rerun_if_changed, warnings) = if builder.disable_git {
(
&mut empty_cargo_rustc_env_map,
&mut empty_rerun_if_changed,
&mut empty_warnings,
)
} else {
(
&mut self.cargo_rustc_env_map,
&mut self.rerun_if_changed,
&mut self.warnings,
)
};
builder
.add_git_map_entries(
repo_path,
idem,
&mut self.cargo_rustc_env_map,
&mut self.warnings,
&mut self.rerun_if_changed,
cargo_rustc_env_map,
warnings,
rerun_if_changed,
)
.or_else(|e| {
self.failed = true;
builder.add_git_default(
e,
fail_on_error,
&mut self.cargo_rustc_env_map,
&mut self.warnings,
&mut self.rerun_if_changed,
cargo_rustc_env_map,
warnings,
rerun_if_changed,
)
})
}
Expand All @@ -169,15 +187,16 @@ impl Emitter {
#[cfg(feature = "rustc")]
fn add_rustc_entries(&mut self, builder: &EmitBuilder) -> Result<()> {
let fail_on_error = builder.fail_on_error;
let mut empty = BTreeMap::new();
let cargo_rustc_env_map = if builder.disable_rustc {
&mut empty
} else {
&mut self.cargo_rustc_env_map
};
builder
.add_rustc_map_entries(&mut self.cargo_rustc_env_map, &mut self.warnings)
.add_rustc_map_entries(cargo_rustc_env_map, &mut self.warnings)
.or_else(|e| {
builder.add_rustc_default(
e,
fail_on_error,
&mut self.cargo_rustc_env_map,
&mut self.warnings,
)
builder.add_rustc_default(e, fail_on_error, cargo_rustc_env_map, &mut self.warnings)
})
}

Expand All @@ -194,7 +213,13 @@ impl Emitter {
#[cfg(feature = "si")]
fn add_si_entries(&mut self, builder: &EmitBuilder) {
let idem = builder.idempotent;
builder.add_sysinfo_map_entries(idem, &mut self.cargo_rustc_env_map, &mut self.warnings);
let mut empty = BTreeMap::new();
let cargo_rustc_env_map = if builder.disable_sysinfo {
&mut empty
} else {
&mut self.cargo_rustc_env_map
};
builder.add_sysinfo_map_entries(idem, cargo_rustc_env_map, &mut self.warnings);
}

#[cfg(not(feature = "si"))]
Expand Down Expand Up @@ -268,22 +293,36 @@ impl Emitter {
/// Build the `vergen` configuration to enable specific cargo instruction
/// output
#[derive(Clone, Copy, Debug)]
#[allow(clippy::struct_excessive_bools)]
pub struct EmitBuilder {
idempotent: bool,
fail_on_error: bool,
quiet: bool,
#[cfg(feature = "build")]
disable_build: bool,
#[cfg(feature = "build")]
pub(crate) build_config: BuildConfig,
#[cfg(feature = "cargo")]
disable_cargo: bool,
#[cfg(feature = "cargo")]
pub(crate) cargo_config: CargoConfig,
#[cfg(all(
feature = "git",
any(feature = "git2", feature = "gitcl", feature = "gix")
))]
disable_git: bool,
#[cfg(all(
feature = "git",
any(feature = "git2", feature = "gitcl", feature = "gix")
))]
pub(crate) git_config: GitConfig,
#[cfg(feature = "rustc")]
disable_rustc: bool,
#[cfg(feature = "rustc")]
pub(crate) rustc_config: RustcConfig,
#[cfg(feature = "si")]
disable_sysinfo: bool,
#[cfg(feature = "si")]
pub(crate) sysinfo_config: SysinfoConfig,
}

Expand All @@ -297,17 +336,30 @@ impl EmitBuilder {
fail_on_error: false,
quiet: false,
#[cfg(feature = "build")]
disable_build: false,
#[cfg(feature = "build")]
build_config: BuildConfig::default(),
#[cfg(feature = "cargo")]
disable_cargo: false,
#[cfg(feature = "cargo")]
cargo_config: CargoConfig::default(),
#[cfg(all(
feature = "git",
any(feature = "git2", feature = "gitcl", feature = "gix")
))]
disable_git: false,
#[cfg(all(
feature = "git",
any(feature = "git2", feature = "gitcl", feature = "gix")
))]
git_config: GitConfig::default(),
#[cfg(feature = "rustc")]
disable_rustc: false,
#[cfg(feature = "rustc")]
rustc_config: RustcConfig::default(),
#[cfg(feature = "si")]
disable_sysinfo: false,
#[cfg(feature = "si")]
sysinfo_config: SysinfoConfig::default(),
}
}
Expand Down Expand Up @@ -422,6 +474,129 @@ EmitBuilder::builder().quiet().all_build().emit()?;
self
}

/// Disable the build output, even when the build feature is enabled.
///
/// # Example
///
/// ```
/// # use anyhow::Result;
/// # use vergen::EmitBuilder;
/// #
/// # fn main() -> Result<()> {
#[cfg_attr(
feature = "build",
doc = r##"
EmitBuilder::builder().all_build().disable_build().emit()?;
"##
)]
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "build")]
pub fn disable_build(&mut self) -> &mut Self {
self.disable_build = true;
self
}

/// Disable the cargo output, even when the cargo feature is enabled.
///
/// # Example
///
/// ```
/// # use anyhow::Result;
/// # use vergen::EmitBuilder;
/// #
/// # fn main() -> Result<()> {
#[cfg_attr(
feature = "cargo",
doc = r##"
EmitBuilder::builder().all_cargo().disable_cargo().emit()?;
"##
)]
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "cargo")]
pub fn disable_cargo(&mut self) -> &mut Self {
self.disable_cargo = true;
self
}

/// Disable the git output, even when the git feature is enabled.
///
/// # Example
///
/// ```
/// # use anyhow::Result;
/// # use vergen::EmitBuilder;
/// #
/// # fn main() -> Result<()> {
#[cfg_attr(
feature = "git",
doc = r##"
EmitBuilder::builder().all_git().disable_git().emit()?;
"##
)]
/// # Ok(())
/// # }
/// ```
#[cfg(all(
feature = "git",
any(feature = "git2", feature = "gitcl", feature = "gix")
))]
pub fn disable_git(&mut self) -> &mut Self {
self.disable_git = true;
self
}

/// Disable the rustc output, even when the rustc feature is enabled.
///
/// # Example
///
/// ```
/// # use anyhow::Result;
/// # use vergen::EmitBuilder;
/// #
/// # fn main() -> Result<()> {
#[cfg_attr(
feature = "rustc",
doc = r##"
EmitBuilder::builder().all_rustc().disable_rustc().emit()?;
"##
)]
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "rustc")]
pub fn disable_rustc(&mut self) -> &mut Self {
self.disable_rustc = true;
self
}

/// Disable the sysinfo output, even when the sysinfo feature is enabled.
///
/// # Example
///
/// ```
/// # use anyhow::Result;
/// # use vergen::EmitBuilder;
/// #
/// # fn main() -> Result<()> {
#[cfg_attr(
feature = "si",
doc = r##"
EmitBuilder::builder().all_sysinfo().disable_sysinfo().emit()?;
"##
)]
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "si")]
pub fn disable_sysinfo(&mut self) -> &mut Self {
self.disable_sysinfo = true;
self
}

/// Emit cargo instructions from your build script
///
/// - Will emit [`cargo:rustc-env=VAR=VALUE`](https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-envvarvalue) for each feature you have enabled.
Expand Down
15 changes: 15 additions & 0 deletions vergen/tests/build_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ cargo:rerun-if-env-changed=VERGEN_IDEMPOTENT
cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH
"#;

const DISABLED_OUTPUT: &str = r#""#;

const SOURCE_DATE_EPOCH_IDEM_OUTPUT: &str = r#"cargo:rustc-env=VERGEN_BUILD_DATE=2022-12-23
cargo:rustc-env=VERGEN_BUILD_TIMESTAMP=2022-12-23T15:29:20.000000000Z
cargo:rerun-if-changed=build.rs
Expand All @@ -58,6 +60,19 @@ cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH
Ok(())
}

#[test]
#[serial_test::serial]
fn build_disabled_output() -> Result<()> {
let mut stdout_buf = vec![];
EmitBuilder::builder()
.all_build()
.disable_build()
.emit_to(&mut stdout_buf)?;
let output = String::from_utf8_lossy(&stdout_buf);
assert_eq!(DISABLED_OUTPUT, output);
Ok(())
}

#[test]
#[serial_test::serial]
fn build_all_idempotent_output() -> Result<()> {
Expand Down
17 changes: 17 additions & 0 deletions vergen/tests/cargo_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ mod test_build {
env::remove_var("TARGET");
}

const DISABLED_OUTPUT: &str = r#""#;

#[test]
#[serial_test::serial]
fn cargo_all_output() -> Result<()> {
Expand All @@ -57,6 +59,21 @@ mod test_build {
Ok(())
}

#[test]
#[serial_test::serial]
fn cargo_disabled_output() -> Result<()> {
setup();
let mut stdout_buf = vec![];
EmitBuilder::builder()
.all_cargo()
.disable_cargo()
.emit_to(&mut stdout_buf)?;
let output = String::from_utf8_lossy(&stdout_buf);
assert_eq!(DISABLED_OUTPUT, output);
teardown();
Ok(())
}

#[test]
#[serial_test::serial]
fn cargo_all_idempotent_output() -> Result<()> {
Expand Down
Loading

0 comments on commit 5cf8f34

Please sign in to comment.