mirror of
https://github.com/neovide/neovide.git
synced 2026-09-10 07:16:25 -04:00
we introduce NEOVIDE_BUILD_VERSION in build.rs from `git describe` so tagged builds keep the package version and non-tagged builds report `nightly-<count>+g<sha>` with `-dirty` preserved when applicable. we kinda of follow the neovim pattern for nightly versions since the target users are used to it. we also add cargo rerun tracking for git metadata and tracked files, and use the generated version for the CLI `--version` output, startup version logging and `g:neovide_version` consecutively. note: bundles will not be affected since they have its own limited versioning scheme.
99 lines
2.7 KiB
Rust
99 lines
2.7 KiB
Rust
use std::process::Command;
|
|
|
|
fn main() {
|
|
set_rerun();
|
|
|
|
println!("cargo:rustc-env=NEOVIDE_BUILD_VERSION={}", build_version());
|
|
|
|
#[cfg(windows)]
|
|
{
|
|
let mut res = winres::WindowsResource::new();
|
|
res.set_icon("assets/neovide.ico");
|
|
res.compile().expect("Could not attach exe icon");
|
|
}
|
|
}
|
|
|
|
fn set_rerun() {
|
|
println!("cargo:rerun-if-changed=build.rs");
|
|
println!("cargo:rerun-if-changed=Cargo.toml");
|
|
println!("cargo:rerun-if-changed=assets/neovide.ico");
|
|
|
|
for path in ["HEAD", "refs", "index", "packed-refs"] {
|
|
if let Some(path) = git_path(path) {
|
|
println!("cargo:rerun-if-changed={path}");
|
|
}
|
|
}
|
|
|
|
if let Some(files) = git_output(&["ls-files", "-z"]) {
|
|
for file in files.split('\0').filter(|file| !file.is_empty()) {
|
|
println!("cargo:rerun-if-changed={file}");
|
|
}
|
|
} else {
|
|
println!("cargo:warning=Could not list tracked files for version rerun tracking");
|
|
}
|
|
}
|
|
|
|
fn build_version() -> String {
|
|
let package_version = env!("CARGO_PKG_VERSION");
|
|
let git_describe = git_output(&[
|
|
"describe",
|
|
"--tags",
|
|
"--match",
|
|
"[0-9]*.[0-9]*.[0-9]*",
|
|
"--dirty",
|
|
"--always",
|
|
]);
|
|
|
|
git_describe
|
|
.as_deref()
|
|
.map(|describe| format(package_version, describe))
|
|
.unwrap_or_else(|| package_version.to_owned())
|
|
}
|
|
|
|
fn format(version: &str, describe: &str) -> String {
|
|
if describe == version {
|
|
return version.to_owned();
|
|
}
|
|
|
|
if describe == format!("{version}-dirty") {
|
|
return describe.to_owned();
|
|
}
|
|
|
|
let (describe, dirty) = match describe.strip_suffix("-dirty") {
|
|
Some(version) => (version, true),
|
|
None => (describe, false),
|
|
};
|
|
|
|
let mut parts = describe.splitn(3, '-');
|
|
let tag = parts.next();
|
|
let height = parts.next();
|
|
let commit = parts.next();
|
|
|
|
match (tag, height, commit) {
|
|
// formats non-exact matches as <tag>-<count>-g<abbrev>
|
|
// see: https://git-scm.com/docs/git-describe#_examples
|
|
(Some(tag), Some(height), Some(commit)) if tag == version && commit.starts_with('g') => {
|
|
let mut version = format!("nightly-{height}+{commit}");
|
|
if dirty {
|
|
version.push_str("-dirty");
|
|
}
|
|
version
|
|
}
|
|
_ => version.to_owned(),
|
|
}
|
|
}
|
|
|
|
fn git_path(path: &str) -> Option<String> {
|
|
git_output(&["rev-parse", "--git-path", path])
|
|
}
|
|
|
|
fn git_output(args: &[&str]) -> Option<String> {
|
|
Command::new("git")
|
|
.args(args)
|
|
.output()
|
|
.ok()
|
|
.filter(|output| output.status.success())
|
|
.and_then(|output| String::from_utf8(output.stdout).ok())
|
|
.map(|output| output.trim().to_owned())
|
|
}
|