Skip to content

Commit

Permalink
Add sysinfo support (#58)
Browse files Browse the repository at this point in the history
* initial sysinfo impl

* Added some data to the sysinfo feature

* Fixed sysinfo issue on windows

* Fix broken tests

* fixed broken test on windows

* Added some tests and refactor for coverage

* coverage crap

* added name, brand, and freq to sysinfo

* Fixed lint error

* Fix rustc test on beta and stable

Co-authored-by: Jason Ozias <jason.ozias@kroger.com>
  • Loading branch information
CraZySacX and jasonozias committed Mar 25, 2021
1 parent ba5162a commit 61e560e
Show file tree
Hide file tree
Showing 8 changed files with 502 additions and 34 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ repository = "https://github.com/rustyhorde/vergen"
version = "5.0.1"

[features]
default = ["build", "cargo", "git", "rustc"]
default = ["build", "cargo", "git", "rustc", "si"]
build = ["chrono"]
cargo = []
git = ["chrono", "git2"]
rustc = ["rustc_version"]
si = ["sysinfo"]

[dependencies]
anyhow = "1"
Expand All @@ -26,6 +27,7 @@ enum-iterator = "0"
getset = "0"
git2 = { version = "0", optional = true, default-features = false }
rustc_version = { version = "0", optional = true }
sysinfo = { version = "0", optional = true }
thiserror = "1"

[build-dependencies]
Expand All @@ -35,7 +37,6 @@ rustversion = "1"
[dev-dependencies]
lazy_static = "1"
regex = "1"
rustversion = "1"
serial_test = "0"

[package.metadata.cargo-all-features]
Expand Down
67 changes: 65 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ use crate::feature::Cargo;
use crate::feature::Git;
#[cfg(feature = "rustc")]
use crate::feature::Rustc;
#[cfg(feature = "si")]
use crate::feature::Sysinfo;
use crate::{
constants::{
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,
RUSTC_HOST_TRIPLE_NAME, RUSTC_LLVM_VERSION, RUSTC_SEMVER_NAME, SYSINFO_CPU_BRAND,
SYSINFO_CPU_CORE_COUNT, SYSINFO_CPU_FREQUENCY, SYSINFO_CPU_NAME, SYSINFO_CPU_VENDOR,
SYSINFO_MEMORY, SYSINFO_NAME, SYSINFO_OS_VERSION, SYSINFO_USER,
},
feature::{
configure_build, configure_cargo, configure_git, configure_rustc, configure_sysinfo,
},
feature::{configure_build, configure_cargo, configure_git, configure_rustc},
};
use anyhow::Result;
use enum_iterator::IntoEnumIterator;
Expand All @@ -40,6 +46,7 @@ use std::{
/// * 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
/// * See [`Sysinfo`](crate::Sysinfo) for details on `VERGEN_SYSINFO_*` instruction configuration
///
/// # Example
///
Expand Down Expand Up @@ -76,6 +83,13 @@ use std::{
*config.cargo_mut().profile_mut() = false;
"##
)]
#[cfg_attr(
feature = "si",
doc = r##"
// Turn off the sysinfo name instruction
*config.sysinfo_mut().name_mut() = false;
"##
)]
/// ```
#[derive(Clone, Copy, Debug, Getters, MutGetters)]
#[getset(get = "pub(crate)", get_mut = "pub")]
Expand All @@ -92,6 +106,9 @@ pub struct Instructions {
/// Use this to modify the [`Rustc`] feature configuration.
#[cfg(feature = "rustc")]
rustc: Rustc,
/// Use this to modify the [`Sysinfo`] feature configuration.
#[cfg(feature = "si")]
sysinfo: Sysinfo,
}

