br_addon/
addon.rs

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