2022-02-17 12:10:29 +00:00
|
|
|
use crate::code_gen::util::path_buf_with_component;
|
2022-02-09 01:57:03 +00:00
|
|
|
use std::fs;
|
2022-02-17 12:10:29 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-02-09 01:57:03 +00:00
|
|
|
|
2022-06-15 11:40:18 +00:00
|
|
|
#[derive(serde::Deserialize, Clone, Debug)]
|
2022-02-09 01:57:03 +00:00
|
|
|
pub struct FlowyConfig {
|
|
|
|
pub proto_crates: Vec<String>,
|
|
|
|
pub event_files: Vec<String>,
|
2022-06-15 11:40:18 +00:00
|
|
|
pub proto_output_dir: String,
|
|
|
|
pub protobuf_crate_path: String,
|
2022-02-09 01:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FlowyConfig {
|
2022-02-17 12:10:29 +00:00
|
|
|
pub fn from_toml_file(path: &Path) -> Self {
|
2022-02-09 01:57:03 +00:00
|
|
|
let content = fs::read_to_string(path).unwrap();
|
|
|
|
let config: FlowyConfig = toml::from_str(content.as_ref()).unwrap();
|
|
|
|
config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CrateConfig {
|
2022-02-17 12:10:29 +00:00
|
|
|
pub crate_path: PathBuf,
|
2022-02-15 13:27:00 +00:00
|
|
|
pub folder_name: String,
|
|
|
|
pub flowy_config: FlowyConfig,
|
2022-02-09 01:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CrateConfig {
|
2022-02-17 12:10:29 +00:00
|
|
|
pub fn proto_paths(&self) -> Vec<PathBuf> {
|
2022-02-09 01:57:03 +00:00
|
|
|
let proto_paths = self
|
|
|
|
.flowy_config
|
|
|
|
.proto_crates
|
|
|
|
.iter()
|
2022-02-18 15:04:55 +00:00
|
|
|
.map(|name| path_buf_with_component(&self.crate_path, vec![name]))
|
2022-02-17 12:10:29 +00:00
|
|
|
.collect::<Vec<PathBuf>>();
|
2022-02-09 01:57:03 +00:00
|
|
|
proto_paths
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_crate_config_from(entry: &walkdir::DirEntry) -> Option<CrateConfig> {
|
2022-02-17 12:10:29 +00:00
|
|
|
let mut config_path = entry.path().parent().unwrap().to_path_buf();
|
|
|
|
config_path.push("Flowy.toml");
|
|
|
|
if !config_path.as_path().exists() {
|
2022-02-09 01:57:03 +00:00
|
|
|
return None;
|
|
|
|
}
|
2022-02-17 12:10:29 +00:00
|
|
|
let crate_path = entry.path().parent().unwrap().to_path_buf();
|
|
|
|
let flowy_config = FlowyConfig::from_toml_file(config_path.as_path());
|
|
|
|
let folder_name = crate_path.file_stem().unwrap().to_str().unwrap().to_string();
|
2022-02-09 01:57:03 +00:00
|
|
|
|
|
|
|
Some(CrateConfig {
|
|
|
|
crate_path,
|
|
|
|
folder_name,
|
|
|
|
flowy_config,
|
|
|
|
})
|
|
|
|
}
|