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
17 changes: 11 additions & 6 deletions crates/xmake_ls/src/handlers/initialized/xmake_initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,25 @@ pub async fn init_xmake(context: &ServerContextSnapshot) {
};

log::info!("xmake path: {:?}", xmake_path);
let xmake_dir_path = match xmake_path.parent().map(|p| p.to_path_buf()) {
let xmake_program_dir = match context
.xmake()
.get_xmake_program_dir()
.await
.or_else(|| xmake_path.parent().map(|p| p.to_path_buf()))
{
Some(path) => path,
None => {
log::warn!("xmake directory not found");
let message = ShowMessageParams {
typ: lsp_types::MessageType::ERROR,
message: "xmake directory not found, Please set the XMAKE_ROOT or XMAKE_HOME environment variable. ".to_string(),
message: "xmake directory not found, Please set the XMAKE_ROOT or XMAKE_HOME environment variable.".to_string(),
};

context.client().show_message(message);
return;
}
};
log::info!("start to load xmake lib files from {:?}", xmake_dir_path);

log::info!("start to load xmake lib files from {:?}", xmake_program_dir);
let status_bar = context.status_bar();
status_bar
.create_progress_task(ProgressTask::XmakeLoad)
Expand All @@ -58,8 +63,8 @@ pub async fn init_xmake(context: &ServerContextSnapshot) {
let mut analysis = context.analysis().write().await;
let emmyrc = analysis.get_emmyrc();
let xmake_workspace = vec![
xmake_dir_path.join("core/sandbox/modules/import"),
xmake_dir_path.join("includes"),
xmake_program_dir.join("core/sandbox/modules/import"),
xmake_program_dir.join("includes"),
// other xmake lib paths can be added here if needed
];

Expand Down
31 changes: 21 additions & 10 deletions crates/xmake_wrapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ pub use executor::*;
use std::path::PathBuf;
use tokio::process::Command;

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

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

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

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

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

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

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

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

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

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

None
}

pub async fn get_xmake_program_dir(&self) -> Option<PathBuf> {
if let Ok(xmake_program_dir) = std::env::var("XMAKE_PROGRAM_DIR") {
let xmake_program_dir = PathBuf::from(xmake_program_dir);
if xmake_program_dir.exists() {
return Some(xmake_program_dir);
}
}

None
}
}

impl Default for XmakeWrapper {
Expand Down
Loading