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
use anyhow::{anyhow, Context, Result};
use std::path::{Path, PathBuf};
use tonic_build::Builder;
pub mod base;
pub mod tree;
pub fn build(
in_dir: &str,
out_dir: &str,
build_server: bool,
build_client: bool,
force: bool,
) -> Result<()> {
build_with_config(in_dir, out_dir, build_server, build_client, force, |c| c)
}
pub fn build_with_config(
in_dir: &str,
out_dir: &str,
build_server: bool,
build_client: bool,
force: bool,
user_config: impl FnOnce(Builder) -> Builder,
) -> Result<()> {
if !force && Path::new(out_dir).exists() {
return Err(anyhow!("the output directory already exists: {}", out_dir));
}
base::prepare_out_dir(out_dir).context("failed to prepare out dir")?;
compile(in_dir, out_dir, build_server, build_client, user_config)
.context("failed to compile the protos")?;
base::refactor(out_dir).context("failed to refactor the protos")?;
Ok(())
}
fn compile(
input_dir: &str,
output_dir: &str,
server: bool,
client: bool,
user_config: impl FnOnce(Builder) -> Builder,
) -> Result<(), anyhow::Error> {
let protos = crate::base::get_protos(input_dir).collect::<Vec<_>>();
let compile_includes: PathBuf = match Path::new(input_dir).parent() {
None => PathBuf::from("."),
Some(parent) => parent.to_path_buf(),
};
user_config(
tonic_build::configure()
.out_dir(output_dir)
.build_client(client)
.build_server(server),
)
.compile(&protos, &[compile_includes])?;
Ok(())
}