-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
154 lines (134 loc) · 4.38 KB
/
build.rs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::{
env::var,
process::{Command, Stdio},
sync::LazyLock,
};
use winresource::WindowsResource;
fn format(dirty: bool, commit_id: &str, branches: &str) -> (String, String) {
// no branches or multiple
if branches.is_empty() || branches.contains(' ') {
let info = format!("{commit_id:.7}{}", if dirty { "*" } else { "" });
return (commit_id.to_owned(), info);
}
let info = format!(
"{}@{commit_id:.7}{}",
branches.strip_suffix('*').unwrap_or(branches),
if dirty { "*" } else { "" },
);
(commit_id.to_owned(), info)
}
// why not just &[&str]?
// that, detective, is the right question
fn sh<'a>(args: impl IntoIterator<Item = &'a str>) -> Option<String> {
let mut args = args.into_iter();
Command::new(args.next()?)
.args(args)
.stdout(Stdio::piped())
.output()
.ok()
.filter(|out| out.status.success())
.and_then(|out| String::from_utf8(out.stdout).ok())
.map(|mut s| {
s.truncate(s.trim_end().len());
s
})
}
static IS_RA: LazyLock<bool> = LazyLock::new(|| var("RA_RUSTC_WRAPPER").is_ok());
static IS_CLIPPY: LazyLock<bool> = LazyLock::new(|| var("CARGO_CFG_CLIPPY").is_ok());
fn get_from_jj() -> Option<(String, String)> {
// avoid jj snapshots when RA calls this
if *IS_RA {
let stub = "rust-analyzer".to_owned();
return Some((stub.clone(), stub));
}
// ..or clippy
if *IS_CLIPPY {
let stub = "clippy".to_owned();
return Some((stub.clone(), stub));
}
if let Ok(nix_rev) = std::env::var("NIX_REV") {
let info = format!("nix!{nix_rev:.7}");
return Some((nix_rev, info));
}
// in the future we'd get the commit topic
// instead of git branches here, would be better
let res = sh([
"jj",
"log",
"--no-graph",
"-r",
"@|@-",
"-T",
r#"concat(empty,",",commit_id,",",bookmarks,"\n")"#,
])?;
let (wc, parent) = res.split_once('\n')?;
let (is_empty, rest) = wc.split_once(',')?;
let (commit_id, branches) = rest.split_once(',')?;
if is_empty != "true" {
return Some(format(true, commit_id, branches));
}
let (commit_id, branches) = parent.split_once(',')?.1.split_once(',')?;
Some(format(false, commit_id, branches))
}
// we need to run *3* git commands to get all the information
fn get_from_git() -> Option<(String, String)> {
let is_empty = sh(["git", "diff", "--shortstat"])?.is_empty();
let commit_id = sh(["git", "rev-parse", "HEAD"])?;
let branch = sh([
"git",
"name-rev",
"--name-only",
"--refs=heads/*",
"--refs=tags/*",
"HEAD",
])?;
let branch = branch
.strip_suffix("~1")
.or_else(|| branch.strip_suffix("^0"))
.filter(|b| *b != "undefined")
.unwrap_or_default();
Some(format(!is_empty, &commit_id, branch))
}
fn emit_build_info() {
let (commit, info) = get_from_jj()
.or_else(get_from_git)
.expect("Building without jj or git installed, or not in a repo");
println!("cargo::rustc-env=BUILD_COMMIT={commit}");
println!("cargo::rustc-env=BUILD_INFO={info}")
}
fn embed_windows_resource() {
if var("CARGO_CFG_WINDOWS").is_ok() && !(*IS_RA || *IS_CLIPPY) {
let res = (|| -> Result<(), Box<dyn std::error::Error>> {
Command::new("magick")
.args([
"res/icon.png",
"-define",
"icon:auto-resize=256,128,96,64,48,32,16",
"-filter",
"point",
"res/icon.ico",
])
.spawn()?
.wait()?;
WindowsResource::new()
.set_icon("res/icon.ico")
.set("ProductName", "Noita Utility Box")
.set("CompanyName", "necauqua")
.set("LegalCopyright", &var("CARGO_PKG_LICENSE").unwrap())
.compile()?;
Ok(())
})();
if let Err(e) = res {
let msg = format!("Failed to embed Windows resource: {e}");
if let Ok("release") = var("PROFILE").as_deref() {
panic!("{msg}")
} else {
println!("cargo:warning={msg}")
}
}
}
}
fn main() {
emit_build_info();
embed_windows_resource();
}