-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathbuild.rs
89 lines (76 loc) · 2.91 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
extern crate chrono;
extern crate git2;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
fn main() {
// build info
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let mut f = File::create(out_dir.join("build-info.txt")).unwrap();
match read_commit() {
Ok(commit) => writeln!(f, "commit: {}", commit).unwrap(),
Err(err) => println!("cargo:warning=Failed to fetch commit info: {}", err)
}
writeln!(f, "build date: {}", chrono::Utc::now().date_naive()).unwrap();
// extools bundling
println!("cargo:rustc-cfg=extools_bundle");
download_dll(
&out_dir,
"extools.dll",
"v0.0.7", // EXTOOLS_TAG
"https://github.com/tgstation/tgstation/raw/34f0cc6394a064b87cbd1d6cb225f1d3df444ba7/byond-extools.dll", // EXTOOLS_DLL_URL
"073dd08790a13580bae71758e9217917700dd85ce8d35cb030cef0cf5920fca8", // EXTOOLS_DLL_SHA256
);
// auxtools bundling
println!("cargo:rustc-cfg=auxtools_bundle");
download_dll(
&out_dir,
"debug_server.dll",
"v2.3.3", // DEBUG_SERVER_TAG
"https://github.com/willox/auxtools/releases/download/v2.3.3/debug_server.dll", // DEBUG_SERVER_DLL_URL
"8c3633d8237738be39a8c8f34bbd3d819cdaa270d1f30774c5007481fc32418c", // DEBUG_SERVER_DLL_SHA256
);
}
fn read_commit() -> Result<String, git2::Error> {
let repo = git2::Repository::discover(".")?;
let head = repo.head()?.peel_to_commit()?.id();
let mut all_tags = Vec::new();
repo.tag_foreach(|oid, _| { all_tags.push(oid); true })?;
let mut best = None;
for tag_id in all_tags {
if let Ok(possible_tag) = repo.find_tag(tag_id) {
let tag_commit = possible_tag.as_object().peel_to_commit()?.id();
let (ahead, behind) = repo.graph_ahead_behind(head, tag_commit)?;
if behind == 0 {
match best {
None => best = Some(ahead),
Some(prev) if ahead < prev => best = Some(ahead),
_ => {}
}
}
if ahead == 0 {
break;
}
}
}
match best {
None | Some(0) => {}
Some(ahead) => println!("cargo:rustc-env=CARGO_PKG_VERSION={}+{}", std::env::var("CARGO_PKG_VERSION").unwrap(), ahead),
}
Ok(head.to_string())
}
fn download_dll(out_dir: &Path, fname: &str, tag: &str, url: &str, sha256: &str) {
let full_path = out_dir.join(fname);
println!("cargo:rustc-env=BUNDLE_PATH_{}={}", fname, full_path.display());
println!("cargo:rustc-env=BUNDLE_VERSION_{}={}", fname, tag);
if let Ok(digest) = sha256::try_digest(&full_path) {
if digest == sha256 {
return;
}
}
std::io::copy(
&mut ureq::get(url).call().expect("Error downloading DLL to bundle").into_reader(),
&mut std::fs::File::create(full_path).unwrap(),
).unwrap();
}