This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
116 lines (109 loc) · 3.88 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
#![allow(dead_code)]
use convert_case::{Case, Casing};
use std::fmt::Write as _;
use std::fs;
use std::path::Path;
fn main() {
generate_macro_mod("src/_macro");
generate_list_mod("src/action/_type");
generate_list_mod("src/actor/_type");
generate_list_mod("src/command/_type");
generate_list_mod("src/chunk/_type");
generate_list_mod_with_alias("src/component/_type", "Component");
generate_list_mod("src/effect/_type");
generate_list_mod_with_alias("src/event/_type", "Event");
generate_list_mod("src/marker/_type");
generate_list_mod("src/output/_type");
generate_list_mod_with_alias("src/resource/_type", "Resource");
generate_list_mod("src/passage/_type");
generate_list_mod("src/room/_type");
generate_list_mod_with_alias("src/system/_type", "System");
// generate_list_mod("src/game_state/_impl");
// generate_list_mod_with_alias("src/game_state/_trait", "Trait");
// generate_list_mod("src/game_state/_type");
// generate_list_mod("src/lookup_service/_impl");
// generate_list_mod_with_alias("src/lookup_service/_trait", "Trait");
// generate_list_mod_with_alias("src/system", "System");
// generate_list_mod_with_alias("src/system/_trait", "Trait");
}
fn generate_macro_mod(base_dir: &str) {
let path = format!("{}/_list", base_dir);
let dir = Path::new(&path);
let mut files = get_files_in_dir(dir);
files.sort();
let mut content = String::new();
write_mod_header(&mut content);
for file in &files {
writeln!(content, "#[macro_use]\npub mod {};", file).expect("Failed to write to content string");
}
fs::write(dir.join("mod.rs"), content).expect("Failed to write mod.rs");
}
fn write_mod_header(content: &mut String) {
writeln!(
content,
"// This file is automatically generated.\n// See <project_root>/build.rs for details.\n// Remember not to edit it manually."
)
.expect("Failed to write to content string");
}
fn generate_list_mod(base_dir: &str) {
let path = format!("{}/_list", base_dir);
let dir = Path::new(&path);
let mut subdirs = get_subdirs_in_dir(dir);
subdirs.sort();
let mut content = String::new();
write_mod_header(&mut content);
for subdir in &subdirs {
writeln!(content, "pub mod {};", subdir).expect("Failed to write to content string");
writeln!(content, "pub use {}::*;", subdir).expect("Failed to write to content string");
}
fs::write(dir.join("mod.rs"), content).expect("Failed to write mod.rs");
}
fn generate_list_mod_with_alias(base_dir: &str, suffix: &str) {
let path = format!("{}/_list", base_dir);
let dir = Path::new(&path);
let mut subdirs = get_subdirs_in_dir(dir);
subdirs.sort();
let mut content = String::new();
write_mod_header(&mut content);
for subdir in &subdirs {
let struct_name = format!("{}{}", subdir.to_case(Case::UpperCamel), suffix);
writeln!(content, "pub mod {};", subdir).expect("Failed to write to content string");
writeln!(
content,
"pub use {}::{} as {};",
subdir,
subdir.to_case(Case::UpperCamel),
struct_name
)
.expect("Failed to write to content string");
}
fs::write(dir.join("mod.rs"), content).expect("Failed to write mod.rs");
}
fn get_files_in_dir(dir: &Path) -> Vec<String> {
fs::read_dir(dir)
.unwrap_or_else(|_| panic!("Failed to read directory {:?}", dir))
.filter_map(|e| {
if let Ok(entry) = e {
let path = entry.path();
if path.extension()? == "rs" && path.file_stem()? != "mod" {
return Some(path.file_stem()?.to_string_lossy().into_owned());
}
}
None
})
.collect()
}
fn get_subdirs_in_dir(dir: &Path) -> Vec<String> {
fs::read_dir(dir)
.unwrap_or_else(|_| panic!("Failed to read directory {:?}", dir))
.filter_map(|e| {
if let Ok(entry) = e {
let path = entry.path();
if path.is_dir() {
return Some(path.file_name()?.to_string_lossy().into_owned());
}
}
None
})
.collect()
}