1use crate::module::Module;
2use std::any::type_name;
3use std::fs;
4use std::fs::create_dir_all;
5use std::path::PathBuf;
6
7pub fn addon_create(path: &str, plugin: &str, plugin_title: &str, model: &str, model_title: &str, action: &str, action_title: &str) {
8 let root_path = PathBuf::from(path);
9 let plugin_path = root_path.join("plugin");
10 let plugin_path = plugin_path.join(plugin);
11 create_dir_all(plugin_path.as_os_str()).expect("创建目录失败");
12
13 let plugin_mod_path = plugin_path.join("mod.rs");
14 if !plugin_mod_path.is_file() {
15 let temp_plugin_mod_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
16 let temp_plugin_mod_path = temp_plugin_mod_path.join("temp").join("plugin");
17 let temp_plugin_mod_data = fs::read_to_string(temp_plugin_mod_path).unwrap();
18 let addon_1 = plugin.to_lowercase();
19 let plugin_1 = plugin[0..=0].to_uppercase();
20 let plugin_2 = plugin[1..].to_lowercase();
21 let temp_plugin_mod_data = temp_plugin_mod_data.replace("{{addon}}", &addon_1.to_string());
22 let temp_plugin_mod_data = temp_plugin_mod_data.replace("{{plugin}}", &format!("{}{}", plugin_1, plugin_2));
23 let temp_plugin_mod_data = temp_plugin_mod_data.replace("{{title}}", plugin_title);
24 fs::write(plugin_mod_path, temp_plugin_mod_data).expect("写入插件mod错误");
25 }
26
27 if !model.is_empty() {
28 let model_path = plugin_path.join(model);
29 create_dir_all(model_path.as_os_str()).expect("创建模型目录失败");
30
31
32 let plugin_d = {
33 let t: Vec<String> = plugin.split("_").map(|x| {
34 let x1 = x[0..=0].to_uppercase();
35 let x2 = x[1..].to_lowercase();
36 format!("{}{}", x1.clone(), x2.clone())
37 }).collect();
38 t.join("")
39 };
40 let model_d = {
41 let t: Vec<String> = model.split("_").map(|x| {
42 let x1 = x[0..=0].to_uppercase();
43 let x2 = x[1..].to_lowercase();
44 format!("{}{}", x1.clone(), x2.clone())
45 }).collect();
46 t.join("")
47 };
48
49 let model_mod_path = model_path.join("mod.rs");
50 if !model_mod_path.is_file() {
51 let temp_plugin_mod_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
52 let temp_model_mod_path = temp_plugin_mod_path.join("temp").join("model");
53 let mut temp_model_mod_data = fs::read_to_string(temp_model_mod_path).unwrap();
54 temp_model_mod_data = temp_model_mod_data.replace("{{addon}}", &plugin_d.to_string());
55 temp_model_mod_data = temp_model_mod_data.replace("{{model}}", &format!("{}{}", plugin_d, model_d));
56 temp_model_mod_data = temp_model_mod_data.replace("{{plugin_model}}", &format!("{}_{}", plugin, model));
57 temp_model_mod_data = temp_model_mod_data.replace("{{model_name}}", model);
58 let temp_model_mod_data = temp_model_mod_data.replace("{{title}}", model_title);
59 fs::write(model_mod_path, temp_model_mod_data).expect("写入模型mod错误");
60 }
61
62 if !action.is_empty() {
63 let action_path = model_path.join(format!("{}.rs", action));
64 if !action_path.is_file() {
65 let temp_action_mod_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
66 let temp_action_mod_path = {
67 if action.contains("table") {
68 temp_action_mod_path.join("temp").join("action_table")
69 } else if action.contains("add") {
70 temp_action_mod_path.join("temp").join("action_add")
71 } else if action.contains("del") {
72 temp_action_mod_path.join("temp").join("action_del")
73 } else if action.contains("put") {
74 temp_action_mod_path.join("temp").join("action_put")
75 } else if action.contains("select") {
76 temp_action_mod_path.join("temp").join("action_select")
77 } else if action.contains("get") {
78 temp_action_mod_path.join("temp").join("action_get")
79 } else if action.contains("menu") {
80 temp_action_mod_path.join("temp").join("action_menu")
81 } else if action.contains("down") {
82 temp_action_mod_path.join("temp").join("action_down")
83 } else if action.contains("config") {
84 temp_action_mod_path.join("temp").join("action_config")
85 } else if action.contains("tree") {
86 temp_action_mod_path.join("temp").join("action_tree")
87 } else if action.contains("import") {
88 temp_action_mod_path.join("temp").join("action_import")
89 } else {
90 temp_action_mod_path.join("temp").join("action")
91 }
92 };
93
94 let temp_action_mod_data = fs::read_to_string(temp_action_mod_path).unwrap();
95
96
97 let action_d = {
98 let t: Vec<String> = action.split("_").map(|x| {
99 let x1 = x[0..=0].to_uppercase();
100 let x2 = x[1..].to_lowercase();
101 format!("{}{}", x1.clone(), x2.clone())
102 }).collect();
103 t.join("")
104 };
105
106 let temp_action_mod_data = temp_action_mod_data.replace("{{action}}", &format!("{}{}{}", plugin_d, model_d, action_d));
107 let temp_action_mod_data = temp_action_mod_data.replace("{{api}}", &format!("{}.{}.{}", plugin, model, action));
108 let temp_action_mod_data = temp_action_mod_data.replace("{{model}}", &format!("{}{}", plugin_d, model_d));
109 let temp_action_mod_data = temp_action_mod_data.replace("{{model_a}}", model);
110 let temp_action_mod_data = temp_action_mod_data.replace("{{plugin}}", plugin);
111 let temp_action_mod_data = temp_action_mod_data.replace("{{title}}", action_title);
112 fs::write(action_path, temp_action_mod_data).expect("写入动作文件错误");
113 }
114 }
115 }
116}
117
118pub trait Addon {
120 fn _name(&self) -> String {
122 type_name::<Self>()
123 .rsplit("::")
124 .next()
125 .unwrap()
126 .to_lowercase()
127 }
128 fn title(&self) -> &'static str;
130 fn icon(&self) -> &'static str {
132 ""
133 }
134 fn description(&self) -> &'static str {
136 ""
137 }
138 fn pages(&self)-> &'static str {
140 ""
141 }
142 fn sort(&self) -> usize {
143 99
144 }
145 fn module(&mut self, name: &str) -> Result<Box<dyn Module>, String>;
147}