br_plugin/
addon.rs

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