use crate::module::Module;
use std::any::type_name;
use std::fs;
use std::fs::create_dir_all;
use std::path::PathBuf;
pub fn addon_create(path: &str, plugin: &str, plugin_title: &str, model: &str, model_title: &str, action: &str, action_title: &str) {
let root_path = PathBuf::from(path);
let plugin_path = root_path.join("plugin");
let plugin_path = plugin_path.join(plugin);
create_dir_all(plugin_path.as_os_str()).expect("创建目录失败");
let plugin_mod_path = plugin_path.join("mod.rs");
if !plugin_mod_path.is_file() {
let temp_plugin_mod_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let temp_plugin_mod_path = temp_plugin_mod_path.join("temp").join("plugin");
let temp_plugin_mod_data = fs::read_to_string(temp_plugin_mod_path).unwrap();
let addon_1 = plugin.to_lowercase();
let plugin_1 = plugin[0..=0].to_uppercase();
let plugin_2 = plugin[1..].to_lowercase();
let temp_plugin_mod_data = temp_plugin_mod_data.replace("{{addon}}", &addon_1.to_string());
let temp_plugin_mod_data = temp_plugin_mod_data.replace("{{plugin}}", &format!("{}{}", plugin_1, plugin_2));
let temp_plugin_mod_data = temp_plugin_mod_data.replace("{{title}}", plugin_title);
fs::write(plugin_mod_path, temp_plugin_mod_data).expect("写入插件mod错误");
}
if !model.is_empty() {
let model_path = plugin_path.join(model);
create_dir_all(model_path.as_os_str()).expect("创建模型目录失败");
let plugin_d = {
let t: Vec<String> = plugin.split("_").map(|x| {
let x1 = x[0..=0].to_uppercase();
let x2 = x[1..].to_lowercase();
format!("{}{}", x1.clone(), x2.clone())
}).collect();
t.join("")
};
let model_d = {
let t: Vec<String> = model.split("_").map(|x| {
let x1 = x[0..=0].to_uppercase();
let x2 = x[1..].to_lowercase();
format!("{}{}", x1.clone(), x2.clone())
}).collect();
t.join("")
};
let model_mod_path = model_path.join("mod.rs");
if !model_mod_path.is_file() {
let temp_plugin_mod_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let temp_model_mod_path = temp_plugin_mod_path.join("temp").join("model");
let mut temp_model_mod_data = fs::read_to_string(temp_model_mod_path).unwrap();
temp_model_mod_data = temp_model_mod_data.replace("{{addon}}", &plugin_d.to_string());
temp_model_mod_data = temp_model_mod_data.replace("{{model}}", &format!("{}{}", plugin_d, model_d));
temp_model_mod_data = temp_model_mod_data.replace("{{plugin_model}}", &format!("{}_{}", plugin, model));
temp_model_mod_data = temp_model_mod_data.replace("{{model_name}}", model);
let temp_model_mod_data = temp_model_mod_data.replace("{{title}}", model_title);
fs::write(model_mod_path, temp_model_mod_data).expect("写入模型mod错误");
}
if !action.is_empty() {
let action_path = model_path.join(format!("{}.rs", action));
if !action_path.is_file() {
let temp_action_mod_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let temp_action_mod_path = {
if action.contains("table") {
temp_action_mod_path.join("temp").join("action_table")
} else if action.contains("add") {
temp_action_mod_path.join("temp").join("action_add")
} else if action.contains("del") {
temp_action_mod_path.join("temp").join("action_del")
} else if action.contains("put") {
temp_action_mod_path.join("temp").join("action_put")
} else if action.contains("select") {
temp_action_mod_path.join("temp").join("action_select")
} else if action.contains("get") {
temp_action_mod_path.join("temp").join("action_get")
} else if action.contains("menu") {
temp_action_mod_path.join("temp").join("action_menu")
} else if action.contains("down") {
temp_action_mod_path.join("temp").join("action_down")
} else if action.contains("config") {
temp_action_mod_path.join("temp").join("action_config")
} else if action.contains("tree") {
temp_action_mod_path.join("temp").join("action_tree")
} else if action.contains("import") {
temp_action_mod_path.join("temp").join("action_import")
} else {
temp_action_mod_path.join("temp").join("action")
}
};
let temp_action_mod_data = fs::read_to_string(temp_action_mod_path).unwrap();
let action_d = {
let t: Vec<String> = action.split("_").map(|x| {
let x1 = x[0..=0].to_uppercase();
let x2 = x[1..].to_lowercase();
format!("{}{}", x1.clone(), x2.clone())
}).collect();
t.join("")
};
let temp_action_mod_data = temp_action_mod_data.replace("{{action}}", &format!("{}{}{}", plugin_d, model_d, action_d));
let temp_action_mod_data = temp_action_mod_data.replace("{{api}}", &format!("{}.{}.{}", plugin, model, action));
let temp_action_mod_data = temp_action_mod_data.replace("{{model}}", &format!("{}{}", plugin_d, model_d));
let temp_action_mod_data = temp_action_mod_data.replace("{{model_a}}", model);
let temp_action_mod_data = temp_action_mod_data.replace("{{plugin}}", plugin);
let temp_action_mod_data = temp_action_mod_data.replace("{{title}}", action_title);
fs::write(action_path, temp_action_mod_data).expect("写入动作文件错误");
}
}
}
}
pub trait Addon {
fn _name(&self) -> String {
type_name::<Self>()
.rsplit("::")
.next()
.unwrap()
.to_lowercase()
}
fn title(&self) -> &'static str;
fn icon(&self) -> &'static str {
""
}
fn description(&self) -> &'static str {
""
}
fn sort(&self) -> usize {
99
}
fn module(&mut self, name: &str) -> Result<Box<dyn Module>, String>;
}