-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.rs
114 lines (84 loc) · 3.57 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
use std::{error::Error, fs::File, io::Write, path::PathBuf};
use contribs::contributors::Contributors;
use dotenv::dotenv;
#[path = "build/mod.rs"]
mod build;
use build::tools::{colours::Colours, lock::Lockfile, version::SprinklesVersion};
const WIN_MANIFEST: &str = include_str!("./sfsu.exe.manifest");
fn get_contributors((owner, repo): (&str, &str)) -> Result<String, Box<dyn Error>> {
// Try and load dotenv file
_ = dotenv();
if let Ok(api_key) = std::env::var("CONTRIBUTORS_TOKEN") {
let contributors = Contributors::new(api_key, owner.into(), repo.into())?;
let contributors =
tokio::runtime::Runtime::new()?.block_on(async move { contributors.await })?;
let contributors = contributors
.into_iter()
.filter_map(|contrib| {
let name = contrib.name.as_ref().or(contrib.login.as_ref())?.clone();
if name.contains("[bot]") || name == "jewlexx" {
return None;
}
let login = contrib.login.as_ref()?.clone();
let url = format!("https://github.com/{login}");
Some(format!("(\"{name}\",\"{url}\")"))
})
.collect::<Vec<_>>();
let length = contributors.len();
let contributors = format!("[{}]", contributors.join(", "));
let contributors_output =
format!("pub const CONTRIBUTORS: [(&str, &str); {length}] = {contributors};");
Ok(contributors_output)
} else {
if std::env::var("IS_RELEASE").is_ok() {
panic!("No CONTRIBUTORS_TOKEN found, contributors will not be updated.");
}
Ok("pub const CONTRIBUTORS: [(&str, &str); 0] = [];".to_string())
}
}
#[allow(unused_variables, unreachable_code)]
fn main() -> Result<(), Box<dyn Error>> {
let out_dir = PathBuf::from(std::env::var("OUT_DIR")?);
println!("cargo:rerun-if-changed=sfsu.exe.manifest");
let mut res = winres::WindowsResource::new();
res.set_manifest(WIN_MANIFEST);
res.compile().expect("Failed to compile Windows resources");
let lockfile = Lockfile::new();
let shadow = shadow_rs::ShadowBuilder::builder()
.hook(append_shadow_hooks)
.build_pattern(shadow_rs::BuildPattern::RealTime)
.build()?;
std::fs::write(
out_dir.join("long_version.txt"),
SprinklesVersion::from_doc(&lockfile).long_version(&shadow),
)?;
Ok(())
}
fn append_shadow_hooks(mut file: &File) -> shadow_rs::SdResult<()> {
let sfsu_contribs = {
let contributors = get_contributors(("winpax", "sfsu"));
match contributors {
Ok(contributors) => contributors,
Err(e) if std::env::var("IS_RELEASE").is_ok_and(|v| v == "true") => {
panic!("Getting contributors failed with error: {e}");
}
_ => "pub const CONTRIBUTORS: [(&str, &str); 0] = [];".to_string(),
}
};
writeln!(file, "pub mod sfsu {{\n{sfsu_contribs}\n}}")?;
let sprinkles_contribs = {
let contributors = get_contributors(("winpax", "sprinkles"));
match contributors {
Ok(contributors) => contributors,
Err(e) if std::env::var("IS_RELEASE").is_ok_and(|v| v == "true") => {
panic!("Getting contributors failed with error: {e}");
}
_ => "pub const CONTRIBUTORS: [(&str, &str); 0] = [];".to_string(),
}
};
writeln!(file, "pub mod sprinkles {{\n{sprinkles_contribs}\n}}")?;
let lockfile = Lockfile::new();
lockfile.hook(file)?;
Colours::hook(file)?;
Ok(())
}