forked from starship/starship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
72 lines (63 loc) · 1.73 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
use std::fs::{self, File};
use std::io::Write;
use shadow_rs::SdResult;
fn main() -> SdResult<()> {
shadow_rs::new().map_err(|err| err.to_string())?;
shadow_rs::new_hook(gen_presets_hook)?;
#[cfg(windows)]
{
let mut res = winres::WindowsResource::new();
res.set_manifest_file("starship.exe.manifest")
.set_icon("media/icon.ico");
res.compile()?;
}
Ok(())
}
fn gen_presets_hook(mut file: &File) -> SdResult<()> {
println!("cargo:rerun-if-changed=docs/.vuepress/public/presets/toml");
let paths = fs::read_dir("docs/.vuepress/public/presets/toml")?;
let mut presets = String::new();
let mut match_arms = String::new();
for path in paths {
let unwrapped = path?;
let file_name = unwrapped.file_name();
let full_path = dunce::canonicalize(unwrapped.path())?;
let full_path = full_path.to_str().expect("failed to convert to string");
let name = file_name
.to_str()
.and_then(|v| v.strip_suffix(".toml"))
.expect("Failed to process filename");
presets.push_str(format!("print::Preset(\"{}\"),\n", name).as_str());
match_arms.push_str(
format!(
r#"
"{name}" => {{
let stdout = io::stdout();
let mut stdout = stdout.lock();
let _ = stdout.write_all(include_bytes!(r"{full_path}"));
}}
"#
)
.as_str(),
);
}
writeln!(
file,
r#"
use std::io::{{self, Write}};
use crate::print;
pub fn get_preset_list<'a>() -> &'a [print::Preset] {{
&[
{presets}
]
}}
pub fn print_preset_content(name: &str) {{
match name {{
{match_arms}
_ => {{}}
}}
}}
"#
)?;
Ok(())
}