Skip to content

Commit ae83887

Browse files
authored
Merge pull request #12 from qianmoQ/dev-25.0.0
feat (core): 适配插件定制配置
2 parents 6c90aee + 9ff82a9 commit ae83887

12 files changed

Lines changed: 635 additions & 183 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
"@tauri-apps/plugin-opener": "^2",
1616
"@tauri-apps/plugin-shell": "^2.3.0",
1717
"@vueuse/core": "^13.6.0",
18+
"lodash-es": "^4.17.21",
1819
"lucide-vue-next": "^0.539.0",
1920
"vue": "^3.5.13"
2021
},
2122
"devDependencies": {
2223
"@tailwindcss/postcss": "^4.1.11",
2324
"@tauri-apps/cli": "^2",
25+
"@types/lodash-es": "^4.17.12",
2426
"@vitejs/plugin-vue": "^5.2.1",
2527
"autoprefixer": "^10.4.21",
2628
"postcss": "^8.5.6",

src-tauri/src/config.rs

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1+
use crate::plugins::PluginConfig;
2+
// 全局配置管理器
3+
use crate::PluginManagerState;
14
use log::{info, warn};
25
use serde::{Deserialize, Serialize};
36
use std::fs;
47
use std::path::PathBuf;
8+
use std::sync::Mutex;
9+
use tauri::{AppHandle, Manager, command};
510

6-
#[derive(Debug, Clone, Serialize, Deserialize)]
7-
pub struct PluginConfig {
8-
pub enabled: bool, // 插件是否启用
9-
pub execute_home: String, // 插件的执行路径
10-
pub extensions: Vec<String>, // 插件支持的文件扩展名
11-
pub language: String, // 插件所属语言
12-
pub before_compile: String, // 插件在编译前执行的命令
13-
pub after_compile: String, // 插件在编译完成后执行的命令
14-
pub run_command: String, // 插件执行的命令
15-
pub template: String, // 插件的模板
16-
}
11+
static CONFIG_MANAGER: Mutex<Option<ConfigManager>> = Mutex::new(None);
1712

1813
#[derive(Debug, Clone, Serialize, Deserialize)]
1914
pub struct AppConfig {
@@ -42,9 +37,9 @@ pub struct ConfigManager {
4237
}
4338

4439
impl ConfigManager {
45-
pub fn new() -> Result<Self, String> {
40+
pub fn new(app_handle: Option<&AppHandle>) -> Result<Self, String> {
4641
let config_path = Self::get_config_path()?;
47-
let config = Self::load_config(&config_path)?;
42+
let config = Self::load_config(&config_path, app_handle)?;
4843

4944
Ok(Self {
5045
config_path,
@@ -66,27 +61,65 @@ impl ConfigManager {
6661
Ok(config_file)
6762
}
6863

69-
fn load_config(config_path: &PathBuf) -> Result<AppConfig, String> {
64+
fn load_config(
65+
config_path: &PathBuf,
66+
app_handle: Option<&AppHandle>,
67+
) -> Result<AppConfig, String> {
68+
println!("读取配置 -> 正在读取配置文件 {:?}", config_path);
7069
if config_path.exists() {
7170
match fs::read_to_string(config_path) {
7271
Ok(content) => match serde_json::from_str::<AppConfig>(&content) {
73-
Ok(config) => {
74-
info!("读取配置 -> 成功加载配置文件: {:?}", config_path);
72+
Ok(mut config) => {
73+
println!("读取配置 -> 成功加载配置文件: {:?}", config_path);
74+
75+
// 检查 plugins 是否为 null,如果是则加载默认配置
76+
if config.plugins.is_none() {
77+
println!("读取配置 -> plugins 为 null,加载默认插件配置");
78+
config.plugins = Self::get_default_plugins_config(app_handle);
79+
}
80+
7581
Ok(config)
7682
}
7783
Err(e) => {
7884
warn!("读取配置 -> 配置文件格式错误,使用默认配置: {}", e);
79-
Ok(AppConfig::default())
85+
Ok(Self::create_default_config(app_handle))
8086
}
8187
},
8288
Err(e) => {
8389
warn!("读取配置 -> 读取配置文件失败,使用默认配置: {}", e);
84-
Ok(AppConfig::default())
90+
Ok(Self::create_default_config(app_handle))
8591
}
8692
}
8793
} else {
88-
info!("读取配置 -> 配置文件不存在,使用默认配置");
89-
Ok(AppConfig::default())
94+
println!("读取配置 -> 配置文件不存在,使用默认配置");
95+
Ok(Self::create_default_config(app_handle))
96+
}
97+
}
98+
99+
fn get_default_plugins_config(app_handle: Option<&AppHandle>) -> Option<Vec<PluginConfig>> {
100+
if let Some(handle) = app_handle {
101+
// 从 Tauri 状态中获取 PluginManager
102+
if let Some(plugin_manager_state) = handle.try_state::<PluginManagerState>() {
103+
// 同步访问插件管理器
104+
if let Ok(manager) = plugin_manager_state.try_lock() {
105+
return Some(manager.get_all_plugin_default_config());
106+
} else {
107+
println!("读取配置 -> 无法获取插件管理器锁,使用空配置");
108+
}
109+
} else {
110+
println!("读取配置 -> 无法获取插件管理器状态,使用空配置");
111+
}
112+
}
113+
Some(vec![])
114+
}
115+
116+
fn create_default_config(app_handle: Option<&AppHandle>) -> AppConfig {
117+
AppConfig {
118+
log_directory: None,
119+
auto_clear_logs: Some(true),
120+
keep_log_days: Some(30),
121+
theme: Some("system".to_string()),
122+
plugins: Self::get_default_plugins_config(app_handle),
90123
}
91124
}
92125

@@ -96,7 +129,7 @@ impl ConfigManager {
96129

97130
fs::write(&self.config_path, content).map_err(|e| format!("写入配置文件失败: {}", e))?;
98131

99-
info!("保存配置 -> 配置文件已保存: {:?}", self.config_path);
132+
info!("保存配置 -> 配置文件已保存 {}", self.config_path.display());
100133
Ok(())
101134
}
102135

@@ -114,20 +147,16 @@ impl ConfigManager {
114147
}
115148
}
116149

117-
// 全局配置管理器
118-
use std::sync::Mutex;
119-
static CONFIG_MANAGER: Mutex<Option<ConfigManager>> = Mutex::new(None);
120-
121150
// 初始化配置
122-
pub fn init_config() -> Result<(), String> {
123-
let config_manager = ConfigManager::new()?;
151+
pub fn init_config(app_handle: Option<&AppHandle>) -> Result<(), String> {
152+
let config_manager = ConfigManager::new(app_handle)?;
124153

125154
// 如果配置中有自定义日志目录,设置到日志系统
126155
if let Some(log_dir) = config_manager.get_log_directory() {
127-
info!("初始化 -> 从配置文件加载日志目录: {}", log_dir);
156+
println!("读取配置 -> 从配置文件加载日志目录: {}", log_dir);
128157
// 使用内部函数设置,避免循环保存
129158
if let Err(e) = crate::logger::set_log_directory_internal(log_dir.to_string()) {
130-
warn!("初始化 -> 应用配置中的日志目录失败: {}", e);
159+
warn!("读取配置 -> 应用配置中的日志目录失败: {}", e);
131160
}
132161
}
133162

@@ -144,9 +173,6 @@ pub fn get_config_manager() -> Result<std::sync::MutexGuard<'static, Option<Conf
144173
.map_err(|e| format!("获取配置管理器失败: {}", e))
145174
}
146175

147-
// Tauri 命令
148-
use tauri::command;
149-
150176
#[command]
151177
pub async fn get_app_config() -> Result<AppConfig, String> {
152178
let guard = get_config_manager()?;
@@ -157,6 +183,15 @@ pub async fn get_app_config() -> Result<AppConfig, String> {
157183
}
158184
}
159185

186+
pub fn get_app_config_internal() -> Result<AppConfig, String> {
187+
let guard = get_config_manager()?;
188+
if let Some(config_manager) = guard.as_ref() {
189+
Ok(config_manager.get_config().clone())
190+
} else {
191+
Err("配置管理器未初始化".to_string())
192+
}
193+
}
194+
160195
#[command]
161196
pub async fn update_app_config(config: AppConfig) -> Result<(), String> {
162197
let mut guard = get_config_manager()?;

0 commit comments

Comments
 (0)