From 3c4a278db3e5b9696269caefad2bfaa79578704c Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 9 Mar 2022 19:04:17 -0800 Subject: refactor: use vergen for build variables --- Cargo.toml | 7 +++++++ build.rs | 30 ++---------------------------- src/constants.rs | 2 +- src/lib.rs | 2 +- src/utils.rs | 2 +- 5 files changed, 12 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6d38c68..0f5308d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,3 +45,10 @@ lazy_static = "1.4.0" # Web reqwest = { version = "0.11.9", features = ["json"] } + +[build-dependencies] +# Build variables +vergen = "6.0.2" + +# `Result` +anyhow = "1.0.56" diff --git a/build.rs b/build.rs index a6f53ae..ce2f258 100644 --- a/build.rs +++ b/build.rs @@ -16,32 +16,6 @@ // Copyright (C) 2022-2022 Fuwn // SPDX-License-Identifier: GPL-3.0-only -use std::env::var; - -/// -fn git_commit_hash() -> String { - if let Ok(output) = std::process::Command::new("git") - .arg("rev-list") - .arg("-1") - .arg("HEAD") - .output() - { - if output.status.success() { - std::str::from_utf8(&output.stdout[..40]) - .unwrap() - .to_string() - } else { - "UNKNOWN".to_string() - } - } else { - "UNKNOWN".to_string() - } -} - -fn main() { - println!("cargo:rerun-if-changed=build.rs"); - println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_commit_hash()); - println!("cargo:rerun-if-env-changed=GIT_COMMIT_HASH"); - println!("cargo:rustc-env=TARGET={}", var("TARGET").unwrap()); - println!("cargo:rustc-env=PROFILE={}", var("PROFILE").unwrap()); +fn main() -> anyhow::Result<()> { + vergen::vergen(vergen::Config::default()) } diff --git a/src/constants.rs b/src/constants.rs index b5fb1e0..6f3e6e4 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -24,7 +24,7 @@ lazy_static::lazy_static! { include_str!("index.rst"), format_args!( "https://github.com/senpy-club/api-worker/tree/{}", - env!("GIT_COMMIT_HASH"), + env!("VERGEN_GIT_SHA"), ) ) }; diff --git a/src/lib.rs b/src/lib.rs index 229472a..e5661dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,7 @@ pub async fn main( .get("/v2/version", |_, _| { Response::from_json(&serde_json::json!({ "crate_version": env!("CARGO_PKG_VERSION"), - "git_commit_hash": env!("GIT_COMMIT_HASH"), + "git_commit_hash": env!("VERGEN_GIT_SHA"), }))? .with_cors(&utils::cors()) }) diff --git a/src/utils.rs b/src/utils.rs index d48ff34..8bb70da 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -28,7 +28,7 @@ pub async fn github_api( .get(constants::GITHUB_API_ENDPOINT) .header( "User-Agent", - format!("senpy-club/api-worker - {}", env!("GIT_COMMIT_HASH")), + format!("senpy-club/api-worker - {}", env!("VERGEN_GIT_SHA")), ); if std::env::var("GITHUB_TOKEN").is_ok() { -- cgit v1.2.3