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