impl Default for Instructions {
Expand All @@ -105,6 +122,8 @@ impl Default for Instructions {
git: Git::default(),
#[cfg(feature = "rustc")]
rustc: Rustc::default(),
#[cfg(feature = "si")]
sysinfo: Sysinfo::default(),
}
}
}
Expand All @@ -120,6 +139,7 @@ impl Instructions {
configure_git(self, repo_path, &mut config)?;
configure_rustc(self, &mut config)?;
configure_cargo(self, &mut config);
configure_sysinfo(self, &mut config)?;

Ok(config)
}
Expand Down Expand Up @@ -171,6 +191,24 @@ pub(crate) enum VergenKey {
CargoProfile,
/// The cargo features (VERGEN_CARGO_FEATURES)
CargoFeatures,
/// The sysinfo system name (VERGEN_SYSINFO_NAME)
SysinfoName,
/// The sysinfo os version (VERGEN_SYSINFO_OS_VERSION)
SysinfoOsVersion,
/// The sysinfo user name (VERGEN_SYSINFO_USER)
SysinfoUser,
/// The sysinfo total memory (VERGEN_SYSINFO_TOTAL_MEMORY)
SysinfoMemory,
/// The sysinfo cpu vendor (VERGEN_SYSINFO_CPU_VENDOR)
SysinfoCpuVendor,
/// The sysinfo cpu core count (VERGEN_SYSINFO_CPU_CORE_COUNT)
SysinfoCpuCoreCount,
/// The sysinfo cpu core count (VERGEN_SYSINFO_CPU_NAME)
SysinfoCpuName,
/// The sysinfo cpu core count (VERGEN_SYSINFO_CPU_BRAND)
SysinfoCpuBrand,
/// The sysinfo cpu core count (VERGEN_SYSINFO_CPU_FREQUENCY)
SysinfoCpuFrequency,
}

impl VergenKey {
Expand Down Expand Up @@ -198,6 +236,15 @@ impl VergenKey {
VergenKey::CargoTargetTriple => CARGO_TARGET_TRIPLE,
VergenKey::CargoProfile => CARGO_PROFILE,
VergenKey::CargoFeatures => CARGO_FEATURES,
VergenKey::SysinfoName => SYSINFO_NAME,
VergenKey::SysinfoOsVersion => SYSINFO_OS_VERSION,
VergenKey::SysinfoUser => SYSINFO_USER,
VergenKey::SysinfoMemory => SYSINFO_MEMORY,
VergenKey::SysinfoCpuVendor => SYSINFO_CPU_VENDOR,
VergenKey::SysinfoCpuCoreCount => SYSINFO_CPU_CORE_COUNT,
VergenKey::SysinfoCpuName => SYSINFO_CPU_NAME,
VergenKey::SysinfoCpuBrand => SYSINFO_CPU_BRAND,
VergenKey::SysinfoCpuFrequency => SYSINFO_CPU_FREQUENCY,
}
}
}
Expand Down Expand Up @@ -291,12 +338,28 @@ mod test {
#[cfg(not(feature = "rustc"))]
fn check_rustc_config(_instructions: &Instructions) {}

#[cfg(feature = "si")]
fn check_sysinfo_config(instructions: &Instructions) {
let config = instructions.sysinfo();
assert!(config.has_enabled());
assert!(config.name());
assert!(config.os_version());
assert!(config.user());
assert!(config.memory());
assert!(config.cpu_vendor());
assert!(config.cpu_core_count());
}

#[cfg(not(feature = "si"))]
fn check_sysinfo_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);
check_sysinfo_config(&default);
}
}
22 changes: 22 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ pub(crate) const CARGO_TARGET_TRIPLE: &str = "VERGEN_CARGO_TARGET_TRIPLE";
pub(crate) const CARGO_PROFILE: &str = "VERGEN_CARGO_PROFILE";
pub(crate) const CARGO_FEATURES: &str = "VERGEN_CARGO_FEATURES";

