diff options
| author | Fuwn <[email protected]> | 2022-03-09 19:04:17 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-03-09 19:04:17 -0800 |
| commit | 3c4a278db3e5b9696269caefad2bfaa79578704c (patch) | |
| tree | 096febe388721081a092f09582bdd9589c6b6cbd | |
| parent | feat(index): git commit hash on index (diff) | |
| download | api-worker-3c4a278db3e5b9696269caefad2bfaa79578704c.tar.xz api-worker-3c4a278db3e5b9696269caefad2bfaa79578704c.zip | |
refactor: use vergen for build variables
| -rw-r--r-- | Cargo.toml | 7 | ||||
| -rw-r--r-- | build.rs | 30 | ||||
| -rw-r--r-- | src/constants.rs | 2 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/utils.rs | 2 |
5 files changed, 12 insertions, 31 deletions
@@ -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" @@ -16,32 +16,6 @@ // Copyright (C) 2022-2022 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -use std::env::var; - -/// <https://github.com/denoland/deno/blob/main/cli/build.rs#L265:L285> -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"), ) ) }; @@ -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() { |