-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
main.rs
64 lines (55 loc) · 1.68 KB
/
main.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
use clap::Parser;
use std::{fs, path::PathBuf};
use color_eyre::{Result, eyre::{ensure, WrapErr}};
/// tokio-console dev tasks
#[derive(Debug, clap::Parser)]
struct Args {
#[clap(subcommand)]
cmd: Command,
}
#[derive(Debug, clap::Subcommand)]
enum Command {
/// Generate `console-api` protobuf bindings.
GenProto,
}
fn main() -> Result<()> {
color_eyre::install()?;
Args::parse().cmd.run()
}
impl Command {
fn run(&self) -> Result<()> {
match self {
Self::GenProto => gen_proto(),
}
}
}
fn gen_proto() -> Result<()> {
eprintln!("generating `console-api` protos...");
let api_dir = {
let mut mydir = PathBuf::from(std::env!("CARGO_MANIFEST_DIR"));
ensure!(mydir.pop(), "manifest path should not be relative!");
mydir.join("console-api")
};
let proto_dir = api_dir.join("proto");
let proto_files = fs::read_dir(&proto_dir).with_context(|| format!("failed to read protobuf directory `{}`", proto_dir.display()))?
.filter_map(|entry| {
(|| {
let entry = entry?;
if entry.file_type()?.is_dir() {
return Ok(None);
}
Ok(Some(entry.path()))
})()
.transpose()
})
.collect::<Result<Vec<_>>>()?;
let out_dir = api_dir.join("src").join("generated");
tonic_build::configure()
.build_client(true)
.build_server(true)git
.emit_rerun_if_changed(false)
.protoc_arg("--experimental_allow_proto3_optional")
.out_dir(&out_dir)
.compile(&proto_files[..], &[proto_dir])
.context("failed to compile protobuf files")
}