Skip to content

Commit 0ad08ec

Browse files
committed
feat (language): 支持 Java 语言
1 parent a36062c commit 0ad08ec

4 files changed

Lines changed: 74 additions & 1 deletion

File tree

src-tauri/src/plugin.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,32 @@ pub async fn get_info(
3232
debug!("获取环境 -> 插件 [ {} ] 命令 {}", language, cmd);
3333

3434
let version_output = Command::new(&cmd).args(plugin.get_version_args()).output();
35+
debug!(
36+
"获取环境 -> 插件 [ {} ] 版本, 结果 {:?}",
37+
plugin.get_language_key(),
38+
version_output
39+
);
3540
if let Ok(version_out) = version_output {
3641
if version_out.status.success() {
3742
let path_result = Command::new(&cmd)
3843
.arg("-c")
3944
.arg(plugin.get_path_command())
4045
.output();
4146

42-
let version = String::from_utf8_lossy(&version_out.stdout)
47+
let mut version = String::from_utf8_lossy(&version_out.stdout)
4348
.trim()
4449
.to_string();
4550

51+
if version.is_empty() {
52+
info!(
53+
"获取环境 -> 调用插件 [ {} ] 版本为空,通过 stderr 获取",
54+
language
55+
);
56+
version = String::from_utf8_lossy(&version_out.stderr)
57+
.trim()
58+
.to_string();
59+
}
60+
4661
let path = if let Ok(path_out) = path_result {
4762
if path_out.status.success() {
4863
String::from_utf8_lossy(&path_out.stdout).trim().to_string()

src-tauri/src/plugins/java.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use super::{LanguagePlugin, PluginConfig};
2+
use std::vec;
3+
4+
pub struct JavaPlugin;
5+
6+
impl LanguagePlugin for JavaPlugin {
7+
fn get_order(&self) -> i32 {
8+
2
9+
}
10+
11+
fn get_language_name(&self) -> &'static str {
12+
"Java"
13+
}
14+
15+
fn get_language_key(&self) -> &'static str {
16+
"java"
17+
}
18+
19+
fn get_file_extension(&self) -> String {
20+
self.get_config()
21+
.map(|config| config.extension.clone())
22+
.unwrap_or_else(|| "java".to_string())
23+
}
24+
25+
fn get_version_args(&self) -> Vec<&'static str> {
26+
vec!["-version"]
27+
}
28+
29+
fn get_path_command(&self) -> String {
30+
"System.out.println(System.getProperty(\"java.home\"));".to_string()
31+
}
32+
33+
fn get_default_config(&self) -> PluginConfig {
34+
PluginConfig {
35+
enabled: true,
36+
language: String::from("java"),
37+
before_compile: None,
38+
extension: String::from("java"),
39+
execute_home: None,
40+
run_command: Some(String::from("java $filename")),
41+
after_compile: Some(String::from("rm -f *.class")),
42+
template: Some(String::from(
43+
"public class Main {\n public static void main(String[] args) {\n System.out.println(\"Hello, World!\");\n }\n}",
44+
)),
45+
timeout: Some(30),
46+
}
47+
}
48+
49+
fn get_default_command(&self) -> String {
50+
self.get_config()
51+
.and_then(|config| config.run_command.clone())
52+
.unwrap_or_else(|| "java".to_string())
53+
}
54+
}

src-tauri/src/plugins/manager.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{
22
LanguagePlugin, PluginConfig, go::GoPlugin, nodejs::NodeJSPlugin, python2::Python2Plugin,
33
python3::Python3Plugin,
44
};
5+
use crate::plugins::java::JavaPlugin;
56
use std::collections::HashMap;
67

78
pub struct PluginManager {
@@ -16,6 +17,7 @@ impl PluginManager {
1617
plugins.insert("python3".to_string(), Box::new(Python3Plugin));
1718
plugins.insert("nodejs".to_string(), Box::new(NodeJSPlugin));
1819
plugins.insert("go".to_string(), Box::new(GoPlugin));
20+
plugins.insert("java".to_string(), Box::new(JavaPlugin));
1921

2022
Self { plugins }
2123
}

src-tauri/src/plugins/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::config::get_app_config_internal;
22
use log::{debug, info};
33
use serde::{Deserialize, Serialize};
4+
use std::format;
45
use std::path::PathBuf;
56

67
// 通用结构定义
@@ -329,6 +330,7 @@ pub trait LanguagePlugin: Send + Sync {
329330

330331
// 重新导出子模块
331332
pub mod go;
333+
pub mod java;
332334
pub mod manager;
333335
pub mod nodejs;
334336
pub mod python2;

0 commit comments

Comments
 (0)