fix long version on nix and ci

This commit is contained in:
Clément DOUIN 2024-11-20 15:00:17 +01:00
parent 396a91a322
commit 36f3690cba
No known key found for this signature in database
GPG key ID: 353E4A18EE0FAB72
4 changed files with 32 additions and 32 deletions

View file

@ -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();
let version = env!("CARGO_PKG_VERSION");
let os = env::var("CARGO_CFG_TARGET_OS").expect("should get CARGO_CFG_TARGET_OS");
let env = env::var("CARGO_CFG_TARGET_ENV").expect("should get CARGO_CFG_TARGET_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(' ')
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());
}
version.push_str(section);
version
});
println!("cargo::rustc-env=HIMALAYA_LONG_VERSION={long_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");
println!("cargo::rustc-env=TARGET_ARCH={arch}");
}

View file

@ -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": {

View file

@ -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)"

View file

@ -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)]