Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 56 additions & 6 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::plugins::PluginConfig;
// 全局配置管理器
use crate::plugin::PluginManagerState;
use crate::plugins::PluginConfig;
use log::{info, warn};
use serde::{Deserialize, Serialize};
use std::fs;
Expand Down Expand Up @@ -72,11 +72,8 @@ impl ConfigManager {
Ok(mut config) => {
println!("读取配置 -> 成功加载配置文件: {:?}", config_path);

// 检查 plugins 是否为 null,如果是则加载默认配置
if config.plugins.is_none() {
println!("读取配置 -> plugins 为 null,加载默认插件配置");
config.plugins = Self::get_default_plugins_config(app_handle);
}
// 合并插件配置(现有配置 + 默认配置中缺失的插件)
config.plugins = Self::merge_plugins_config(config.plugins, app_handle);

Ok(config)
}
Expand All @@ -96,6 +93,59 @@ impl ConfigManager {
}
}

// 合并插件配置(现有配置 + 默认配置中缺失的插件)
fn merge_plugins_config(
existing_plugins: Option<Vec<PluginConfig>>,
app_handle: Option<&AppHandle>,
) -> Option<Vec<PluginConfig>> {
// 获取所有默认插件配置
let default_plugins = Self::get_default_plugins_config(app_handle).unwrap_or_default();

if let Some(existing) = existing_plugins {
let mut merged_plugins = Vec::new();

// 遍历所有默认插件
for default_plugin in &default_plugins {
// 检查现有配置中是否已存在该插件
if let Some(existing_plugin) = existing
.iter()
.find(|p| p.language == default_plugin.language)
{
// 如果存在,使用现有配置
merged_plugins.push(existing_plugin.clone());
println!("读取配置 -> 使用现有插件配置: {}", existing_plugin.language);
} else {
// 如果不存在,使用默认配置
merged_plugins.push(default_plugin.clone());
println!(
"读取配置 -> 添加缺失的默认插件配置: {}",
default_plugin.language
);
}
}

// 添加现有配置中有但默认配置中没有的插件(用户自定义的插件)
for existing_plugin in existing {
if !default_plugins
.iter()
.any(|p| p.language == existing_plugin.language)
{
merged_plugins.push(existing_plugin.clone());
println!(
"读取配置 -> 保留用户自定义插件配置: {}",
existing_plugin.language
);
}
}

Some(merged_plugins)
} else {
// 如果现有配置中没有 plugins,直接使用默认配置
println!("读取配置 -> plugins 为 null,使用默认插件配置");
Some(default_plugins)
}
}

fn get_default_plugins_config(app_handle: Option<&AppHandle>) -> Option<Vec<PluginConfig>> {
if let Some(handle) = app_handle {
// 从 Tauri 状态中获取 PluginManager
Expand Down
4 changes: 1 addition & 3 deletions src-tauri/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ pub trait LanguagePlugin: Send + Sync {
fn get_language_key(&self) -> &'static str;

// 获取插件支持的文件扩展名
fn get_file_extension(&self) -> String {
self.get_config().unwrap().extension.clone()
}
fn get_file_extension(&self) -> String;

// 获取执行目录
fn get_execute_home(&self) -> Option<PathBuf> {
Expand Down
10 changes: 9 additions & 1 deletion src-tauri/src/plugins/python2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ impl LanguagePlugin for Python2Plugin {
"python2"
}

fn get_file_extension(&self) -> String {
self.get_config()
.map(|config| config.extension.clone())
.unwrap_or_else(|| "py".to_string())
}

fn get_version_args(&self) -> Vec<&'static str> {
vec!["--version"]
}
Expand All @@ -39,6 +45,8 @@ impl LanguagePlugin for Python2Plugin {
}

fn get_default_command(&self) -> String {
self.get_config().unwrap().run_command.unwrap()
self.get_config()
.and_then(|config| config.run_command)
.unwrap_or_else(|| "python2".to_string())
}
}
10 changes: 9 additions & 1 deletion src-tauri/src/plugins/python3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ impl LanguagePlugin for Python3Plugin {
"python3"
}

fn get_file_extension(&self) -> String {
self.get_config()
.map(|config| config.extension.clone())
.unwrap_or_else(|| "py".to_string())
}

fn get_version_args(&self) -> Vec<&'static str> {
vec!["--version"]
}
Expand All @@ -38,6 +44,8 @@ impl LanguagePlugin for Python3Plugin {
}

fn get_default_command(&self) -> String {
self.get_config().unwrap().run_command.unwrap()
self.get_config()
.and_then(|config| config.run_command)
.unwrap_or_else(|| "python3".to_string())
}
}
4 changes: 1 addition & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,7 @@ const handleLanguageChange = async (newLanguage: string) => {

// 更新代码模板
code.value = codeTemplates[newLanguage] || `# ${ getLanguageDisplayName(newLanguage) } Code
# Write your code here...

print("Hello from ${ getLanguageDisplayName(newLanguage) }!")`
# Write your code here...`

// 清空输出
clearOutput()
Expand Down
Loading