-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.rs
62 lines (56 loc) · 2.96 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
use prost_wkt_build::*;
use std::{env, io::Result, path::PathBuf};
const PROTO_DEFAULT_DIR: &str = "proto";
fn main() -> Result<()> {
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
let proto_dir: PathBuf =
PathBuf::from(env::var("PROTO_DIR").unwrap_or(PROTO_DEFAULT_DIR.to_string()));
let proto_files = &[
"proto/openfga/v1/authzmodel.proto",
"proto/openfga/v1/errors_ignore.proto",
"proto/openfga/v1/openapi.proto",
"proto/openfga/v1/openfga.proto",
"proto/openfga/v1/openfga_service.proto",
];
let includes = &[proto_dir
.to_str()
.expect("expected proto dir to convert to str")];
let descriptor_file = out.join("descriptors.bin");
let mut prost_build = tonic_build::Config::new();
prost_build
.type_attribute(".", "#[derive(serde::Serialize,serde::Deserialize)]")
.extern_path(".google.protobuf.Timestamp", "::prost_wkt_types::Timestamp")
.extern_path(".google.protobuf.Struct", "::prost_wkt_types::Struct")
.extern_path(".google.protobuf.Value", "::prost_wkt_types::Value")
.extern_path(".google.protobuf.ListValue", "::prost_wkt_types::ListValue")
.extern_path(".google.protobuf.Any", "::prost_wkt_types::Any")
.file_descriptor_set_path(&descriptor_file);
tonic_build::configure()
.build_server(false)
.build_client(true)
.compile_protos_with_config(prost_build, proto_files, includes)
.unwrap_or_else(|e| {
let current_dir = env::current_dir()
.expect("expected to get current dir")
.into_os_string()
.into_string()
.expect("expected to convert os string of current dir to string");
let current_dir_contents = std::fs::read_dir(¤t_dir)
.expect("expected to read current dir")
.map(|entry| entry.expect("expected dir to entry to unwrap ok").file_name().into_string().expect("expected to convert os string of current dir to string"))
.collect::<Vec<String>>()
.join(",\n");
let out_dir = out.display().to_string();
let out_dir_contents = std::fs::read_dir(&out_dir)
.expect("expected to read out dir")
.map(|entry| entry.expect("expected dir entry to unwrap ok").file_name().into_string().expect("expected to convert os string of current dir to string"))
.collect::<Vec<String>>()
.join(",\n");
panic!("failed to compile protos, error: {e}, current dir: {current_dir}, current dir contents: {current_dir_contents}, out_dir: {out_dir}, out_dir contents: {out_dir_contents}")
});
let descriptor_bytes = std::fs::read(descriptor_file).expect("failed to read descriptor file");
let descriptor =
FileDescriptorSet::decode(&descriptor_bytes[..]).expect("failed to decode descriptor file");
prost_wkt_build::add_serde(out, descriptor);
Ok(())
}