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}
44
45
46pub fn addon_create(path: &str, plugin: &str, plugin_title: &str, model: &str, model_title: &str, action: &str, action_title: &str) {
47 let root_path = PathBuf::from(path);
48 let plugin_path = root_path.join("plugin");
49 let plugin_path = plugin_path.join(plugin);
50 create_dir_all(plugin_path.as_os_str()).expect("创建目录失败");
51
52 let plugin_mod_path = plugin_path.join("mod.rs");
53 if !plugin_mod_path.is_file() {
54 let temp_plugin_mod_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
55 let temp_plugin_mod_path = temp_plugin_mod_path.join("temp").join("plugin");
56 let temp_plugin_mod_data = fs::read_to_string(temp_plugin_mod_path).unwrap();
57 let addon_1 = plugin.to_lowercase();
58 let plugin_1 = plugin[0..=0].to_uppercase();
59 let plugin_2 = plugin[1..].to_lowercase();
60 let temp_plugin_mod_data = temp_plugin_mod_data.replace("{{addon}}", &addon_1.to_string());
61 let temp_plugin_mod_data = temp_plugin_mod_data.replace("{{plugin}}", &format!("{plugin_1}{plugin_2}"));
62 let temp_plugin_mod_data = temp_plugin_mod_data.replace("{{title}}", plugin_title);
63 fs::write(plugin_mod_path, temp_plugin_mod_data).expect("写入插件mod错误");
64 }
65
66 if !model.is_empty() {
67 let model_path = plugin_path.join(model);
68 create_dir_all(model_path.as_os_str()).expect("创建模型目录失败");
69
70
71 let plugin_d = {
72 let t: Vec<String> = plugin.split('_').map(|x| {
73 let x1 = x[0..=0].to_uppercase();
74 let x2 = x[1..].to_lowercase();
75 format!("{}{}", x1.clone(), x2.clone())
76 }).collect();
77 t.join("")
78 };
79 let model_d = {
80 let t: Vec<String> = model.split('_').map(|x| {
81 let x1 = x[0..=0].to_uppercase();
82 let x2 = x[1..].to_lowercase();
83 format!("{}{}", x1.clone(), x2.clone())
84 }).collect();
85 t.join("")
86 };
87
88 let model_mod_path = model_path.join("mod.rs");
89 if !model_mod_path.is_file() {
90 let temp_plugin_mod_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
91 let temp_model_mod_path = temp_plugin_mod_path.join("temp").join("model");
92 let mut temp_model_mod_data = fs::read_to_string(temp_model_mod_path).unwrap();
93 temp_model_mod_data = temp_model_mod_data.replace("{{addon}}", &plugin_d.to_string());
94 temp_model_mod_data = temp_model_mod_data.replace("{{model}}", &format!("{plugin_d}{model_d}"));
95 temp_model_mod_data = temp_model_mod_data.replace("{{plugin_model}}", &format!("{plugin}_{model}"));
96 temp_model_mod_data = temp_model_mod_data.replace("{{model_name}}", model);
97 let temp_model_mod_data = temp_model_mod_data.replace("{{title}}", model_title);
98 fs::write(model_mod_path, temp_model_mod_data).expect("写入模型mod错误");
99 }
100
101 if !action.is_empty() {
102 let action_path = model_path.join(format!("{action}.rs"));
103 if !action_path.is_file() {
104 let temp_action_mod_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
105 let temp_action_mod_path = {
106 if action.contains("table") {
107 temp_action_mod_path.join("temp").join("action_table")
108 } else if action.contains("add") {
109 temp_action_mod_path.join("temp").join("action_add")
110 } else if action.contains("del") {
111 temp_action_mod_path.join("temp").join("action_del")
112 } else if action.contains("put") {
113 temp_action_mod_path.join("temp").join("action_put")
114 } else if action.contains("select") {
115 temp_action_mod_path.join("temp").join("action_select")
116 } else if action.contains("get") {
117 temp_action_mod_path.join("temp").join("action_get")
118 } else if action.contains("menu") {
119 temp_action_mod_path.join("temp").join("action_menu")
120 } else if action.contains("down") {
121 temp_action_mod_path.join("temp").join("action_down")
122 } else if action.contains("config") {
123 temp_action_mod_path.join("temp").join("action_config")
124 } else if action.contains("tree") {
125 temp_action_mod_path.join("temp").join("action_tree")
126 } else if action.contains("import") {
127 temp_action_mod_path.join("temp").join("action_import")
128 } else {
129 temp_action_mod_path.join("temp").join("action")
130 }
131 };
132
133 let temp_action_mod_data = fs::read_to_string(temp_action_mod_path).unwrap();
134
135
136 let action_d = {
137 let t: Vec<String> = action.split('_').map(|x| {
138 let x1 = x[0..=0].to_uppercase();
139 let x2 = x[1..].to_lowercase();
140 format!("{}{}", x1.clone(), x2.clone())
141 }).collect();
142 t.join("")
143 };
144
145 let temp_action_mod_data = temp_action_mod_data.replace("{{action}}", &format!("{plugin_d}{model_d}{action_d}"));
146 let temp_action_mod_data = temp_action_mod_data.replace("{{api}}", &format!("{plugin}.{model}.{action}"));
147 let temp_action_mod_data = temp_action_mod_data.replace("{{model}}", &format!("{plugin_d}{model_d}"));
148 let temp_action_mod_data = temp_action_mod_data.replace("{{model_a}}", model);
149 let temp_action_mod_data = temp_action_mod_data.replace("{{plugin}}", plugin);
150 let temp_action_mod_data = temp_action_mod_data.replace("{{title}}", action_title);
151 fs::write(action_path, temp_action_mod_data).expect("写入动作文件错误");
152 }
153 }
154 }
155}