beteran-protobuf/build.rs

169 lines
5.7 KiB
Rust
Raw Normal View History

2022-07-12 07:02:38 +00:00
//! 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![
2022-08-13 20:18:21 +00:00
"./proto/protobuf/rpc.proto",
"./proto/protobuf/pagination.proto",
2022-07-12 07:02:38 +00:00
],
);
m.insert(
"models",
vec![
2022-07-15 06:14:09 +00:00
"./proto/models/core/network.proto",
2022-08-13 20:18:21 +00:00
"./proto/models/bank.proto",
"./proto/models/member_bank_account.proto",
"./proto/models/member_bank_deposit.proto",
"./proto/models/member_bank_withdraw.proto",
"./proto/models/member_class.proto",
"./proto/models/member_level.proto",
"./proto/models/member_permission.proto",
"./proto/models/member_role.proto",
"./proto/models/member_session.proto",
"./proto/models/member.proto",
"./proto/models/resource_action.proto",
"./proto/models/resource.proto",
"./proto/models/role.proto",
"./proto/models/site.proto",
2022-08-18 03:40:03 +00:00
"./proto/models/api/vendor.proto",
2022-07-12 07:02:38 +00:00
],
);
m.insert(
"c2se",
vec![
2022-08-13 20:18:21 +00:00
"./proto/c2se/bank.proto",
"./proto/c2se/identity.proto",
"./proto/c2se/member_bank_account.proto",
"./proto/c2se/member_bank_deposit.proto",
"./proto/c2se/member_bank_withdraw.proto",
"./proto/c2se/member_class.proto",
"./proto/c2se/member_level.proto",
"./proto/c2se/member.proto",
"./proto/c2se/site.proto",
2022-07-27 06:45:53 +00:00
"./proto/c2se/core/network.proto",
2022-08-18 03:40:03 +00:00
"./proto/c2se/api/vendor.proto",
2022-08-13 20:18:21 +00:00
"./proto/c2se/backend/bank.proto",
2022-07-12 07:02:38 +00:00
"./proto/c2se/backend/identity.proto",
2022-08-13 20:18:21 +00:00
"./proto/c2se/backend/member_bank_account.proto",
"./proto/c2se/backend/member_bank_deposit.proto",
"./proto/c2se/backend/member_bank_withdraw.proto",
2022-08-12 08:07:43 +00:00
"./proto/c2se/backend/member_class.proto",
"./proto/c2se/backend/member_level.proto",
2022-07-12 07:02:38 +00:00
"./proto/c2se/backend/member.proto",
2022-08-13 20:18:21 +00:00
"./proto/c2se/backend/site.proto",
2022-08-18 03:40:03 +00:00
"./proto/c2se/backend/api/vendor.proto",
2022-08-13 20:18:21 +00:00
"./proto/c2se/frontend/bank.proto",
2022-07-12 07:02:38 +00:00
"./proto/c2se/frontend/identity.proto",
],
);
2022-08-08 10:10:57 +00:00
m.insert(
"ss",
vec![
2022-08-13 20:18:21 +00:00
"./proto/ss/bank.proto",
"./proto/ss/identity.proto",
"./proto/ss/member_bank_account.proto",
"./proto/ss/member_bank_deposit.proto",
"./proto/ss/member_bank_withdraw.proto",
"./proto/ss/member_class.proto",
"./proto/ss/member_level.proto",
"./proto/ss/member.proto",
"./proto/ss/site.proto",
2022-08-18 03:40:03 +00:00
"./proto/ss/api/vendor.proto",
2022-08-08 10:10:57 +00:00
],
);
2022-07-15 06:14:09 +00:00
2022-07-12 07:02:38 +00:00
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)
);
}
}