This repository has been archived by the owner on Sep 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.rs
157 lines (126 loc) · 4.07 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use proc_macro2::TokenTree;
use std::env;
use std::ffi::OsStr;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use syn::{
visit::{self, Visit},
Macro,
};
use walkdir::WalkDir;
fn main() {
// Parse all .rs files to collect everything which implements Command.
// This code won't work properly with lib.rs or mod.rs.
let mut data = Data::new();
for entry in WalkDir::new("src").into_iter()
.filter_map(|e| e.ok())
.filter(|e| !e.file_type().is_dir())
.filter(|e| e.path().extension() == Some(OsStr::new("rs")))
{
let mut path = String::new();
for name in entry.path().with_extension("").iter().skip(1) {
path.push_str(&format!("::{}", name.to_str().unwrap()));
}
let entry_data = get_data(entry.path());
data.commands.extend(entry_data.commands
.into_iter()
.map(|c| format!("crate{}::{}", &path, c)));
data.cvars.extend(entry_data.cvars
.into_iter()
.map(|c| format!("crate{}::{}", &path, c)));
}
let command_array = make_command_array(data.commands);
let cvar_array = make_cvar_array(data.cvars);
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("command_array.rs");
let mut f = File::create(&dest_path).unwrap();
write!(f, "{}", command_array).unwrap();
let dest_path = Path::new(&out_dir).join("cvar_array.rs");
let mut f = File::create(&dest_path).unwrap();
write!(f, "{}", cvar_array).unwrap();
}
struct Data {
commands: Vec<String>,
cvars: Vec<String>,
}
impl Data {
fn new() -> Self {
Self { commands: Vec::new(),
cvars: Vec::new() }
}
}
fn get_data(path: &Path) -> Data {
let mut source = String::new();
File::open(path).unwrap()
.read_to_string(&mut source)
.unwrap();
if let Ok(file) = syn::parse_file(&source) {
let mut visitor = MyVisitor::new();
visitor.visit_file(&file);
Data { commands: visitor.commands,
cvars: visitor.cvars }
} else {
Data::new()
}
}
fn make_command_array(commands: Vec<String>) -> String {
let mut buf = format!("pub const COMMANDS: [&Command; {}] = [", commands.len());
let mut iter = commands.into_iter();
if let Some(first) = iter.next() {
buf.push_str(&format!("&{}", first));
}
for command in iter {
buf.push_str(&format!(", &{}", command));
}
buf.push_str("];");
buf
}
fn make_cvar_array(cvars: Vec<String>) -> String {
let mut buf = format!("pub static CVARS: [&crate::cvar::CVar; {}] = [",
cvars.len());
let mut iter = cvars.into_iter();
if let Some(first) = iter.next() {
buf.push_str(&format!("&{}", first));
}
for cvar in iter {
buf.push_str(&format!(", &{}", cvar));
}
buf.push_str("];");
buf
}
struct MyVisitor {
commands: Vec<String>,
cvars: Vec<String>,
}
impl MyVisitor {
fn new() -> Self {
Self { commands: Vec::new(),
cvars: Vec::new() }
}
}
impl<'ast> Visit<'ast> for MyVisitor {
fn visit_macro(&mut self, mac: &'ast Macro) {
if mac.path
.segments
.first()
.map(|x| x.value().ident == "command")
.unwrap_or(false)
{
if let Some(TokenTree::Ident(ident)) = mac.tts.clone().into_iter().next() {
self.commands.push(format!("{}", ident));
}
}
if mac.path
.segments
.first()
.map(|x| x.value().ident == "cvar")
.unwrap_or(false)
{
if let Some(TokenTree::Ident(ident)) = mac.tts.clone().into_iter().next() {
self.cvars.push(format!("{}", ident));
}
}
visit::visit_macro(self, mac);
}
}