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

Replace ConstantsFlags with Config #51

Merged
merged 10 commits into from Feb 26, 2021
1 change: 1 addition & 0 deletions .clippy.toml
@@ -0,0 +1 @@
max-struct-bools = 6
28 changes: 13 additions & 15 deletions README.md
Expand Up @@ -13,37 +13,35 @@ Generate build, git, and rustc related 'cargo:rustc-env' instructions via 'build
See the documentation at [docs.rs](https://docs.rs/vergen) for example usage

## Release 4 Breaking Changes
* The main entry point for use has changed from `generate_cargo_keys` to `gen`
* The main entry point for use has changed from `generate_cargo_keys` to `vergen`
* There are now 4 features that allow you to control what instructions can be generated (`build`, `cargo`, `git`, and `rustc`).
* The `build` feature enables the `VERGEN_BUILD_*` instructions.
* The `git` feature enables the `VERGEN_GIT_*` instructions and the `cargo:rerun-if-changed` instructions.
* The `rustc` feature enables the `VERGEN_RUSTC_*` instructions.
* The `cargo` feature enables the `VERGEN_CARGO_*` instructions.
* By default, all features are enabled.
* You can build with all features disabled, which basically make the `gen` function a no-op.
* You can still use `ConstantsFlags` to toggle individual cargo instructions.
* You can build with all features disabled, which basically make the `vergen` function a no-op.
* You can still use `ConstantsFlags` with the `gen` function to toggle individual cargo instructions, but it has been deprecated.
* The generated instructions have been normalized. Therefore, you may need to update what env variable you are referring to in code. I've included a list below of the full set of instructions that can be generated for reference.

```text, no_run
cargo:rustc-env=VERGEN_BUILD_DATE=2021-02-12
cargo:rustc-env=VERGEN_BUILD_TIMESTAMP=2021-02-12T01:54:15.134750+00:00
cargo:rustc-env=VERGEN_GIT_BRANCH=feature/git2
cargo:rustc-env=VERGEN_GIT_COMMIT_DATE=2021-02-11T20:05:53-05:00
cargo:rustc-env=VERGEN_GIT_SEMVER=v3.2.0-86-g95fc0f5
cargo:rustc-env=VERGEN_GIT_SEMVER_LIGHTWEIGHT=blah-33-g95fc0f5
cargo:rustc-env=VERGEN_GIT_SHA=95fc0f5d066710f16e0c23ce3239d6e040abca0d
cargo:rustc-env=VERGEN_GIT_SHA_SHORT=95fc0f5
cargo:rustc-env=VERGEN_BUILD_TIMESTAMP=2021-02-25T23:28:39.493201+00:00
cargo:rustc-env=VERGEN_BUILD_SEMVER=4.1.0
cargo:rustc-env=VERGEN_GIT_BRANCH=feature/datetime-toggles
cargo:rustc-env=VERGEN_GIT_COMMIT_TIMESTAMP=2021-02-24T20:55:21+00:00
cargo:rustc-env=VERGEN_GIT_SEMVER=4.1.0-2-gf49246c
cargo:rustc-env=VERGEN_GIT_SHA=f49246ce334567bff9f950bfd0f3078184a2738a
cargo:rustc-env=VERGEN_RUSTC_CHANNEL=nightly
cargo:rustc-env=VERGEN_RUSTC_COMMIT_DATE=2021-02-10
cargo:rustc-env=VERGEN_RUSTC_COMMIT_HASH=07194ffcd25b0871ce560b9f702e52db27ac9f77
cargo:rustc-env=VERGEN_RUSTC_COMMIT_DATE=2021-02-24
cargo:rustc-env=VERGEN_RUSTC_COMMIT_HASH=a8486b64b0c87dabd045453b6c81500015d122d6
cargo:rustc-env=VERGEN_RUSTC_HOST_TRIPLE=x86_64-apple-darwin
cargo:rustc-env=VERGEN_RUSTC_LLVM_VERSION=11.0
cargo:rustc-env=VERGEN_RUSTC_SEMVER=1.52.0-nightly
cargo:rustc-env=VERGEN_CARGO_TARGET_TRIPLE=x86_64-unknown-linux-gnu
cargo:rustc-env=VERGEN_CARGO_PROFILE=debug
cargo:rustc-env=VERGEN_CARGO_FEATURES=git,build
cargo:rerun-if-changed=/Users/yoda/projects/rust-lang/vergen/.git/HEAD
cargo:rerun-if-changed=/Users/yoda/projects/rust-lang/vergen/.git/refs/heads/feature/git2
cargo:rerun-if-changed=/Users/kon8116/projects/rust-lang/vergen/.git/HEAD
cargo:rerun-if-changed=/Users/kon8116/projects/rust-lang/vergen/.git/refs/heads/feature/datetime-toggles
```

* Under the hood, the `Command`s used for git have been removed in lieu of using the `git2` library directly.
Expand Down
202 changes: 193 additions & 9 deletions src/config.rs
Expand Up @@ -8,16 +8,27 @@

//! `vergen` configuration

#[cfg(feature = "build")]
use crate::feature::Build;
#[cfg(feature = "cargo")]
use crate::feature::Cargo;
#[cfg(feature = "git")]
use crate::feature::Git;
#[cfg(feature = "rustc")]
use crate::feature::Rustc;
use crate::{
constants::{
ConstantsFlags, BUILD_DATE_NAME, BUILD_TIMESTAMP_NAME, CARGO_FEATURES, CARGO_PROFILE,
CARGO_TARGET_TRIPLE, GIT_BRANCH_NAME, GIT_COMMIT_DATE_NAME, GIT_SEMVER_NAME,
GIT_SEMVER_TAGS_NAME, GIT_SHA_NAME, GIT_SHA_SHORT_NAME, RUSTC_CHANNEL_NAME,
RUSTC_COMMIT_DATE, RUSTC_COMMIT_HASH, RUSTC_HOST_TRIPLE_NAME, RUSTC_LLVM_VERSION,
RUSTC_SEMVER_NAME,
ConstantsFlags, BUILD_DATE_NAME, BUILD_SEMVER_NAME, BUILD_TIMESTAMP_NAME, BUILD_TIME_NAME,
CARGO_FEATURES, CARGO_PROFILE, CARGO_TARGET_TRIPLE, GIT_BRANCH_NAME, GIT_COMMIT_DATE_NAME,
GIT_COMMIT_TIMESTAMP_NAME, GIT_COMMIT_TIME_NAME, GIT_SEMVER_NAME, GIT_SEMVER_TAGS_NAME,
GIT_SHA_NAME, GIT_SHA_SHORT_NAME, RUSTC_CHANNEL_NAME, RUSTC_COMMIT_DATE, RUSTC_COMMIT_HASH,
RUSTC_HOST_TRIPLE_NAME, RUSTC_LLVM_VERSION, RUSTC_SEMVER_NAME,
},
error::Result,
feature::{add_build_config, add_cargo_config, add_git_config, add_rustc_config},
feature::{
add_build_config, add_cargo_config, add_git_config, add_rustc_config, configure_build,
configure_cargo, configure_git, configure_rustc,
},
};
use enum_iterator::IntoEnumIterator;
use getset::{Getters, MutGetters};
Expand All @@ -26,17 +37,116 @@ use std::{
path::{Path, PathBuf},
};

/// Configure `vergen` to produce the `cargo:` instructions you need
///
/// * See [`Build`](crate::Build) for details on `VERGEN_BUILD_*` instruction configuration
/// * See [`Cargo`](crate::Cargo) for details on `VERGEN_CARGO_*` instruction configuration
/// * See [`Git`](crate::Git) for details on `VERGEN_GIT_*` instruction configuration
/// * See [`Rustc`](crate::Rustc) for details on `VERGEN_RUSTC_*` instruction configuration
///
/// # Example
///
/// ```
/// use vergen::Config;
#[cfg_attr(feature = "git", doc = r##"use vergen::TimeZone;"##)]
///
/// let mut config = Config::default();
#[cfg_attr(
feature = "build",
doc = r##"
// Turn off the build semver instruction
*config.build_mut().semver_mut() = false;
"##
)]
#[cfg_attr(
feature = "git",
doc = r##"
// Change the commit timestamp timezone to local
*config.git_mut().commit_timestamp_timezone_mut() = TimeZone::Local;
"##
)]
#[cfg_attr(
feature = "rustc",
doc = r##"
// Turn off the LLVM version instruction
*config.rustc_mut().llvm_version_mut() = false;
"##
)]
#[cfg_attr(
feature = "cargo",
doc = r##"
// Turn off the cargo profile instruction
*config.cargo_mut().profile_mut() = false;
"##
)]
/// ```
#[derive(Clone, Copy, Debug, Getters, MutGetters)]
#[getset(get = "pub(crate)", get_mut = "pub")]
pub struct Instructions {
/// Use this to modify the [`Build`] feature configuration.
#[cfg(feature = "build")]
build: Build,
/// Use this to modify the [`Cargo`] feature configuration.
#[cfg(feature = "cargo")]
cargo: Cargo,
/// Use this to modify the [`Git`] feature configuration.
#[cfg(feature = "git")]
git: Git,
/// Use this to modify the [`Rustc`] feature configuration.
#[cfg(feature = "rustc")]
rustc: Rustc,
}

