-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.rs
More file actions
95 lines (85 loc) · 2.99 KB
/
Copy pathbuild.rs
File metadata and controls
95 lines (85 loc) · 2.99 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
95
use std::collections::BTreeSet;
use std::env;
use std::fmt::Write as _;
use std::fs;
use std::path::{Path, PathBuf};
fn main() {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("manifest dir"));
let theme_root = manifest_dir.join("assets/fcitx5-themes");
println!("cargo:rerun-if-changed={}", theme_root.display());
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("out dir"));
let out_path = out_dir.join("fcitx5_theme_manifest.rs");
let mut theme_names = BTreeSet::new();
let mut files = Vec::new();
if theme_root.exists() {
collect_theme_files(
&manifest_dir,
&theme_root,
&theme_root,
&mut theme_names,
&mut files,
);
}
files.sort();
let mut output = String::new();
writeln!(&mut output, "pub const FCITX5_THEME_NAMES: &[&str] = &[").expect("write names");
for name in &theme_names {
writeln!(&mut output, " {:?},", name).expect("write theme name");
}
writeln!(&mut output, "];").expect("close names");
writeln!(
&mut output,
"pub const FCITX5_THEME_FILES: &[(&str, &str, &[u8])] = &["
)
.expect("write files");
for (theme_name, rel_path, manifest_rel_path) in files {
writeln!(
&mut output,
" ({:?}, {:?}, include_bytes!(concat!(env!(\"CARGO_MANIFEST_DIR\"), \"/{}\"))),",
theme_name, rel_path, manifest_rel_path
)
.expect("write theme file");
}
writeln!(&mut output, "];").expect("close files");
fs::write(out_path, output).expect("write manifest");
}
fn collect_theme_files(
manifest_dir: &Path,
theme_root: &Path,
current_dir: &Path,
theme_names: &mut BTreeSet<String>,
files: &mut Vec<(String, String, String)>,
) {
let entries = fs::read_dir(current_dir).expect("read assets directory");
for entry in entries {
let entry = entry.expect("read asset entry");
let path = entry.path();
if path.is_dir() {
collect_theme_files(manifest_dir, theme_root, &path, theme_names, files);
continue;
}
let rel_path = path.strip_prefix(theme_root).expect("theme relative path");
let mut components = rel_path.components();
let theme_name = components
.next()
.expect("theme directory")
.as_os_str()
.to_string_lossy()
.to_string();
if theme_name.is_empty() {
continue;
}
theme_names.insert(theme_name.clone());
let theme_rel_path = rel_path
.strip_prefix(Path::new(&theme_name))
.expect("theme file relative path")
.to_string_lossy()
.replace('\\', "/");
let manifest_rel_path = path
.strip_prefix(manifest_dir)
.expect("manifest relative path")
.to_string_lossy()
.replace('\\', "/");
files.push((theme_name, theme_rel_path, manifest_rel_path));
}
}