Skip to content

Commit 27267c6

Browse files
committed
fix (core): 修复 execute_home 为空导致进入空目录
1 parent 21c3c86 commit 27267c6

5 files changed

Lines changed: 37 additions & 22 deletions

File tree

src-tauri/src/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async fn execute_code(
6464
let start_time = std::time::Instant::now();
6565
let mut _last_error: String = String::new();
6666

67-
let cmd = plugin.get_command();
67+
let cmd = plugin.get_command(None);
6868
let args = plugin.get_execute_args(file_path.to_str().unwrap());
6969
info!(
7070
"执行代码 -> 调用插件 [ {} ] 执行命令 {} 携带参数 {}",
@@ -74,7 +74,7 @@ async fn execute_code(
7474
);
7575

7676
let output = Command::new(&cmd)
77-
.args(&args)
77+
.args(args)
7878
.stdout(Stdio::piped())
7979
.stderr(Stdio::piped())
8080
.output();
@@ -142,7 +142,9 @@ async fn execute_code(
142142
request.language,
143143
request.language,
144144
_last_error,
145-
plugin.get_command().to_string()
145+
plugin
146+
.get_command(Some(file_path.to_str().unwrap()))
147+
.to_string()
146148
),
147149
execution_time,
148150
timestamp,
@@ -172,7 +174,7 @@ async fn get_info(
172174
format!("Pre-execution hook failed: {}", e)
173175
})?;
174176

175-
let cmd = plugin.get_command();
177+
let cmd = plugin.get_command(None);
176178
debug!("获取环境 -> 插件 [ {} ] 命令 {}", language, cmd);
177179

178180
let version_output = Command::new(&cmd).args(plugin.get_version_args()).output();
@@ -211,7 +213,7 @@ async fn get_info(
211213
Ok(LanguageInfo {
212214
installed: false,
213215
version: "Not found".to_string(),
214-
path: format!("Not found - tried: {:?}", plugin.get_command()),
216+
path: format!("Not found - tried: {:?}", plugin.get_command(None)),
215217
language: plugin.get_language_name().to_string(),
216218
})
217219
}

src-tauri/src/plugins/manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl PluginManager {
5454
self.get_plugin(language).map(|plugin| PluginInfo {
5555
name: plugin.get_language_name().to_string(),
5656
file_extension: plugin.get_file_extension(),
57-
available_commands: vec![plugin.get_command().to_string()],
57+
available_commands: vec![plugin.get_command(None).to_string()],
5858
})
5959
}
6060

@@ -65,7 +65,7 @@ impl PluginManager {
6565
.map(|plugin| PluginInfo {
6666
name: plugin.get_language_name().to_string(),
6767
file_extension: plugin.get_file_extension(),
68-
available_commands: vec![plugin.get_command().to_string()],
68+
available_commands: vec![plugin.get_command(None).to_string()],
6969
})
7070
.collect()
7171
}

src-tauri/src/plugins/mod.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,23 @@ pub trait LanguagePlugin: Send + Sync {
6363
fn get_execute_home(&self) -> Option<PathBuf> {
6464
self.get_config()
6565
.and_then(|config| config.execute_home.clone())
66+
.filter(|path| !path.trim().is_empty()) // 过滤掉空字符串和只有空白字符的字符串
6667
.map(PathBuf::from)
6768
}
6869

6970
// 获取插件支持的命令
70-
fn get_command(&self) -> String {
71+
fn get_command(&self, file_path: Option<&str>) -> String {
7172
if let Some(config) = self.get_config() {
7273
if let Some(run_cmd) = &config.run_command {
73-
return run_cmd
74-
.split_whitespace()
75-
.next()
76-
.unwrap_or(&config.language)
77-
.to_string();
74+
if let Some(path) = file_path {
75+
return run_cmd.replace("$filename", path);
76+
} else {
77+
return run_cmd
78+
.split_whitespace()
79+
.next()
80+
.unwrap_or(&config.language)
81+
.to_string();
82+
}
7883
}
7984
}
8085
self.get_default_command()
@@ -120,7 +125,23 @@ pub trait LanguagePlugin: Send + Sync {
120125
}
121126

122127
fn get_version_args(&self) -> Vec<&'static str>;
123-
fn get_execute_args(&self, file_path: &str) -> Vec<String>;
128+
129+
fn get_execute_args(&self, file_path: &str) -> Vec<String> {
130+
if let Some(config) = self.get_config() {
131+
if let Some(run_cmd) = &config.run_command {
132+
// 替换 $filename 后分割,跳过第一个元素(命令本身)
133+
let full_cmd = run_cmd.replace("$filename", file_path);
134+
return full_cmd
135+
.split_whitespace()
136+
.skip(1) // 跳过命令部分,只返回参数
137+
.map(|s| s.to_string())
138+
.collect();
139+
}
140+
}
141+
// 默认情况下,文件路径就是唯一的参数
142+
vec![file_path.to_string()]
143+
}
144+
124145
fn get_path_command(&self) -> String;
125146

126147
// 构建默认配置

src-tauri/src/plugins/python2.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ impl LanguagePlugin for Python2Plugin {
2020
vec!["--version"]
2121
}
2222

23-
fn get_execute_args(&self, file_path: &str) -> Vec<String> {
24-
vec![file_path.to_string()]
25-
}
26-
2723
fn get_path_command(&self) -> String {
2824
"import sys; print(sys.executable)".to_string()
2925
}

src-tauri/src/plugins/python3.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ impl LanguagePlugin for Python3Plugin {
1919
vec!["--version"]
2020
}
2121

22-
fn get_execute_args(&self, file_path: &str) -> Vec<String> {
23-
vec![file_path.to_string()]
24-
}
25-
2622
fn get_path_command(&self) -> String {
2723
"import sys; print(sys.executable)".to_string()
2824
}

0 commit comments

Comments
 (0)