impl Default for Instructions {
fn default() -> Self {
Self {
#[cfg(feature = "build")]
build: Build::default(),
#[cfg(feature = "cargo")]
cargo: Cargo::default(),
#[cfg(feature = "git")]
git: Git::default(),
#[cfg(feature = "rustc")]
rustc: Rustc::default(),
}
}
}

impl Instructions {
pub(crate) fn config<T>(self, repo_path: Option<T>) -> Result<Config>
where
T: AsRef<Path>,
{
let mut config = Config::default();

configure_build(self, &mut config);
configure_git(self, repo_path, &mut config)?;
configure_rustc(self, &mut config)?;
configure_cargo(self, &mut config);

Ok(config)
}
}

/// Build information keys.
#[derive(Clone, Copy, Debug, IntoEnumIterator, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub(crate) enum VergenKey {
/// The build date. (VERGEN_BUILD_DATE)
BuildDate,
/// The build time. (VERGEN_BUILD_TIME)
BuildTime,
/// The build timestamp. (VERGEN_BUILD_TIMESTAMP)
BuildTimestamp,
/// The build semver. (VERGEN_BUILD_SEMVER)
BuildSemver,
/// The current working branch name (VERGEN_BRANCH)
Branch,
/// The commit date. (VERGEN_COMMIT_DATE).
/// The commit date. (VERGEN_COMMIT_DATE)
CommitDate,
/// The commit time. (VERGEN_COMMIT_TIME)
CommitTime,
/// The commit timestamp. (VERGEN_COMMIT_TIMESTAMP)
CommitTimestamp,
/// The semver version from the last git tag. (VERGEN_SEMVER)
Semver,
/// The semver version from the last git tag, including lightweight.
Expand Down Expand Up @@ -71,9 +181,13 @@ impl VergenKey {
pub(crate) fn name(self) -> &'static str {
match self {
VergenKey::BuildDate => BUILD_DATE_NAME,
VergenKey::BuildTime => BUILD_TIME_NAME,
VergenKey::BuildTimestamp => BUILD_TIMESTAMP_NAME,
VergenKey::BuildSemver => BUILD_SEMVER_NAME,
VergenKey::Branch => GIT_BRANCH_NAME,
VergenKey::CommitDate => GIT_COMMIT_DATE_NAME,
VergenKey::CommitTime => GIT_COMMIT_TIME_NAME,
VergenKey::CommitTimestamp => GIT_COMMIT_TIMESTAMP_NAME,
VergenKey::Semver => GIT_SEMVER_NAME,
VergenKey::SemverLightweight => GIT_SEMVER_TAGS_NAME,
VergenKey::Sha => GIT_SHA_NAME,
Expand Down Expand Up @@ -128,10 +242,80 @@ impl Config {

#[cfg(test)]
mod test {
use super::Config;
use super::{Config, Instructions};

#[test]
fn default_works() {
fn default_config_works() {
assert!(!Config::default().cfg_map().is_empty());
}

#[cfg(feature = "build")]
fn check_build_config(instructions: &Instructions) {
use crate::{TimeZone, TimestampKind};

let config = instructions.build();
assert!(config.has_enabled());
assert!(config.timestamp());
assert_eq!(*config.timezone(), TimeZone::Utc);
assert_eq!(*config.kind(), TimestampKind::Timestamp);
assert!(config.semver());
}

#[cfg(not(feature = "build"))]
fn check_build_config(_instructions: &Instructions) {}

#[cfg(feature = "cargo")]
fn check_cargo_config(instructions: &Instructions) {
let config = instructions.cargo();
assert!(config.has_enabled());
assert!(config.features());
assert!(config.profile());
assert!(config.target_triple());
}

#[cfg(not(feature = "cargo"))]
fn check_cargo_config(_instructions: &Instructions) {}

#[cfg(feature = "git")]
fn check_git_config(instructions: &Instructions) {
use crate::{SemverKind, ShaKind, TimeZone, TimestampKind};

let config = instructions.git();
assert!(config.has_enabled());
assert!(config.branch());
assert!(config.commit_timestamp());
assert_eq!(*config.commit_timestamp_timezone(), TimeZone::Utc);
assert_eq!(*config.commit_timestamp_kind(), TimestampKind::Timestamp);
assert!(config.rerun_on_head_change());
assert!(config.semver());
assert_eq!(*config.semver_kind(), SemverKind::Normal);
assert!(config.sha());
assert_eq!(*config.sha_kind(), ShaKind::Normal);
}

#[cfg(not(feature = "git"))]
fn check_git_config(_instructions: &Instructions) {}

#[cfg(feature = "rustc")]
fn check_rustc_config(instructions: &Instructions) {
let config = instructions.rustc();
assert!(config.has_enabled());
assert!(config.channel());
assert!(config.commit_date());
assert!(config.host_triple());
assert!(config.llvm_version());
assert!(config.sha());
}

#[cfg(not(feature = "rustc"))]
fn check_rustc_config(_instructions: &Instructions) {}

#[test]
fn default_instructions() {
let default = Instructions::default();
check_build_config(&default);
check_cargo_config(&default);
check_git_config(&default);
check_rustc_config(&default);
}
}
9 changes: 9 additions & 0 deletions src/constants.rs
Expand Up @@ -11,6 +11,11 @@
use bitflags::bitflags;

bitflags!(
/// **DEPRECATED** - [`ConstantsFlags`] has been deprecated in favor of [`Config`](crate::config::Instructions).
///
/// Please make the switch to that instead. [`ConstantsFlags`] will be removed in
/// version 5.
///
/// Flags used to toggle individual `cargo:` instruction generation
///
/// Use these to toggle off instructions you don't wish to generate
Expand Down Expand Up @@ -178,10 +183,14 @@ bitflags!(
// Build Constants
pub(crate) const BUILD_TIMESTAMP_NAME: &str = "VERGEN_BUILD_TIMESTAMP";
pub(crate) const BUILD_DATE_NAME: &str = "VERGEN_BUILD_DATE";
pub(crate) const BUILD_TIME_NAME: &str = "VERGEN_BUILD_TIME";
pub(crate) const BUILD_SEMVER_NAME: &str = "VERGEN_BUILD_SEMVER";

// git Constants
pub(crate) const GIT_BRANCH_NAME: &str = "VERGEN_GIT_BRANCH";
pub(crate) const GIT_COMMIT_DATE_NAME: &str = "VERGEN_GIT_COMMIT_DATE";
pub(crate) const GIT_COMMIT_TIME_NAME: &str = "VERGEN_GIT_COMMIT_TIME";
pub(crate) const GIT_COMMIT_TIMESTAMP_NAME: &str = "VERGEN_GIT_COMMIT_TIMESTAMP";
pub(crate) const GIT_SEMVER_NAME: &str = "VERGEN_GIT_SEMVER";
pub(crate) const GIT_SEMVER_TAGS_NAME: &str = "VERGEN_GIT_SEMVER_LIGHTWEIGHT";
pub(crate) const GIT_SHA_NAME: &str = "VERGEN_GIT_SHA";
Expand Down