|
| 1 | +use crate::config::{get_app_config_internal, get_config_manager}; |
| 2 | +use crate::execution::PluginManagerState; |
| 3 | +use crate::plugins::PluginConfig; |
| 4 | +use crate::plugins::custom::CustomPlugin; |
| 5 | +use log::info; |
| 6 | +use std::fs; |
| 7 | +use tauri::{AppHandle, Emitter, State, command}; |
| 8 | + |
| 9 | +#[command] |
| 10 | +pub async fn add_custom_plugin( |
| 11 | + config: PluginConfig, |
| 12 | + app_handle: AppHandle, |
| 13 | + plugin_manager: State<'_, PluginManagerState>, |
| 14 | +) -> Result<(), String> { |
| 15 | + { |
| 16 | + let mut guard = get_config_manager()?; |
| 17 | + if let Some(config_manager) = guard.as_mut() { |
| 18 | + let mut app_config = config_manager.get_config().clone(); |
| 19 | + |
| 20 | + let mut custom_plugins = app_config.custom_plugins.unwrap_or_default(); |
| 21 | + |
| 22 | + if custom_plugins.iter().any(|p| p.language == config.language) { |
| 23 | + return Err(format!("自定义插件 {} 已存在", config.language)); |
| 24 | + } |
| 25 | + |
| 26 | + custom_plugins.push(config.clone()); |
| 27 | + app_config.custom_plugins = Some(custom_plugins); |
| 28 | + |
| 29 | + config_manager.update_config(app_config)?; |
| 30 | + } else { |
| 31 | + return Err("配置管理器未初始化".to_string()); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + let mut manager = plugin_manager.lock().await; |
| 36 | + let custom_plugin = CustomPlugin::new(config.clone()); |
| 37 | + manager.register_plugin(config.language.clone(), Box::new(custom_plugin)); |
| 38 | + |
| 39 | + app_handle.emit("config-updated", ()).ok(); |
| 40 | + info!("自定义插件 {} 已添加", config.language); |
| 41 | + |
| 42 | + Ok(()) |
| 43 | +} |
| 44 | + |
| 45 | +#[command] |
| 46 | +pub async fn update_custom_plugin( |
| 47 | + config: PluginConfig, |
| 48 | + app_handle: AppHandle, |
| 49 | + plugin_manager: State<'_, PluginManagerState>, |
| 50 | +) -> Result<(), String> { |
| 51 | + { |
| 52 | + let mut guard = get_config_manager()?; |
| 53 | + if let Some(config_manager) = guard.as_mut() { |
| 54 | + let mut app_config = config_manager.get_config().clone(); |
| 55 | + |
| 56 | + let mut custom_plugins = app_config.custom_plugins.unwrap_or_default(); |
| 57 | + |
| 58 | + if let Some(index) = custom_plugins |
| 59 | + .iter() |
| 60 | + .position(|p| p.language == config.language) |
| 61 | + { |
| 62 | + custom_plugins[index] = config.clone(); |
| 63 | + } else { |
| 64 | + return Err(format!("自定义插件 {} 不存在", config.language)); |
| 65 | + } |
| 66 | + |
| 67 | + app_config.custom_plugins = Some(custom_plugins); |
| 68 | + |
| 69 | + config_manager.update_config(app_config)?; |
| 70 | + } else { |
| 71 | + return Err("配置管理器未初始化".to_string()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + let mut manager = plugin_manager.lock().await; |
| 76 | + manager.unregister_plugin(&config.language); |
| 77 | + let custom_plugin = CustomPlugin::new(config.clone()); |
| 78 | + manager.register_plugin(config.language.clone(), Box::new(custom_plugin)); |
| 79 | + |
| 80 | + app_handle.emit("config-updated", ()).ok(); |
| 81 | + info!("自定义插件 {} 已更新", config.language); |
| 82 | + |
| 83 | + Ok(()) |
| 84 | +} |
| 85 | + |
| 86 | +#[command] |
| 87 | +pub async fn remove_custom_plugin( |
| 88 | + language: String, |
| 89 | + app_handle: AppHandle, |
| 90 | + plugin_manager: State<'_, PluginManagerState>, |
| 91 | +) -> Result<(), String> { |
| 92 | + { |
| 93 | + let mut guard = get_config_manager()?; |
| 94 | + if let Some(config_manager) = guard.as_mut() { |
| 95 | + let mut app_config = config_manager.get_config().clone(); |
| 96 | + |
| 97 | + let mut custom_plugins = app_config.custom_plugins.unwrap_or_default(); |
| 98 | + |
| 99 | + custom_plugins.retain(|p| p.language != language); |
| 100 | + |
| 101 | + app_config.custom_plugins = Some(custom_plugins); |
| 102 | + |
| 103 | + config_manager.update_config(app_config)?; |
| 104 | + } else { |
| 105 | + return Err("配置管理器未初始化".to_string()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + let mut manager = plugin_manager.lock().await; |
| 110 | + manager.unregister_plugin(&language); |
| 111 | + |
| 112 | + app_handle.emit("config-updated", ()).ok(); |
| 113 | + info!("自定义插件 {} 已移除", language); |
| 114 | + |
| 115 | + Ok(()) |
| 116 | +} |
| 117 | + |
| 118 | +#[command] |
| 119 | +pub async fn get_custom_plugins() -> Result<Vec<PluginConfig>, String> { |
| 120 | + let app_config = get_app_config_internal()?; |
| 121 | + Ok(app_config.custom_plugins.unwrap_or_default()) |
| 122 | +} |
| 123 | + |
| 124 | +#[command] |
| 125 | +pub async fn save_custom_icon( |
| 126 | + language: String, |
| 127 | + icon_data: Vec<u8>, |
| 128 | + file_extension: String, |
| 129 | +) -> Result<String, String> { |
| 130 | + let home_dir = dirs::home_dir().ok_or("无法获取用户主目录")?; |
| 131 | + let icons_dir = home_dir.join(".codeforge").join("custom-icons"); |
| 132 | + |
| 133 | + fs::create_dir_all(&icons_dir).map_err(|e| format!("创建图标目录失败: {}", e))?; |
| 134 | + |
| 135 | + let icon_path = icons_dir.join(format!("{}.{}", language, file_extension)); |
| 136 | + |
| 137 | + fs::write(&icon_path, icon_data).map_err(|e| format!("保存图标文件失败: {}", e))?; |
| 138 | + |
| 139 | + info!("自定义图标已保存: {:?}", icon_path); |
| 140 | + |
| 141 | + Ok(icon_path.to_string_lossy().to_string()) |
| 142 | +} |
0 commit comments