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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use include_dir::{include_dir, Dir};
use std::{
convert::AsRef,
fs,
io::Write,
iter::IntoIterator,
os::unix::fs::PermissionsExt,
path::{Path, PathBuf},
process::{Command, Output},
};
use tempdir::TempDir;
use walkdir::WalkDir;
const CODEGEN: Dir = include_dir!("codegen");
pub fn gen_protos<P: AsRef<Path>>(src_paths: Vec<P>) {
GenProtos::builder().src_paths(src_paths).gen_protos()
}
pub struct GenProtos {
gen_path: PathBuf,
src_paths: Vec<PathBuf>,
include_paths: Vec<PathBuf>,
include_extensions: bool,
cleanup_out_path: bool,
}
impl std::default::Default for GenProtos {
fn default() -> Self {
let gen_path = get_cargo_manifest_path()
.expect("couldn't get `CARGO_MANIFEST_DIR` when building default GenProtos");
let gen_path = gen_path.join(PathBuf::from("./gen"));
let src_paths = vec![];
let include_paths = vec![];
let include_extensions = true;
let cleanup_out_path = false;
GenProtos {
gen_path,
src_paths,
include_paths,
include_extensions,
cleanup_out_path,
}
}
}
impl GenProtos {
pub fn builder() -> GenProtos {
GenProtos::default()
}
pub fn out_path<P: AsRef<Path>>(mut self, path: P) -> GenProtos {
let manifest_path = get_cargo_manifest_path().expect("out_path");
self.gen_path = manifest_path.join(path);
self
}
pub fn abs_out_path<P: AsRef<Path>>(mut self, path: P) -> GenProtos {
self.gen_path = path.as_ref().to_owned();
self
}
pub fn src_path<P: AsRef<Path>>(mut self, path: P) -> GenProtos {
self.src_paths.push(path.as_ref().to_owned());
self
}
pub fn src_paths<P: AsRef<Path>, I: IntoIterator<Item = P>>(mut self, paths: I) -> GenProtos {
self.src_paths
.extend(paths.into_iter().map(|p| p.as_ref().to_owned()));
self
}
pub fn include_path<P: AsRef<Path>>(mut self, path: P) -> GenProtos {
self.include_paths.push(path.as_ref().to_owned());
self
}
pub fn include_paths<P: AsRef<Path>, I: IntoIterator<Item = P>>(
mut self,
paths: I,
) -> GenProtos {
self.include_paths
.extend(paths.into_iter().map(|p| p.as_ref().to_owned()));
self
}
pub fn include_extensions(mut self, include: bool) -> GenProtos {
self.include_extensions = include;
self
}
pub fn cleanup_out_path(mut self, cleanup: bool) -> GenProtos {
self.cleanup_out_path = cleanup;
self
}
pub fn gen_protos(self) {
let output = self.gen_protos_helper()
.expect("something went wrong in generating Rust bindings 🤮");
if !output.status.success() {
dbg!(output);
panic!("Failed to generate Rust bindings to proto files!")
}
}
}
impl GenProtos {
fn gen_protos_helper(self) -> std::io::Result<Output> {
if self.cleanup_out_path && self.gen_path.exists() && self.gen_path.is_dir() {
fs::remove_dir_all(&self.gen_path)?;
fs::create_dir(&self.gen_path)?;
}
let temp_dir = self.create_temp_files()?;
self.gen_rust_protos(temp_dir)
}
fn gen_rust_protos(&self, temp_dir: TempDir) -> std::io::Result<Output> {
let codegen_path = temp_dir.path().join("codegen.py");
let mut rust_cmd = Command::new("protoc");
dbg!("Include paths");
for path in self.src_paths.iter() {
rust_cmd.arg("-I");
rust_cmd.arg(path);
dbg!(path);
}
if self.include_extensions {
let ext_path = temp_dir.path();
rust_cmd.arg("-I");
rust_cmd.arg(ext_path);
dbg!(ext_path);
}
for path in self.include_paths.iter() {
rust_cmd.arg("-I");
rust_cmd.arg(path);
dbg!(path);
}
rust_cmd.arg(format!(
"--plugin=protoc-gen-rust={}",
codegen_path.to_str().unwrap()
));
rust_cmd.arg(format!(
"--rust_out={}",
self.gen_path.canonicalize().unwrap().to_str().unwrap()
));
let proto_paths = self
.src_paths
.iter()
.map(|path| {
WalkDir::new(path)
.into_iter()
.filter_map(Result::ok)
.filter(|file| file.path().extension().unwrap_or_default() == "proto")
.map(|file| file.into_path())
})
.flatten();
dbg!("Proto paths");
for path in proto_paths {
dbg!(&path);
rust_cmd.arg(path);
}
rust_cmd.output()
}
fn create_temp_files(&self) -> std::io::Result<TempDir> {
let temp_dir = TempDir::new("codegen")?;
dbg!(temp_dir.path());
fn create_temp_files_helper(dir: &Dir, temp_dir: &TempDir) -> std::io::Result<()> {
for file in dir.files() {
let blob_path = file.path();
let abs_path = temp_dir.path().join(blob_path);
let mut abs_file = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&abs_path)?;
abs_file.write_all(file.contents())?;
let mut permissions = abs_file.metadata()?.permissions();
permissions.set_mode(0o777);
drop(abs_file);
fs::set_permissions(&abs_path, permissions)?;
}
for dir in dir.dirs() {
let blob_path = dir.path();
let abs_path = temp_dir.path().join(blob_path);
fs::create_dir(&abs_path)?;
create_temp_files_helper(dir, temp_dir)?;
}
Ok(())
}
create_temp_files_helper(&CODEGEN, &temp_dir)?;
Ok(temp_dir)
}
}
#[doc(hidden)]
fn get_cargo_manifest_path() -> std::io::Result<PathBuf> {
let path_str = std::env::var("CARGO_MANIFEST_DIR").map_err(|_| std::io::ErrorKind::NotFound)?;
Ok(PathBuf::from(path_str))
}