Skip to content

Commit

Permalink
Custom build.rs path support, local time offset support, updated gix …
Browse files Browse the repository at this point in the history
…dep (#223)

* added support for specifying custom build.rs path, updated gix dep

* version bump for next release

* added custom build.rs test

* added support for local time offset output

* test using local, which fails on unix

* Fixed for linux/non-linux

* Added local test to git_output

* fixed linux test

* fixed non-linux tests

---------

Co-authored-by: Jason Ozias <[email protected]>
  • Loading branch information
CraZySacX and jasonozias authored Jul 1, 2023
1 parent c51c8d3 commit 022e804
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 28 deletions.
7 changes: 4 additions & 3 deletions 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:/rustyhorde/vergen"
version = "8.2.2"
version = "8.2.3"

[package.metadata.cargo-all-features]
denylist = [
Expand Down Expand Up @@ -41,19 +41,20 @@ si = ["sysinfo"]
[dependencies]
anyhow = "1.0.70"
git2-rs = { version = "0.17.1", package = "git2", optional = true, default-features = false }
gix = { version = "0.47.0", optional = true, default-features = false }
gix = { version = "0.48.0", optional = true, default-features = false }
rustc_version = { version = "0.4.0", optional = true }
sysinfo = { version = "0.29.0", optional = true, default-features = false }
time = { version = "0.3.20", features = [
"formatting",
"local-offset",
"parsing",
], optional = true }

[build-dependencies]
rustversion = "1.0.12"

[dev-dependencies]
gix = { version = "0.47.0", default-features = false, features = [
gix = { version = "0.48.0", default-features = false, features = [
"blocking-network-client",
] }
lazy_static = "1.4.0"
Expand Down
81 changes: 69 additions & 12 deletions vergen/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{
env,
io::{self, Write},
};

#[cfg(any(
feature = "build",
feature = "cargo",
Expand Down Expand Up @@ -230,11 +231,16 @@ impl Emitter {
)]
fn add_si_entries(&mut self, _builder: &EmitBuilder) {}

fn emit_output<T>(&self, quiet: bool, stdout: &mut T) -> Result<()>
fn emit_output<T>(
&self,
quiet: bool,
custom_buildrs: Option<&'static str>,
stdout: &mut T,
) -> Result<()>
where
T: Write,
{
self.emit_instructions(quiet, stdout)
self.emit_instructions(quiet, custom_buildrs, stdout)
}

#[cfg(not(any(
Expand All @@ -245,7 +251,12 @@ impl Emitter {
feature = "si"
)))]
#[allow(clippy::unused_self, clippy::unnecessary_wraps)]
fn emit_instructions<T>(&self, _quiet: bool, _stdout: &mut T) -> Result<()>
fn emit_instructions<T>(
&self,
_quiet: bool,
_custom_buildrs: Option<&'static str>,
_stdout: &mut T,
) -> Result<()>
where
T: Write,
{
Expand All @@ -259,7 +270,12 @@ impl Emitter {
feature = "rustc",
feature = "si"
))]
fn emit_instructions<T>(&self, quiet: bool, stdout: &mut T) -> Result<()>
fn emit_instructions<T>(
&self,
quiet: bool,
custom_buildrs: Option<&'static str>,
stdout: &mut T,
) -> Result<()>
where
T: Write,
{
Expand All @@ -282,7 +298,12 @@ impl Emitter {

// Emit the 'cargo:rerun-if-changed' instructions
if !self.cargo_rustc_env_map.is_empty() || !self.warnings.is_empty() {
writeln!(stdout, "cargo:rerun-if-changed=build.rs")?;
let buildrs = if let Some(path) = custom_buildrs {
path
} else {
"build.rs"
};
writeln!(stdout, "cargo:rerun-if-changed={buildrs}")?;
writeln!(stdout, "cargo:rerun-if-env-changed=VERGEN_IDEMPOTENT")?;
writeln!(stdout, "cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH")?;
}
Expand All @@ -298,6 +319,7 @@ pub struct EmitBuilder {
idempotent: bool,
fail_on_error: bool,
quiet: bool,
custom_buildrs: Option<&'static str>,
#[cfg(feature = "build")]
disable_build: bool,
#[cfg(feature = "build")]
Expand Down Expand Up @@ -335,6 +357,7 @@ impl EmitBuilder {
idempotent,
fail_on_error: false,
quiet: false,
custom_buildrs: None,
#[cfg(feature = "build")]
disable_build: false,
#[cfg(feature = "build")]
Expand Down Expand Up @@ -449,6 +472,33 @@ EmitBuilder::builder().fail_on_error().all_build().emit()?;
self
}

/// Set a custom build.rs path if you are using a non-standard path
///
/// By default `vergen` will use `build.rs` as the build path for the
/// `cargo:rerun-if-changed` emit. You can specify a custom `build.rs`
/// path here if you have changed this default
///
/// # Example
///
/// ```
/// # use anyhow::Result;
/// # use vergen::EmitBuilder;
/// #
/// # fn main() -> Result<()> {
#[cfg_attr(
feature = "build",
doc = r##"
EmitBuilder::builder().custom_build_rs("my/custom/build.rs").all_build().emit()?;
"##
)]
/// # Ok(())
/// # }
/// ```
pub fn custom_build_rs(&mut self, path: &'static str) -> &mut Self {
self.custom_buildrs = Some(path);
self
}

/// Enable the quiet feature
///
/// Suppress the emission of the `cargo:warning` instructions.
Expand Down Expand Up @@ -698,7 +748,7 @@ EmitBuilder::builder()
///
pub fn emit(self) -> Result<()> {
self.inner_emit(None)
.and_then(|x| x.emit_output(self.quiet, &mut io::stdout()))
.and_then(|x| x.emit_output(self.quiet, self.custom_buildrs, &mut io::stdout()))
}

/// Emit cargo instructions from your build script and set environment variables for use in `build.rs`
Expand Down Expand Up @@ -765,7 +815,10 @@ EmitBuilder::builder()
))]
pub fn emit_and_set(self) -> Result<()> {
self.inner_emit(None)
.and_then(|x| x.emit_output(self.quiet, &mut io::stdout()).map(|_| x))
.and_then(|x| {
x.emit_output(self.quiet, self.custom_buildrs, &mut io::stdout())
.map(|_| x)
})
.map(|x| {
for (k, v) in &x.cargo_rustc_env_map {
if env::var(k.name()).is_err() {
Expand All @@ -786,7 +839,7 @@ EmitBuilder::builder()
))]
pub fn emit_at(self, repo_path: PathBuf) -> Result<()> {
self.inner_emit(Some(repo_path))
.and_then(|x| x.emit_output(self.quiet, &mut io::stdout()))
.and_then(|x| x.emit_output(self.quiet, self.custom_buildrs, &mut io::stdout()))
}

#[cfg(all(
Expand Down Expand Up @@ -830,8 +883,10 @@ EmitBuilder::builder()
where
T: Write,
{
self.inner_emit(None)
.and_then(|x| x.emit_output(self.quiet, stdout).map(|_| x.failed))
self.inner_emit(None).and_then(|x| {
x.emit_output(self.quiet, self.custom_buildrs, stdout)
.map(|_| x.failed)
})
}

#[doc(hidden)]
Expand All @@ -848,8 +903,10 @@ EmitBuilder::builder()
where
T: Write,
{
self.inner_emit(path)
.and_then(|x| x.emit_output(self.quiet, stdout).map(|_| x.failed))
self.inner_emit(path).and_then(|x| {
x.emit_output(self.quiet, self.custom_buildrs, stdout)
.map(|_| x.failed)
})
}

fn inner_emit(self, path: Option<PathBuf>) -> Result<Emitter> {
Expand Down
15 changes: 14 additions & 1 deletion vergen/src/feature/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use time::{
pub(crate) struct Config {
pub(crate) build_date: bool,
pub(crate) build_timestamp: bool,
use_local: bool,
}

impl Config {
Expand Down Expand Up @@ -155,6 +156,12 @@ impl EmitBuilder {
self
}

/// Enable local offset date/timestamp output
pub fn use_local_build(&mut self) -> &mut Self {
self.build_config.use_local = true;
self
}

pub(crate) fn add_build_default(
&self,
e: Error,
Expand Down Expand Up @@ -198,7 +205,13 @@ impl EmitBuilder {
true,
OffsetDateTime::from_unix_timestamp(i64::from_str(&v)?)?,
),
Err(std::env::VarError::NotPresent) => (false, OffsetDateTime::now_utc()),
Err(std::env::VarError::NotPresent) => {
if self.build_config.use_local {
(false, OffsetDateTime::now_local()?)
} else {
(false, OffsetDateTime::now_utc())
}
}
Err(e) => return Err(e.into()),
};

Expand Down
18 changes: 16 additions & 2 deletions vergen/src/feature/git/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use time::{
self,
well_known::{Iso8601, Rfc3339},
},
OffsetDateTime,
OffsetDateTime, UtcOffset,
};

#[derive(Clone, Copy, Debug, Default)]
Expand Down Expand Up @@ -57,6 +57,7 @@ pub(crate) struct Config {
pub(crate) git_sha: bool,
git_sha_short: bool,
git_cmd: Option<&'static str>,
use_local: bool,
}

// This funkiness allows the command to be output in the docs
Expand Down Expand Up @@ -440,6 +441,12 @@ impl EmitBuilder {
self
}

/// Enable local offset date/timestamp output
pub fn use_local_git(&mut self) -> &mut Self {
self.git_config.use_local = true;
self
}

pub(crate) fn add_git_default(
&self,
e: Error,
Expand Down Expand Up @@ -638,7 +645,14 @@ impl EmitBuilder {
OffsetDateTime::from_unix_timestamp(i64::from_str(&v)?)?,
),
Err(std::env::VarError::NotPresent) => {
(false, OffsetDateTime::parse(&stdout, &Rfc3339)?)
let no_offset = OffsetDateTime::parse(&stdout, &Rfc3339)?;
if self.git_config.use_local {
let local = UtcOffset::local_offset_at(no_offset)?;
let local_offset = no_offset.checked_to_offset(local).unwrap_or(no_offset);
(false, local_offset)
} else {
(false, no_offset)
}
}
Err(e) => return Err(e.into()),
};
Expand Down
23 changes: 18 additions & 5 deletions vergen/src/feature/git/git2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::{
};
use time::{
format_description::{self, well_known::Iso8601},
OffsetDateTime,
OffsetDateTime, UtcOffset,
};

#[derive(Clone, Copy, Debug, Default)]
Expand Down Expand Up @@ -55,6 +55,7 @@ pub(crate) struct Config {
// git rev-parse HEAD (optionally with --short)
pub(crate) git_sha: bool,
git_sha_short: bool,
use_local: bool,
#[cfg(test)]
fail: bool,
}
Expand Down Expand Up @@ -243,6 +244,12 @@ impl EmitBuilder {
self
}

/// Enable local offset date/timestamp output
pub fn use_local_git(&mut self) -> &mut Self {
self.git_config.use_local = true;
self
}

pub(crate) fn add_git_default(
&self,
e: Error,
Expand Down Expand Up @@ -452,10 +459,16 @@ impl EmitBuilder {
true,
OffsetDateTime::from_unix_timestamp(i64::from_str(&v)?)?,
),
Err(std::env::VarError::NotPresent) => (
false,
OffsetDateTime::from_unix_timestamp(commit.time().seconds())?,
),
Err(std::env::VarError::NotPresent) => {
let no_offset = OffsetDateTime::from_unix_timestamp(commit.time().seconds())?;
if self.git_config.use_local {
let local = UtcOffset::local_offset_at(no_offset)?;
let local_offset = no_offset.checked_to_offset(local).unwrap_or(no_offset);
(false, local_offset)
} else {
(false, no_offset)
}
}
Err(e) => return Err(e.into()),
};

Expand Down
23 changes: 18 additions & 5 deletions vergen/src/feature/git/gix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::{
};
use time::{
format_description::{self, well_known::Iso8601},
OffsetDateTime,
OffsetDateTime, UtcOffset,
};

#[derive(Clone, Copy, Debug, Default)]
Expand All @@ -54,6 +54,7 @@ pub(crate) struct Config {
// git rev-parse HEAD (optionally with --short)
pub(crate) git_sha: bool,
git_sha_short: bool,
use_local: bool,
#[cfg(test)]
fail: bool,
}
Expand Down Expand Up @@ -289,6 +290,12 @@ impl EmitBuilder {
}
}

/// Enable local offset date/timestamp output
pub fn use_local_git(&mut self) -> &mut Self {
self.git_config.use_local = true;
self
}

#[cfg(not(test))]
pub(crate) fn add_git_map_entries(
&self,
Expand Down Expand Up @@ -452,10 +459,16 @@ impl EmitBuilder {
true,
OffsetDateTime::from_unix_timestamp(i64::from_str(&v)?)?,
),
Err(std::env::VarError::NotPresent) => (
false,
OffsetDateTime::from_unix_timestamp(commit.time()?.seconds.try_into()?)?,
),
Err(std::env::VarError::NotPresent) => {
let no_offset = OffsetDateTime::from_unix_timestamp(commit.time()?.seconds)?;
if self.git_config.use_local {
let local = UtcOffset::local_offset_at(no_offset)?;
let local_offset = no_offset.checked_to_offset(local).unwrap_or(no_offset);
(false, local_offset)
} else {
(false, no_offset)
}
}
Err(e) => return Err(e.into()),
};

Expand Down
Loading

0 comments on commit 022e804

Please sign in to comment.