Skip to content

Commit

Permalink
fix long version on nix and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Nov 20, 2024
1 parent 396a91a commit 36f3690
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
38 changes: 10 additions & 28 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
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");
println!("cargo::rustc-env=TARGET_OS={os}");

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
});
let env = env::var("CARGO_CFG_TARGET_ENV").expect("should get CARGO_CFG_TARGET_ENV");
println!("cargo::rustc-env=TARGET_ENV={env}");

println!("cargo::rustc-env=HIMALAYA_LONG_VERSION={long_version}");
let arch = env::var("CARGO_CFG_TARGET_ARCH").expect("should get CARGO_CFG_TARGET_ARCH");
println!("cargo::rustc-env=TARGET_ARCH={arch}");
}
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
19 changes: 18 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 36f3690

Please sign in to comment.