-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
80 lines (71 loc) · 1.97 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
use std::path::Path;
use std::path::PathBuf;
use walkdir::{DirEntry, WalkDir};
#[derive(Debug)]
#[allow(unused)]
struct Package {
path: PathBuf,
name: String,
components: Vec<String>,
}
impl Package {
fn from_path(base: &Path, path: PathBuf) -> Package {
let components: Vec<String> = path
.strip_prefix(base)
.unwrap()
.components()
.filter_map(|component| match component {
std::path::Component::Normal(path) => path.to_str().map(ToOwned::to_owned),
_ => None,
})
.collect();
Package {
path,
name: components
.iter()
.last()
.and_then(|v| Path::new(v).file_stem())
.and_then(|v| v.to_str().map(ToOwned::to_owned))
.unwrap(),
components,
}
}
}
fn main() {
let base = Path::new("./proto");
let dir = WalkDir::new("./proto");
let entries = dir
.into_iter()
.filter_entry(|e| !is_hidden(e))
.collect::<Result<Vec<_>, _>>()
.unwrap();
let packages: Vec<_> = entries
.into_iter()
.filter_map(|entry| {
if entry.file_type().is_file() {
Some(Package::from_path(base, entry.into_path()))
} else {
None
}
})
.collect();
let mut builder = tonic_build::configure();
builder = builder.build_client(false).build_server(false);
#[cfg(feature = "client")]
{
builder = builder.build_client(true);
}
#[cfg(feature = "server")]
{
builder = builder.build_server(true);
}
let paths: Vec<&Path> = packages.iter().map(|p| p.path.as_ref()).collect();
builder.compile(&paths, &[Path::new("./proto")]).unwrap();
}
fn is_hidden(entry: &DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| s.starts_with("."))
.unwrap_or(false)
}