From 36f3690cbaf473a4be880642ff978d84769b4c61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Wed, 20 Nov 2024 15:00:17 +0100 Subject: [PATCH] fix long version on nix and ci --- build.rs | 38 ++++++++++---------------------------- flake.lock | 6 +++--- flake.nix | 1 + src/cli.rs | 19 ++++++++++++++++++- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/build.rs b/build.rs index 9d5f7cc..afd452c 100644 --- a/build.rs +++ b/build.rs @@ -3,36 +3,18 @@ use std::env; use git2::Repository; fn main() { - let repo = Repository::open(".").expect("should open git repository"); - let head = repo.head().expect("should get HEAD"); - let branch = head.shorthand().expect("should get branch name"); - let commit = head.peel_to_commit().expect("should get HEAD commit"); - let rev = commit.id().to_string(); + if let Ok(repo) = Repository::open(".") { + let head = repo.head().expect("should get git HEAD"); + let commit = head.peel_to_commit().expect("should get git HEAD commit"); + println!("cargo::rustc-env=GIT_REV={}", commit.id()); + } - let version = env!("CARGO_PKG_VERSION"); let os = env::var("CARGO_CFG_TARGET_OS").expect("should get CARGO_CFG_TARGET_OS"); + println!("cargo::rustc-env=TARGET_OS={os}"); + let env = env::var("CARGO_CFG_TARGET_ENV").expect("should get CARGO_CFG_TARGET_ENV"); + println!("cargo::rustc-env=TARGET_ENV={env}"); + let arch = env::var("CARGO_CFG_TARGET_ARCH").expect("should get CARGO_CFG_TARGET_ARCH"); - - let long_version = [ - version, - &os, - &env, - &arch, - "git branch", - &branch, - "rev", - &rev, - ] - .into_iter() - .filter(|s| !s.trim().is_empty()) - .fold(String::new(), |mut version, section| { - if !version.is_empty() { - version.push(' ') - } - version.push_str(section); - version - }); - - println!("cargo::rustc-env=HIMALAYA_LONG_VERSION={long_version}"); + println!("cargo::rustc-env=TARGET_ARCH={arch}"); } diff --git a/flake.lock b/flake.lock index 86a4dc9..5dd29d6 100644 --- a/flake.lock +++ b/flake.lock @@ -79,11 +79,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1730137625, - "narHash": "sha256-9z8oOgFZiaguj+bbi3k4QhAD6JabWrnv7fscC/mt0KE=", + "lastModified": 1731797254, + "narHash": "sha256-df3dJApLPhd11AlueuoN0Q4fHo/hagP75LlM5K1sz9g=", "owner": "nixos", "repo": "nixpkgs", - "rev": "64b80bfb316b57cdb8919a9110ef63393d74382a", + "rev": "e8c38b73aeb218e27163376a2d617e61a2ad9b59", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index b52c237..4abdb9b 100644 --- a/flake.nix +++ b/flake.nix @@ -134,6 +134,7 @@ nativeBuildInputs = with pkgs; [ pkg-config ]; CARGO_BUILD_TARGET = targetConfig.rustTarget; CARGO_BUILD_RUSTFLAGS = [ "-Ctarget-feature=+crt-static" ]; + GIT_REV = self.rev or self.dirtyRev or "dirty"; postInstall = '' export WINEPREFIX="$(mktemp -d)" diff --git a/src/cli.rs b/src/cli.rs index 52e7fec..cc3f4d6 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -24,7 +24,9 @@ use crate::{ }; #[derive(Parser, Debug)] -#[command(name = "himalaya", author, version, about)] +#[command(name = env!("CARGO_PKG_NAME"))] +#[command(author, version, about)] +#[command(long_version = Cli::LONG_VERSION)] #[command(propagate_version = true, infer_subcommands = true)] pub struct Cli { #[command(subcommand)] @@ -72,6 +74,21 @@ pub struct Cli { pub trace: bool, } +impl Cli { + pub const LONG_VERSION: &'static str = concat!( + "v", + env!("CARGO_PKG_VERSION"), + " on ", + env!("TARGET_OS"), + " ", + env!("TARGET_ENV"), + " ", + env!("TARGET_ARCH"), + ", git rev ", + env!("GIT_REV"), + ); +} + #[derive(Subcommand, Debug)] pub enum HimalayaCommand { #[command(subcommand)]