// sysinfo Constants
pub(crate) const SYSINFO_NAME: &str = "VERGEN_SYSINFO_NAME";
pub(crate) const SYSINFO_OS_VERSION: &str = "VERGEN_SYSINFO_OS_VERSION";
pub(crate) const SYSINFO_USER: &str = "VERGEN_SYSINFO_USER";
pub(crate) const SYSINFO_MEMORY: &str = "VERGEN_SYSINFO_TOTAL_MEMORY";
pub(crate) const SYSINFO_CPU_VENDOR: &str = "VERGEN_SYSINFO_CPU_VENDOR";
pub(crate) const SYSINFO_CPU_CORE_COUNT: &str = "VERGEN_SYSINFO_CPU_CORE_COUNT";
pub(crate) const SYSINFO_CPU_NAME: &str = "VERGEN_SYSINFO_CPU_NAME";
pub(crate) const SYSINFO_CPU_BRAND: &str = "VERGEN_SYSINFO_CPU_BRAND";
pub(crate) const SYSINFO_CPU_FREQUENCY: &str = "VERGEN_SYSINFO_CPU_FREQUENCY";

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -67,5 +78,16 @@ mod test {
assert_eq!(CARGO_TARGET_TRIPLE, "VERGEN_CARGO_TARGET_TRIPLE");
assert_eq!(CARGO_PROFILE, "VERGEN_CARGO_PROFILE");
assert_eq!(CARGO_FEATURES, "VERGEN_CARGO_FEATURES");

// sysinfo Constants
assert_eq!(SYSINFO_NAME, "VERGEN_SYSINFO_NAME");
assert_eq!(SYSINFO_OS_VERSION, "VERGEN_SYSINFO_OS_VERSION");
assert_eq!(SYSINFO_USER, "VERGEN_SYSINFO_USER");
assert_eq!(SYSINFO_MEMORY, "VERGEN_SYSINFO_TOTAL_MEMORY");
assert_eq!(SYSINFO_CPU_VENDOR, "VERGEN_SYSINFO_CPU_VENDOR");
assert_eq!(SYSINFO_CPU_CORE_COUNT, "VERGEN_SYSINFO_CPU_CORE_COUNT");
assert_eq!(SYSINFO_CPU_NAME, "VERGEN_SYSINFO_CPU_NAME");
assert_eq!(SYSINFO_CPU_BRAND, "VERGEN_SYSINFO_CPU_BRAND");
assert_eq!(SYSINFO_CPU_FREQUENCY, "VERGEN_SYSINFO_CPU_FREQUENCY");
}
}
18 changes: 18 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ pub(crate) enum Error {
/// An error getting the 'CARGO_PKG_VERSION' environment variable
#[error("{}: The 'CARGO_PKG_VERSION' environment variable may not be set: {}", ErrKind::Env, .0)]
Var(#[from] std::env::VarError),
/// An error getting the current pid
#[cfg(feature = "si")]
#[error(
"{}: Unable to determine the current process pid: {}",
ErrKind::Protocol,
msg
)]
Pid { msg: &'static str },
}

#[cfg(test)]
Expand Down Expand Up @@ -89,6 +97,16 @@ mod test {
assert_eq!("protocol: An error occurred in the \'git2\' library: failed to resolve path \'blah\': The system cannot find the file specified.\r\n; class=Os (2); code=NotFound (-3)", format!("{}", err));
}

#[cfg(feature = "si")]
#[test]
fn pid_error() {
let err: Error = Error::Pid { msg: "test" };
assert_eq!(
"protocol: Unable to determine the current process pid: test",
format!("{}", err)
);
}

#[test]
fn var_error() {
let res = env::var("yoda").map_err(|e| Error::from(e));
Expand Down
13 changes: 10 additions & 3 deletions src/feature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
feature = "build",
feature = "cargo",
feature = "git",
feature = "rustc"
feature = "rustc",
feature = "si",
))]
use {crate::config::VergenKey, std::collections::BTreeMap};

mod build;
mod cargo;
mod git;
mod rustc;
mod si;

pub(crate) use build::configure_build;
#[cfg(feature = "build")]
Expand All @@ -33,12 +35,16 @@ pub use git::{Git, SemverKind, ShaKind};
pub(crate) use rustc::configure_rustc;
#[cfg(feature = "rustc")]
pub use rustc::Rustc;
pub(crate) use si::configure_sysinfo;
#[cfg(feature = "si")]
pub use si::Sysinfo;

#[cfg(any(
feature = "build",
feature = "cargo",
feature = "git",
feature = "rustc"
feature = "rustc",
feature = "si",
))]
pub(crate) fn add_entry(
map: &mut BTreeMap<VergenKey, Option<String>>,
Expand Down Expand Up @@ -80,7 +86,8 @@ pub enum TimestampKind {
feature = "build",
feature = "cargo",
feature = "git",
feature = "rustc"
feature = "rustc",
feature = "si",
)
))]
mod test {
Expand Down
Loading

0 comments on commit 61e560e

Please sign in to comment.