Merge pull request #314 from AppFlowy-IO/fix_dart_pb

ci: fix dart pb
This commit is contained in:
Nathan.fooo 2022-02-09 08:02:58 +08:00 committed by GitHub
commit 32e0b5e96d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 19 deletions

View File

@ -32,8 +32,8 @@ jobs:
run: | run: |
cargo install --force cargo-make cargo install --force cargo-make
cargo install --force duckscript_cli cargo install --force duckscript_cli
brew install protobuf
cargo make flowy_dev cargo make flowy_dev
echo PATH="$PATH":"$HOME/.pub-cache/bin" >> $GITHUB_PATH
- name: Build - name: Build
working-directory: frontend working-directory: frontend
run: cargo make --profile production-mac-x86 appflowy run: cargo make --profile production-mac-x86 appflowy
@ -74,8 +74,8 @@ jobs:
run: | run: |
cargo install --force cargo-make cargo install --force cargo-make
cargo install --force duckscript_cli cargo install --force duckscript_cli
sudo apt-get install protobuf-compiler
cargo make flowy_dev cargo make flowy_dev
echo PATH="$PATH":"$HOME/.pub-cache/bin" >> $GITHUB_PATH
- name: Build - name: Build
working-directory: frontend working-directory: frontend
run: cargo make --profile production-linux-x86 appflowy run: cargo make --profile production-linux-x86 appflowy

View File

@ -5,7 +5,7 @@
# cargo make --profile production-windows-x86 flowy-sdk-dev # cargo make --profile production-windows-x86 flowy-sdk-dev
[tasks.env_check] [tasks.env_check]
dependencies = ["echo_env", "install-pb-tool-if-need"] dependencies = ["echo_env", "install_protobuf"]
condition = { env_set = [ "BUILD_FLAG", "RUST_COMPILE_TARGET", "CRATE_TYPE", "TARGET_OS"], channels = ["stable"] } condition = { env_set = [ "BUILD_FLAG", "RUST_COMPILE_TARGET", "CRATE_TYPE", "TARGET_OS"], channels = ["stable"] }
[tasks.flowy-sdk-dev] [tasks.flowy-sdk-dev]

View File

@ -2,16 +2,14 @@
[tasks.pb] [tasks.pb]
dependencies = ["check_protoc_cmd", "gen_pb_file"] dependencies = ["check_protoc_cmd", "gen_pb_file"]
[tasks.install-pb-tool-if-need] [tasks.install_protobuf]
condition_script = [ condition_script = [
""" """
if [ ! "$(command -v dart)" ]; then if [ ! "$(command -v protoc)" ]; then
echo Please make sure flutter/dart is properly installed and in PATH env var exit 0
exit 1
fi fi
if [ ! "$(command -v protoc-gen-dart)" ]; then if [ ! "$(command -v protoc-gen-dart)" ]; then
# not install
exit 0 exit 0
fi fi
@ -19,19 +17,27 @@ condition_script = [
exit 1 exit 1
""", """,
] ]
run_task = { name = ["install_protobuf"] } run_task = { name = ["install_protobuf_compiler"] }
[tasks.install_protobuf] [tasks.install_protobuf_compiler]
script = """ script = """
brew install protobuf
echo "Install protoc_plugin (Dart)" echo "Install protoc_plugin (Dart)"
dart pub global activate protoc_plugin dart pub global activate protoc_plugin
export PATH="$HOME/.pub-cache/bin:$PATH"
cargo install --version 2.22.1 protobuf-codegen
""" """
script_runner = "@shell" script_runner = "@shell"
[tasks.install_protobuf.windows] [tasks.install_protobuf_compiler.linux]
script = """
sudo apt-get install protobuf-compiler
echo "Install protoc_plugin (Dart)"
dart pub global activate protoc_plugin
"""
script_runner = "@shell"
[tasks.install_protobuf_compiler.windows]
script = """ script = """
ret = which dart ret = which dart
if is_empty ${ret} if is_empty ${ret}

View File

@ -1,10 +1,12 @@
#![allow(clippy::all)]
#![allow(unused_imports)] #![allow(unused_imports)]
#![allow(unused_attributes)] #![allow(unused_attributes)]
#![allow(dead_code)]
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::process::Command;
use walkdir::WalkDir; use walkdir::WalkDir;
#[allow(dead_code)]
pub fn gen(name: &str, root: &str) { pub fn gen(name: &str, root: &str) {
let mut paths = vec![]; let mut paths = vec![];
let mut file_names = vec![]; let mut file_names = vec![];
@ -20,9 +22,10 @@ pub fn gen(name: &str, root: &str) {
file_names.push(file_name); file_names.push(file_name);
} }
} }
println!("cargo:rerun-if-changed=build.rs");
// #[cfg(feature = "dart")] #[cfg(feature = "dart")]
// gen_pb_for_dart(name, root, &paths, &file_names); gen_pb_for_dart(name, root, &paths, &file_names);
protoc_rust::Codegen::new() protoc_rust::Codegen::new()
.out_dir("./src/protobuf/model") .out_dir("./src/protobuf/model")
@ -33,7 +36,6 @@ pub fn gen(name: &str, root: &str) {
} }
#[cfg(feature = "dart")] #[cfg(feature = "dart")]
#[allow(dead_code)]
fn gen_pb_for_dart(name: &str, root: &str, paths: &Vec<String>, file_names: &Vec<String>) { fn gen_pb_for_dart(name: &str, root: &str, paths: &Vec<String>, file_names: &Vec<String>) {
let output = format!( 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() { if !std::path::Path::new(&output).exists() {
std::fs::create_dir_all(&output).unwrap(); std::fs::create_dir_all(&output).unwrap();
} }
check_pb_compiler();
check_pb_dart_plugin();
paths.iter().for_each(|path| { paths.iter().for_each(|path| {
if cmd_lib::run_cmd! { if cmd_lib::run_cmd! {
protoc --dart_out=${output} --proto_path=${root} ${path} protoc --dart_out=${output} --proto_path=${root} ${path}
} }
.is_err() .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()
}