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("{{addon::model}}", &format!("{plugin}::{model}"));
98 temp_model_mod_data = temp_model_mod_data.replace("{{plugin_model}}", &format!("{plugin}_{model}"));
99 temp_model_mod_data = temp_model_mod_data.replace("{{model_name}}", model);
100 let temp_model_mod_data = temp_model_mod_data.replace("{{title}}", model_title);
101 fs::write(model_mod_path, temp_model_mod_data).expect("写入模型mod错误");
102 }
103
104 if !action.is_empty() {
105 let action_path = model_path.join(format!("{action}.rs"));
106 if !action_path.is_file() {
107 let temp_action_mod_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
108 let temp_action_mod_path = {
109 if action.contains("table") {
110 temp_action_mod_path.join("temp").join("action_table")
111 } else if action.contains("add") {
112 temp_action_mod_path.join("temp").join("action_add")
113 } else if action.contains("del") {
114 temp_action_mod_path.join("temp").join("action_del")
115 } else if action.contains("put") {
116 temp_action_mod_path.join("temp").join("action_put")
117 } else if action.contains("select_tree") {
118 temp_action_mod_path.join("temp").join("action_select_tree")
119 } else if action.contains("select") {
120 temp_action_mod_path.join("temp").join("action_select")
121 } else if action.contains("get") {
122 temp_action_mod_path.join("temp").join("action_get")
123 } else if action.contains("menu") {
124 temp_action_mod_path.join("temp").join("action_menu")
125 } else if action.contains("down") {
126 temp_action_mod_path.join("temp").join("action_down")
127 } else if action.contains("tree") {
128 temp_action_mod_path.join("temp").join("action_tree")
129 } else if action.contains("import") {
130 temp_action_mod_path.join("temp").join("action_import")
131 } else {
132 temp_action_mod_path.join("temp").join("action")
133 }
134 };
135
136 let temp_action_mod_data = fs::read_to_string(temp_action_mod_path).unwrap();
137
138
139 let action_d = {
140 let t: Vec<String> = action.split('_').map(|x| {
141 let x1 = x[0..=0].to_uppercase();
142 let x2 = x[1..].to_lowercase();
143 format!("{}{}", x1.clone(), x2.clone())
144 }).collect();
145 t.join("")
146 };
147
148 let temp_action_mod_data = temp_action_mod_data.replace("{{action}}", &format!("{plugin_d}{model_d}{action_d}"));
149 let temp_action_mod_data = temp_action_mod_data.replace("{{api}}", &format!("{plugin}.{model}.{action}"));
150 let temp_action_mod_data = temp_action_mod_data.replace("{{model}}", &format!("{plugin_d}{model_d}"));
151 let temp_action_mod_data = temp_action_mod_data.replace("{{addon::model}}", &format!("{plugin}::{model}"));
152 let temp_action_mod_data = temp_action_mod_data.replace("{{model_a}}", model);
153 let temp_action_mod_data = temp_action_mod_data.replace("{{plugin}}", plugin);
154 let temp_action_mod_data = temp_action_mod_data.replace("{{title}}", action_title);
155 fs::write(action_path, temp_action_mod_data).expect("写入动作文件错误");
156 }
157 }
158 }
159}