Skip to content

Commit 4f1afbf

Browse files
authored
Merge pull request #2 from star-hengxing/main
Add XMAKE_PROGRAM_DIR env support
2 parents f6b95e8 + 95efcd5 commit 4f1afbf

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

crates/xmake_ls/src/handlers/initialized/xmake_initialize.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,25 @@ pub async fn init_xmake(context: &ServerContextSnapshot) {
3636
};
3737

3838
log::info!("xmake path: {:?}", xmake_path);
39-
let xmake_dir_path = match xmake_path.parent().map(|p| p.to_path_buf()) {
39+
let xmake_program_dir = match context
40+
.xmake()
41+
.get_xmake_program_dir()
42+
.await
43+
.or_else(|| xmake_path.parent().map(|p| p.to_path_buf()))
44+
{
4045
Some(path) => path,
4146
None => {
4247
log::warn!("xmake directory not found");
4348
let message = ShowMessageParams {
4449
typ: lsp_types::MessageType::ERROR,
45-
message: "xmake directory not found, Please set the XMAKE_ROOT or XMAKE_HOME environment variable. ".to_string(),
50+
message: "xmake directory not found, Please set the XMAKE_ROOT or XMAKE_HOME environment variable.".to_string(),
4651
};
47-
4852
context.client().show_message(message);
4953
return;
5054
}
5155
};
52-
log::info!("start to load xmake lib files from {:?}", xmake_dir_path);
56+
57+
log::info!("start to load xmake lib files from {:?}", xmake_program_dir);
5358
let status_bar = context.status_bar();
5459
status_bar
5560
.create_progress_task(ProgressTask::XmakeLoad)
@@ -58,8 +63,8 @@ pub async fn init_xmake(context: &ServerContextSnapshot) {
5863
let mut analysis = context.analysis().write().await;
5964
let emmyrc = analysis.get_emmyrc();
6065
let xmake_workspace = vec![
61-
xmake_dir_path.join("core/sandbox/modules/import"),
62-
xmake_dir_path.join("includes"),
66+
xmake_program_dir.join("core/sandbox/modules/import"),
67+
xmake_program_dir.join("includes"),
6368
// other xmake lib paths can be added here if needed
6469
];
6570

crates/xmake_wrapper/src/lib.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ pub use executor::*;
1111
use std::path::PathBuf;
1212
use tokio::process::Command;
1313

14-
/// xmake命令包装器的主要结构
14+
/// xmake 命令包装器的主要结构
1515
#[derive(Debug, Clone)]
1616
#[allow(dead_code)] // 这是一个库模块,某些方法可能在外部使用
1717
pub struct XmakeWrapper {
18-
/// xmake可执行文件路径
18+
/// xmake 可执行文件路径
1919
pub xmake_env: String,
2020
/// 工作目录
2121
pub working_dir: Option<PathBuf>,
2222
}
2323

2424
#[allow(dead_code)] // 这是一个库模块,某些方法可能在外部使用
2525
impl XmakeWrapper {
26-
/// 创建新的xmake包装器实例
26+
/// 创建新的 xmake 包装器实例
2727
pub fn new() -> Self {
2828
Self {
2929
xmake_env: "xmake".to_string(),
3030
working_dir: None,
3131
}
3232
}
3333

34-
/// 使用自定义xmake路径创建实例
34+
/// 使用自定义 xmake 路径创建实例
3535
pub fn with_path<P: AsRef<str>>(xmake_path: P) -> Self {
3636
Self {
3737
xmake_env: xmake_path.as_ref().to_string(),
@@ -45,17 +45,17 @@ impl XmakeWrapper {
4545
self
4646
}
4747

48-
/// 创建一个新的xmake命令
48+
/// 创建一个新的 xmake 命令
4949
pub fn command(&self) -> XmakeCommandBuilder {
5050
XmakeCommandBuilder::new(self.clone())
5151
}
5252

53-
/// 执行xmake命令
53+
/// 执行 xmake 命令
5454
pub async fn execute(&self, cmd: &XmakeCommand) -> Result<XmakeOutput, XmakeError> {
5555
execute_command(self, cmd).await
5656
}
5757

58-
/// 检查xmake是否可用
58+
/// 检查 xmake 是否可用
5959
pub async fn check_available(&self) -> bool {
6060
let result = Command::new(&self.xmake_env)
6161
.arg("--version")
@@ -69,13 +69,13 @@ impl XmakeWrapper {
6969
}
7070

7171
pub async fn get_xmake_path(&self) -> Option<PathBuf> {
72-
// 首先尝试直接解析当前配置的xmake_env路径
72+
// 首先尝试直接解析当前配置的 xmake_env 路径
7373
let xmake_path = PathBuf::from(&self.xmake_env);
7474
if xmake_path.is_absolute() && xmake_path.exists() {
7575
return Some(xmake_path);
7676
}
7777

78-
// 尝试从环境变量获取xmake路径
78+
// 尝试从环境变量获取 xmake 路径
7979
if let Ok(xmake_root) = std::env::var("XMAKE_ROOT") {
8080
let xmake_exe = PathBuf::from(xmake_root).join("bin").join("xmake");
8181
if xmake_exe.exists() {
@@ -90,7 +90,7 @@ impl XmakeWrapper {
9090
}
9191
}
9292

93-
// 使用which/where命令在PATH中查找xmake
93+
// 使用 which/where 命令在 PATH 中查找 xmake
9494
let which_cmd = if cfg!(windows) { "where" } else { "which" };
9595

9696
let result = Command::new(which_cmd).arg(&self.xmake_env).output().await;
@@ -109,6 +109,17 @@ impl XmakeWrapper {
109109

110110
None
111111
}
112+
113+
pub async fn get_xmake_program_dir(&self) -> Option<PathBuf> {
114+
if let Ok(xmake_program_dir) = std::env::var("XMAKE_PROGRAM_DIR") {
115+
let xmake_program_dir = PathBuf::from(xmake_program_dir);
116+
if xmake_program_dir.exists() {
117+
return Some(xmake_program_dir);
118+
}
119+
}
120+
121+
None
122+
}
112123
}
113124

114125
impl Default for XmakeWrapper {

0 commit comments

Comments
 (0)