beteran-protobuf/build.rs
2022-08-08 01:46:20 +00:00

132 lines
4.4 KiB
Rust

//! client client
//! NATS(c2se) NATS(c2se)
//! <---> <--->
//! client broker
//! <---> <--->
//! NATS(se2c) NATS(se2c)
//! server edge server edge
//! NATS(se) NATS(se)
//! <---> <--->
//! server service server broker server service
//! NATS(ss) <---> <---> NATS(ss)
//!
use once_cell::sync::Lazy;
use std::{collections::HashMap, fs, path::Path, process::Command};
static PROTO_PATHS: Lazy<Vec<&str>> =
Lazy::new(|| vec!["/usr/local/include/protobuf/", "./proto/"]);
static TARGETS: Lazy<HashMap<&str, Vec<&str>>> = Lazy::new(|| {
println!("initializing");
let mut m = HashMap::new();
m.insert(
"protobuf",
vec![
"./proto/protobuf/rpc/error.proto",
"./proto/protobuf/pagination/pagination.proto",
"./proto/protobuf/pagination/search.proto",
"./proto/protobuf/pagination/sort.proto",
],
);
m.insert(
"models",
vec![
"./proto/models/core/network.proto",
"./proto/models/domain/site.proto",
"./proto/models/rbac/resource.proto",
"./proto/models/rbac/resource_action.proto",
"./proto/models/rbac/role.proto",
"./proto/models/member/member_class.proto",
"./proto/models/member/member_level.proto",
"./proto/models/member/member.proto",
"./proto/models/member/member_session.proto",
],
);
m.insert(
"c2se",
vec![
"./proto/c2se/core/network.proto",
"./proto/c2se/common/identity.proto",
"./proto/c2se/backend/identity.proto",
"./proto/c2se/backend/member.proto",
"./proto/c2se/frontend/identity.proto",
],
);
m.insert("ss", vec!["./proto/ss/member/identity.proto"]);
m
});
fn main() {
let build_path = "./build";
if Path::new(build_path).exists() {
fs::remove_dir_all(build_path).expect("clean for library directory is failed");
}
fs::create_dir_all(build_path).expect("creating for library directory is failed");
let build_path_rust = format!("{}/rust", build_path);
if Path::new(&build_path_rust).exists() {
fs::remove_dir_all(&build_path_rust).expect("clean for rust library directory is failed");
}
fs::create_dir_all(&build_path_rust).expect("creating for rust library directory is failed");
let build_path_javascript = format!("{}/javascript", build_path);
if Path::new(&build_path_javascript).exists() {
fs::remove_dir_all(&build_path_javascript)
.expect("clean for javascript library directory is failed");
}
fs::create_dir_all(&build_path_javascript)
.expect("creating for javascript library directory is failed");
let proto_paths: Vec<_> = PROTO_PATHS
.iter()
.map(|v| format!("--proto_path={}", *v))
.collect();
eprintln!("proto_paths: {:?}", proto_paths);
for (key, protos) in TARGETS.iter() {
let build_path_rust_sub = format!("{}/{}", &build_path_rust, key);
fs::create_dir_all(&build_path_rust_sub)
.expect("creating for rust library sub directory is failed");
let mut prost_build = prost_build::Config::new();
prost_build.out_dir(build_path_rust_sub);
prost_build
.compile_protos(protos.as_slice(), PROTO_PATHS.as_slice())
.expect("generating library for rust is failed");
let output = Command::new("protoc")
.args(proto_paths.as_slice())
.args(["--experimental_allow_proto3_optional"])
.args(&[
&format!(
"--js_out=import_style=commonjs,binary:{}",
&build_path_javascript
),
&format!("--ts_out={}", &build_path_javascript),
])
.args(protos.as_slice())
.output()
.expect("generating library for javascript is failed");
eprintln!(
"generating library for javascript status: {}",
output.status.success()
);
eprintln!(
"generating library for typescript stdout: {}",
String::from_utf8_lossy(&output.stdout)
);
eprintln!(
"generating library for typescript stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
}
}