Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
94 lines (86 loc) · 2.84 KB
/
Copy pathbuild.rs
File metadata and controls
94 lines (86 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::process::{Command, ExitStatus};
fn main() {
#[cfg(any(feature = "memory-serve", feature = "storybook"))]
{
let sources = vec![
#[cfg(feature = "memory-serve")]
("frontend", "../frontend/dist"),
#[cfg(feature = "storybook")]
("storybook", "../frontend/dist-storybook"),
];
memory_serve::load_names_directories(sources, true);
}
// check if the repository is dirty (if there is any)
let is_dirty = if let Ok((_, status)) =
run_command("git", &["diff-index", "--quiet", "HEAD", "--"], false)
{
!status.success()
} else {
false
};
let dirty_postfix = if is_dirty { "-dirty" } else { "" };
// determine the git commit (if there is any)
let git_rev_raw = run_command_out("git", &["rev-parse", "HEAD"], true).ok();
let git_rev = git_rev_raw
.as_deref()
.map(|rev| format!("{rev}{dirty_postfix}"));
// determine if there is a tag for this commit (if we found a commit previously)
let git_version = if let Some(git_rev_raw) = &git_rev_raw {
run_command_out(
"git",
&[
"describe",
"--tags",
"--exact-match",
"--abbrev=0",
"--match",
"v*",
git_rev_raw,
],
true,
)
.ok()
.map(|tag| tag.trim_start_matches('v').to_owned())
} else {
None
};
// fallback to short commit hash if no tag found
let git_version = git_version.unwrap_or_else(|| {
git_rev
.as_deref()
.map(|rev| format!("dev-{}", &rev[..7]))
.unwrap_or_else(|| "-".to_owned())
});
let git_version = format!("{git_version}{dirty_postfix}");
// output
println!(
"cargo:rustc-env=ABACUS_GIT_REV={}",
git_rev.unwrap_or("-".to_owned())
);
println!("cargo:rustc-env=ABACUS_GIT_VERSION={}", git_version);
}
fn run_command(
cmd: &str,
args: &[&str],
err_on_fail: bool,
) -> std::io::Result<(String, ExitStatus)> {
let res = Command::new(cmd).args(args).output()?;
match String::from_utf8(res.stdout) {
Ok(data) => {
let trimmed = data.trim().to_owned();
if err_on_fail && !res.status.success() {
Err(std::io::Error::other(format!(
"Command '{}' failed with error code: {}",
cmd,
res.status.code().unwrap_or(-1)
)))
} else {
Ok((trimmed, res.status))
}
}
Err(e) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e)),
}
}
fn run_command_out(cmd: &str, args: &[&str], err_on_fail: bool) -> std::io::Result<String> {
run_command(cmd, args, err_on_fail).map(|(out, _)| out)
}