-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
185 lines (171 loc) · 7.74 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use handlebars::Handlebars;
use std::{path::PathBuf, io::Write, os::unix::prelude::PermissionsExt};
const RESS_TEMPLATE: &str = include_str!("templates/ress_bin.rs.handlebars");
const RESSA_TEMPLATE: &str = include_str!("templates/ressa_bin.rs.handlebars");
const LIBS: &[MajorLib] = &[
MajorLib::Angular,
MajorLib::Dexie,
MajorLib::Everything,
MajorLib::Everything2015,
MajorLib::Jquery,
MajorLib::Moment,
MajorLib::React,
MajorLib::ReactDom,
MajorLib::Vue,
];
fn main() {
let proj_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let scripts_dir = proj_dir.join("scripts");
let src_dir = proj_dir.join("src");
let bin_dir = src_dir.join("bin");
std::fs::remove_dir_all(&bin_dir).ok();
std::fs::create_dir_all(&bin_dir).expect("Failed to create src/bin");
let reg = Handlebars::new();
let mut bin_names = Vec::new();
for &lib in LIBS {
if let Some(js_path) = lib.path() {
let ress_bin = reg.render_template(RESS_TEMPLATE, &serde_json::json!({
"js_path": js_path,
})).unwrap_or_else(|e| {
panic!("Failed to render ress-{}: {e}", lib.name());
});
let bin_name = format!("ress_{}", lib.name());
bin_names.push(bin_name.clone());
let bin_path = bin_dir.join(&format!("{bin_name}.rs"));
std::fs::write(&bin_path, ress_bin).unwrap();
let bin_name = format!("ressa_{}", lib.name());
bin_names.push(bin_name.clone());
let bin_path = bin_dir.join(&format!("{bin_name}.rs"));
let ressa_bin = reg.render_template(RESSA_TEMPLATE, &serde_json::json!({
"js_path": js_path,
"is_module": false,
})).unwrap_or_else(|e| {
panic!("Failed to render ressa-{}: {e}", lib.name());
});
std::fs::write(&bin_path, ressa_bin).unwrap();
}
if let Some(js_path) = lib.path_min() {
let ress_bin = reg.render_template(RESS_TEMPLATE, &serde_json::json!({
"js_path": js_path,
})).unwrap_or_else(|e| {
panic!("Failed to render ress-{}: {e}", lib.name());
});
let bin_name = format!("ress_{}_min", lib.name());
bin_names.push(bin_name.clone());
let bin_path = bin_dir.join(&format!("{bin_name}.rs"));
std::fs::write(&bin_path, ress_bin).unwrap();
let bin_name = format!("ressa_{}_min", lib.name());
bin_names.push(bin_name.clone());
let bin_path = bin_dir.join(&format!("{bin_name}.rs"));
let ressa_bin = reg.render_template(RESSA_TEMPLATE, &serde_json::json!({
"js_path": js_path,
"is_module": false,
})).unwrap_or_else(|e| {
panic!("Failed to render ressa-{}: {e}", lib.name());
});
std::fs::write(&bin_path, ressa_bin).unwrap();
}
if let Some(js_path) = lib.path_mod() {
let ress_bin = reg.render_template(RESS_TEMPLATE, &serde_json::json!({
"js_path": js_path,
})).unwrap_or_else(|e| {
panic!("Failed to render ress-{}: {e}", lib.name());
});
let bin_name = format!("ress_{}_mod", lib.name());
bin_names.push(bin_name.clone());
let bin_path = bin_dir.join(&format!("{bin_name}.rs"));
std::fs::write(&bin_path, ress_bin).unwrap();
let bin_name = format!("ressa_{}_mod", lib.name());
bin_names.push(bin_name.clone());
let bin_path = bin_dir.join(&format!("{bin_name}.rs"));
let ressa_bin = reg.render_template(RESSA_TEMPLATE, &serde_json::json!({
"js_path": js_path,
"is_module": true,
})).unwrap_or_else(|e| {
panic!("Failed to render ressa-{}: {e}", lib.name());
});
std::fs::write(&bin_path, ressa_bin).unwrap();
}
}
let script_path = scripts_dir.join("compile_all.sh");
std::fs::remove_file(&script_path).ok();
let mut file = std::fs::File::create(&script_path).unwrap();
file.write_all(b"#! /bin/bash\n").unwrap();
for bin in bin_names {
file.write_all(format!("\nBIN_PATH=$(cargo build --release --bin {bin} --message-format json | tail -n2 | head -n1 | jq -r '.executable')").as_bytes()).unwrap();
file.write_all(format!("\ncp $BIN_PATH ./bins\n").as_bytes()).unwrap();
}
std::fs::set_permissions(&script_path, PermissionsExt::from_mode(0x16D)).unwrap();
}
#[derive(Debug, Clone, Copy)]
enum MajorLib {
Angular,
Dexie,
Everything,
Everything2015,
Jquery,
Moment,
React,
ReactDom,
Vue,
}
impl MajorLib {
const fn name(self) -> &'static str {
match self {
MajorLib::Angular => "angular",
MajorLib::Dexie => "dexie",
MajorLib::Everything => "everything",
MajorLib::Everything2015 => "everything2015",
MajorLib::Jquery => "jquery",
MajorLib::Moment => "moment",
MajorLib::React => "react",
MajorLib::ReactDom => "react_dom",
MajorLib::Vue => "vue",
}
}
const fn path(self) -> Option<&'static str> {
match self {
MajorLib::Angular => Some("node_modules/angular/angular.js"),
MajorLib::Dexie => Some("node_modules/dexie/dist/dexie.js"),
MajorLib::Everything => Some("node_modules/everything.js/es5.js"),
MajorLib::Everything2015 => Some("node_modules/everything.js/es2015-script.js"),
MajorLib::Jquery => Some("node_modules/jquery/dist/jquery.js"),
MajorLib::Moment => Some("node_modules/moment/min/moment-with-locales.js"),
MajorLib::React => Some("node_modules/react/umd/react.development.js"),
MajorLib::ReactDom => Some("node_modules/react-dom/umd/react-dom.development.js"),
// TODO: this errors on lexical redecl for the name `get` once as a `const` and once
// as a function expressions with an identifier
// MajorLib::Vue => Some("node_modules/vue/dist/vue.global.js"),
MajorLib::Vue => None,
}
}
const fn path_min(self) -> Option<&'static str> {
match self {
MajorLib::Angular => Some("node_modules/angular/angular.min.js"),
MajorLib::Dexie => Some("node_modules/dexie/dist/dexie.min.js"),
MajorLib::Everything => None,
MajorLib::Everything2015 => None,
MajorLib::Jquery => Some("node_modules/jquery/dist/jquery.min.js"),
MajorLib::Moment => Some("node_modules/moment/min/moment.min.js"),
MajorLib::React => Some("node_modules/react/umd/react.production.min.js"),
MajorLib::ReactDom => Some("node_modules/react-dom/umd/react-dom.production.min.js"),
MajorLib::Vue => Some("node_modules/vue/dist/vue.global.prod.js"),
}
}
const fn path_mod(self) -> Option<&'static str> {
match self {
MajorLib::Angular => None,
// TODO: this has a duplicate export error since the `as` alias for `Dexie$1` should
// be enough to not create a duplicate export
// MajorLib::Dexie => Some("node_modules/dexie/dist/dexie.mjs"),
MajorLib::Dexie => None,
MajorLib::Everything => None,
MajorLib::Everything2015 => Some("node_modules/everything.js/es2015-module.js"),
MajorLib::Jquery => None,
MajorLib::Moment => Some("node_modules/moment/dist/moment.js"),
MajorLib::React => None,
MajorLib::ReactDom => None,
MajorLib::Vue => Some("node_modules/vue/dist/vue.esm-browser.js"),
}
}
}