diff --git a/.gitignore b/.gitignore index 0ec367a..f64b65f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ .cargo .build SNAPSHOT +SOURCE_COMMIT # Tauri generated files gen diff --git a/Makefile b/Makefile index b816bd6..78d96a4 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,16 @@ RUST_VERSION ?= 1.89 IGNORE_RUST_VERSION ?= 0 VERSION = $(shell grep '^version' Cargo.toml | head -1 | sed 's/version *= *"\(.*\)"/\1/') +SOURCE_COMMIT_VALUE := $(shell \ + if [ -n "$$SOURCE_GIT_COMMIT" ]; then \ + printf '%s' "$$SOURCE_GIT_COMMIT" | cut -c1-9; \ + elif [ -n "$$GITHUB_SHA" ]; then \ + printf '%s' "$$GITHUB_SHA" | cut -c1-9; \ + elif [ -f SOURCE_COMMIT ]; then \ + head -n1 SOURCE_COMMIT | tr -d '[:space:]' | cut -c1-9; \ + else \ + git rev-parse --short=9 HEAD 2>/dev/null || printf unknown; \ + fi) GUI_LIBC_SUFFIX ?= $(shell ldd --version 2>&1 | grep -qi musl && echo -musl || true) REVISION ?= 1 RPM_SOURCE ?= %{name}.tar.gz @@ -55,11 +65,13 @@ clean-tarball: rm -rf .vendor rm -rf vendor.tar.xz rm -rf .cargo + rm -f SOURCE_COMMIT # Create a tarball, include the cargo dependencies if OFFLINE is set to 1 tarball: clean-tarball mkdir -p .cargo mkdir -p .build/tarball + printf '%s\n' "$(SOURCE_COMMIT_VALUE)" > SOURCE_COMMIT # If OFFLINE is set to 1, vendor all cargo dependencies # Generate a OFFLINE_BUILD file to indicate offline build diff --git a/apps/gpauth/build.rs b/apps/gpauth/build.rs index 280edd3..6b1dc12 100644 --- a/apps/gpauth/build.rs +++ b/apps/gpauth/build.rs @@ -1,4 +1,63 @@ +use std::{env, fs, path::PathBuf, process::Command}; + +const UNKNOWN_COMMIT: &str = "unknown"; +const DISPLAY_COMMIT_LEN: usize = 9; + fn main() { + println!("cargo:rerun-if-env-changed=GPAUTH_GIT_COMMIT"); + println!("cargo:rerun-if-env-changed=SOURCE_GIT_COMMIT"); + println!("cargo:rerun-if-env-changed=GITHUB_SHA"); + + let repo_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is set")).join("../.."); + let source_commit = repo_root.join("SOURCE_COMMIT"); + println!("cargo:rerun-if-changed={}", source_commit.display()); + println!( + "cargo:rustc-env=GPAUTH_GIT_COMMIT={}", + resolve_commit(&repo_root, &source_commit) + ); + #[cfg(feature = "webview-auth")] tauri_build::build() } + +fn resolve_commit(repo_root: &PathBuf, source_commit: &PathBuf) -> String { + env_commit("GPAUTH_GIT_COMMIT") + .or_else(|| env_commit("SOURCE_GIT_COMMIT")) + .or_else(|| env_commit("GITHUB_SHA")) + .or_else(|| file_commit(source_commit)) + .or_else(|| git_commit(repo_root)) + .unwrap_or_else(|| UNKNOWN_COMMIT.to_string()) +} + +fn env_commit(key: &str) -> Option { + env::var(key).ok().and_then(|value| normalize_commit(&value)) +} + +fn file_commit(path: &PathBuf) -> Option { + fs::read_to_string(path).ok().and_then(|value| normalize_commit(&value)) +} + +fn git_commit(repo_root: &PathBuf) -> Option { + let output = Command::new("git") + .args(["rev-parse", "--short=9", "HEAD"]) + .current_dir(repo_root) + .output() + .ok()?; + + if !output.status.success() { + return None; + } + + String::from_utf8(output.stdout) + .ok() + .and_then(|value| normalize_commit(&value)) +} + +fn normalize_commit(value: &str) -> Option { + let value = value.trim(); + if value.is_empty() { + return None; + } + + Some(value.chars().take(DISPLAY_COMMIT_LEN).collect()) +} diff --git a/apps/gpauth/src/cli.rs b/apps/gpauth/src/cli.rs index d63dd02..607bdf0 100644 --- a/apps/gpauth/src/cli.rs +++ b/apps/gpauth/src/cli.rs @@ -11,7 +11,14 @@ use log::info; use serde_json::json; use tempfile::NamedTempFile; -const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", compile_time::date_str!(), ")"); +const VERSION: &str = concat!( + env!("CARGO_PKG_VERSION"), + " (", + env!("GPAUTH_GIT_COMMIT"), + " ", + compile_time::date_str!(), + ")" +); #[derive(Parser)] #[command( diff --git a/apps/gpclient/build.rs b/apps/gpclient/build.rs new file mode 100644 index 0000000..d66ac42 --- /dev/null +++ b/apps/gpclient/build.rs @@ -0,0 +1,56 @@ +use std::{env, fs, path::PathBuf, process::Command}; + +const UNKNOWN_COMMIT: &str = "unknown"; +const DISPLAY_COMMIT_LEN: usize = 9; + +fn main() { + println!("cargo:rerun-if-env-changed=SOURCE_GIT_COMMIT"); + println!("cargo:rerun-if-env-changed=GPCLIENT_GIT_COMMIT"); + println!("cargo:rerun-if-env-changed=GITHUB_SHA"); + + let repo_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is set")).join("../.."); + let source_commit = repo_root.join("SOURCE_COMMIT"); + println!("cargo:rerun-if-changed={}", source_commit.display()); + + let commit = env_commit("GPCLIENT_GIT_COMMIT") + .or_else(|| env_commit("SOURCE_GIT_COMMIT")) + .or_else(|| env_commit("GITHUB_SHA")) + .or_else(|| file_commit(&source_commit)) + .or_else(|| git_commit(&repo_root)) + .unwrap_or_else(|| UNKNOWN_COMMIT.to_string()); + + println!("cargo:rustc-env=GPCLIENT_GIT_COMMIT={commit}"); +} + +fn env_commit(key: &str) -> Option { + env::var(key).ok().and_then(|value| normalize_commit(&value)) +} + +fn file_commit(path: &PathBuf) -> Option { + fs::read_to_string(path).ok().and_then(|value| normalize_commit(&value)) +} + +fn git_commit(repo_root: &PathBuf) -> Option { + let output = Command::new("git") + .args(["rev-parse", "--short=12", "HEAD"]) + .current_dir(repo_root) + .output() + .ok()?; + + if !output.status.success() { + return None; + } + + String::from_utf8(output.stdout) + .ok() + .and_then(|value| normalize_commit(&value)) +} + +fn normalize_commit(value: &str) -> Option { + let value = value.trim(); + if value.is_empty() { + return None; + } + + Some(value.chars().take(DISPLAY_COMMIT_LEN).collect()) +} diff --git a/apps/gpclient/src/cli.rs b/apps/gpclient/src/cli.rs index cff6aa1..1b38dd1 100644 --- a/apps/gpclient/src/cli.rs +++ b/apps/gpclient/src/cli.rs @@ -19,7 +19,14 @@ use crate::{ launch_gui::{LaunchGuiArgs, LaunchGuiHandler}, }; -const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", compile_time::date_str!(), ")"); +const VERSION: &str = concat!( + env!("CARGO_PKG_VERSION"), + " (", + env!("GPCLIENT_GIT_COMMIT"), + " ", + compile_time::date_str!(), + ")" +); pub(crate) struct SharedArgs<'a> { pub(crate) fix_openssl: bool, diff --git a/apps/gpgui-helper/src-tauri/build.rs b/apps/gpgui-helper/src-tauri/build.rs index 795b9b7..3978591 100644 --- a/apps/gpgui-helper/src-tauri/build.rs +++ b/apps/gpgui-helper/src-tauri/build.rs @@ -1,3 +1,62 @@ +use std::{env, fs, path::PathBuf, process::Command}; + +const UNKNOWN_COMMIT: &str = "unknown"; +const DISPLAY_COMMIT_LEN: usize = 9; + fn main() { + println!("cargo:rerun-if-env-changed=GPGUI_HELPER_GIT_COMMIT"); + println!("cargo:rerun-if-env-changed=SOURCE_GIT_COMMIT"); + println!("cargo:rerun-if-env-changed=GITHUB_SHA"); + + let repo_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is set")).join("../../.."); + let source_commit = repo_root.join("SOURCE_COMMIT"); + println!("cargo:rerun-if-changed={}", source_commit.display()); + println!( + "cargo:rustc-env=GPGUI_HELPER_GIT_COMMIT={}", + resolve_commit(&repo_root, &source_commit) + ); + tauri_build::build() } + +fn resolve_commit(repo_root: &PathBuf, source_commit: &PathBuf) -> String { + env_commit("GPGUI_HELPER_GIT_COMMIT") + .or_else(|| env_commit("SOURCE_GIT_COMMIT")) + .or_else(|| env_commit("GITHUB_SHA")) + .or_else(|| file_commit(source_commit)) + .or_else(|| git_commit(repo_root)) + .unwrap_or_else(|| UNKNOWN_COMMIT.to_string()) +} + +fn env_commit(key: &str) -> Option { + env::var(key).ok().and_then(|value| normalize_commit(&value)) +} + +fn file_commit(path: &PathBuf) -> Option { + fs::read_to_string(path).ok().and_then(|value| normalize_commit(&value)) +} + +fn git_commit(repo_root: &PathBuf) -> Option { + let output = Command::new("git") + .args(["rev-parse", "--short=9", "HEAD"]) + .current_dir(repo_root) + .output() + .ok()?; + + if !output.status.success() { + return None; + } + + String::from_utf8(output.stdout) + .ok() + .and_then(|value| normalize_commit(&value)) +} + +fn normalize_commit(value: &str) -> Option { + let value = value.trim(); + if value.is_empty() { + return None; + } + + Some(value.chars().take(DISPLAY_COMMIT_LEN).collect()) +} diff --git a/apps/gpgui-helper/src-tauri/src/cli.rs b/apps/gpgui-helper/src-tauri/src/cli.rs index ed92ffe..532fb1a 100644 --- a/apps/gpgui-helper/src-tauri/src/cli.rs +++ b/apps/gpgui-helper/src-tauri/src/cli.rs @@ -7,7 +7,14 @@ use log::info; use crate::app::App; -const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", compile_time::date_str!(), ")"); +const VERSION: &str = concat!( + env!("CARGO_PKG_VERSION"), + " (", + env!("GPGUI_HELPER_GIT_COMMIT"), + " ", + compile_time::date_str!(), + ")" +); const GP_API_KEY: &[u8; 32] = &[0; 32]; #[derive(Parser)] diff --git a/apps/gpservice/build.rs b/apps/gpservice/build.rs new file mode 100644 index 0000000..bec97de --- /dev/null +++ b/apps/gpservice/build.rs @@ -0,0 +1,60 @@ +use std::{env, fs, path::PathBuf, process::Command}; + +const UNKNOWN_COMMIT: &str = "unknown"; +const DISPLAY_COMMIT_LEN: usize = 9; + +fn main() { + println!("cargo:rerun-if-env-changed=GPSERVICE_GIT_COMMIT"); + println!("cargo:rerun-if-env-changed=SOURCE_GIT_COMMIT"); + println!("cargo:rerun-if-env-changed=GITHUB_SHA"); + + let repo_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is set")).join("../.."); + let source_commit = repo_root.join("SOURCE_COMMIT"); + println!("cargo:rerun-if-changed={}", source_commit.display()); + println!( + "cargo:rustc-env=GPSERVICE_GIT_COMMIT={}", + resolve_commit(&repo_root, &source_commit) + ); +} + +fn resolve_commit(repo_root: &PathBuf, source_commit: &PathBuf) -> String { + env_commit("GPSERVICE_GIT_COMMIT") + .or_else(|| env_commit("SOURCE_GIT_COMMIT")) + .or_else(|| env_commit("GITHUB_SHA")) + .or_else(|| file_commit(source_commit)) + .or_else(|| git_commit(repo_root)) + .unwrap_or_else(|| UNKNOWN_COMMIT.to_string()) +} + +fn env_commit(key: &str) -> Option { + env::var(key).ok().and_then(|value| normalize_commit(&value)) +} + +fn file_commit(path: &PathBuf) -> Option { + fs::read_to_string(path).ok().and_then(|value| normalize_commit(&value)) +} + +fn git_commit(repo_root: &PathBuf) -> Option { + let output = Command::new("git") + .args(["rev-parse", "--short=9", "HEAD"]) + .current_dir(repo_root) + .output() + .ok()?; + + if !output.status.success() { + return None; + } + + String::from_utf8(output.stdout) + .ok() + .and_then(|value| normalize_commit(&value)) +} + +fn normalize_commit(value: &str) -> Option { + let value = value.trim(); + if value.is_empty() { + return None; + } + + Some(value.chars().take(DISPLAY_COMMIT_LEN).collect()) +} diff --git a/apps/gpservice/src/cli.rs b/apps/gpservice/src/cli.rs index 54e6ff7..2256e92 100644 --- a/apps/gpservice/src/cli.rs +++ b/apps/gpservice/src/cli.rs @@ -16,7 +16,14 @@ use tokio::sync::{mpsc, watch}; use crate::{vpn_task::VpnTask, ws_server::WsServer}; -const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", compile_time::date_str!(), ")"); +const VERSION: &str = concat!( + env!("CARGO_PKG_VERSION"), + " (", + env!("GPSERVICE_GIT_COMMIT"), + " ", + compile_time::date_str!(), + ")" +); #[derive(Parser)] #[command(version = VERSION)]