Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run rust formatter. #439

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 54 additions & 57 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,73 @@ use std::env;
use std::path::Path;
use std::process::Command;


fn main() {

let result_of_git = version_from_git_info();
match result_of_git{
Ok(version) => {
println!("cargo:rustc-env=VW_VERSION={version}");
println!("cargo:rustc-env=CARGO_PKG_VERSION={version}");
println!("cargo:rustc-env=GIT_EXACT_TAG={version}");
},
Err(_) => {
println!("cargo:rustc-env=VW_VERSION=unknown");
println!("cargo:rustc-env=CARGO_PKG_VERSION=unknown");
println!("cargo:rustc-env=GIT_EXACT_TAG=unknown");
println!("cargo:rustc-env=GIT_LAST_TAG=unknown");
println!("cargo:rustc-env=GIT_BRANCH=unknown");
println!("cargo:rustc-env=GIT_REV=unknown");
}
let result_of_git = version_from_git_info();
match result_of_git {
Ok(version) => {
println!("cargo:rustc-env=VW_VERSION={version}");
println!("cargo:rustc-env=CARGO_PKG_VERSION={version}");
println!("cargo:rustc-env=GIT_EXACT_TAG={version}");
}
Err(_) => {
println!("cargo:rustc-env=VW_VERSION=unknown");
println!("cargo:rustc-env=CARGO_PKG_VERSION=unknown");
println!("cargo:rustc-env=GIT_EXACT_TAG=unknown");
println!("cargo:rustc-env=GIT_LAST_TAG=unknown");
println!("cargo:rustc-env=GIT_BRANCH=unknown");
println!("cargo:rustc-env=GIT_REV=unknown");
}
}

create_git_sqlite();
create_git_sqlite();
}


fn create_git_sqlite() {
let src = env::var("CARGO_MANIFEST_DIR").unwrap();
let dst = Path::new(&env::var("OUT_DIR").unwrap()).join("built.rs");
println!("Path: {:?}", dst);
let path = Path::new(&src);
built::write_built_file_with_opts(Option::from(path), &dst)
.expect("Failed to acquire build-time information");
let src = env::var("CARGO_MANIFEST_DIR").unwrap();
let dst = Path::new(&env::var("OUT_DIR").unwrap()).join("built.rs");
println!("Path: {:?}", dst);
let path = Path::new(&src);
built::write_built_file_with_opts(Option::from(path), &dst)
.expect("Failed to acquire build-time information");
}

fn version_from_git_info() -> Result<String, std::io::Error> {
// The exact tag for the current commit, can be empty when
// the current commit doesn't have an associated tag
let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
if let Some(ref exact) = exact_tag {
println!("cargo:rustc-env=GIT_EXACT_TAG={exact}");
}
// The exact tag for the current commit, can be empty when
// the current commit doesn't have an associated tag
let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
if let Some(ref exact) = exact_tag {
println!("cargo:rustc-env=GIT_EXACT_TAG={exact}");
}

// The last available tag, equal to exact_tag when
// the current commit is tagged
let last_tag = run(&["git", "describe", "--abbrev=0", "--tags"])?;
println!("cargo:rustc-env=GIT_LAST_TAG={last_tag}");
// The last available tag, equal to exact_tag when
// the current commit is tagged
let last_tag = run(&["git", "describe", "--abbrev=0", "--tags"])?;
println!("cargo:rustc-env=GIT_LAST_TAG={last_tag}");

// The current branch name
let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?;
println!("cargo:rustc-env=GIT_BRANCH={branch}");
// The current branch name
let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?;
println!("cargo:rustc-env=GIT_BRANCH={branch}");

// The current git commit hash
let rev = run(&["git", "rev-parse", "HEAD"])?;
let rev_short = rev.get(..8).unwrap_or_default();
println!("cargo:rustc-env=GIT_REV={rev_short}");
// The current git commit hash
let rev = run(&["git", "rev-parse", "HEAD"])?;
let rev_short = rev.get(..8).unwrap_or_default();
println!("cargo:rustc-env=GIT_REV={rev_short}");

// Combined version
if let Some(exact) = exact_tag {
Ok(exact)
} else if &branch != "main" && &branch != "master" {
Ok(format!("{last_tag}-{rev_short} ({branch})"))
} else {
Ok(format!("{last_tag}-{rev_short}"))
}
// Combined version
if let Some(exact) = exact_tag {
Ok(exact)
} else if &branch != "main" && &branch != "master" {
Ok(format!("{last_tag}-{rev_short} ({branch})"))
} else {
Ok(format!("{last_tag}-{rev_short}"))
}
}

fn run(args: &[&str]) -> Result<String, std::io::Error> {
let out = Command::new(args[0]).args(&args[1..]).output()?;
if !out.status.success() {
use std::io::{Error, ErrorKind};
return Err(Error::new(ErrorKind::Other, "Command not successful"));
}
Ok(String::from_utf8(out.stdout).unwrap().trim().to_string())
}
let out = Command::new(args[0]).args(&args[1..]).output()?;
if !out.status.success() {
use std::io::{Error, ErrorKind};
return Err(Error::new(ErrorKind::Other, "Command not successful"));
}
Ok(String::from_utf8(out.stdout).unwrap().trim().to_string())
}
Loading
Loading