|
@@ -1,10 +1,12 @@
|
|
|
+#![allow(clippy::all)]
|
|
|
#![allow(unused_imports)]
|
|
|
#![allow(unused_attributes)]
|
|
|
+#![allow(dead_code)]
|
|
|
use std::fs::File;
|
|
|
use std::io::Write;
|
|
|
+use std::process::Command;
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
-#[allow(dead_code)]
|
|
|
pub fn gen(name: &str, root: &str) {
|
|
|
let mut paths = vec![];
|
|
|
let mut file_names = vec![];
|
|
@@ -20,9 +22,10 @@ pub fn gen(name: &str, root: &str) {
|
|
|
file_names.push(file_name);
|
|
|
}
|
|
|
}
|
|
|
+ println!("cargo:rerun-if-changed=build.rs");
|
|
|
|
|
|
- // #[cfg(feature = "dart")]
|
|
|
- // gen_pb_for_dart(name, root, &paths, &file_names);
|
|
|
+ #[cfg(feature = "dart")]
|
|
|
+ gen_pb_for_dart(name, root, &paths, &file_names);
|
|
|
|
|
|
protoc_rust::Codegen::new()
|
|
|
.out_dir("./src/protobuf/model")
|
|
@@ -33,7 +36,6 @@ pub fn gen(name: &str, root: &str) {
|
|
|
}
|
|
|
|
|
|
#[cfg(feature = "dart")]
|
|
|
-#[allow(dead_code)]
|
|
|
fn gen_pb_for_dart(name: &str, root: &str, paths: &Vec<String>, file_names: &Vec<String>) {
|
|
|
let output = format!(
|
|
|
"{}/{}/{}",
|
|
@@ -44,13 +46,17 @@ fn gen_pb_for_dart(name: &str, root: &str, paths: &Vec<String>, file_names: &Vec
|
|
|
if !std::path::Path::new(&output).exists() {
|
|
|
std::fs::create_dir_all(&output).unwrap();
|
|
|
}
|
|
|
+ check_pb_compiler();
|
|
|
+
|
|
|
+ check_pb_dart_plugin();
|
|
|
+
|
|
|
paths.iter().for_each(|path| {
|
|
|
if cmd_lib::run_cmd! {
|
|
|
protoc --dart_out=${output} --proto_path=${root} ${path}
|
|
|
}
|
|
|
.is_err()
|
|
|
{
|
|
|
- panic!("Run flutter protoc fail")
|
|
|
+ panic!("Generate pb file failed with: {}", path)
|
|
|
};
|
|
|
});
|
|
|
|
|
@@ -78,3 +84,31 @@ fn gen_pb_for_dart(name: &str, root: &str, paths: &Vec<String>, file_names: &Vec
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+fn check_pb_compiler() {
|
|
|
+ assert!(run_command("command -v protoc"), "protoc was not installed correctly");
|
|
|
+}
|
|
|
+
|
|
|
+fn check_pb_dart_plugin() {
|
|
|
+ assert!(
|
|
|
+ run_command("command -v protoc-gen-dart"),
|
|
|
+ "protoc-gen-dart was not installed correctly"
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+fn run_command(cmd: &str) -> bool {
|
|
|
+ let output = if cfg!(target_os = "windows") {
|
|
|
+ Command::new("cmd")
|
|
|
+ .arg("/C")
|
|
|
+ .arg(cmd)
|
|
|
+ .status()
|
|
|
+ .expect("failed to execute process")
|
|
|
+ } else {
|
|
|
+ Command::new("sh")
|
|
|
+ .arg("-c")
|
|
|
+ .arg(cmd)
|
|
|
+ .status()
|
|
|
+ .expect("failed to execute process")
|
|
|
+ };
|
|
|
+ output.success()
|
|
|
+}
|