Skip to content

Commit d41645e

Browse files
committed
support resource ouput default to xdg path
1 parent 942c324 commit d41645e

File tree

4 files changed

+96
-13
lines changed

4 files changed

+96
-13
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::path::PathBuf;
2+
3+
pub fn get_best_resources_dir() -> PathBuf {
4+
if cfg!(debug_assertions) {
5+
let exe_path = std::env::current_exe().unwrap();
6+
let exe_dir = exe_path.parent().unwrap();
7+
return exe_dir.join("resources");
8+
}
9+
10+
if cfg!(target_os = "windows") {
11+
// On Windows, try LOCALAPPDATA first
12+
if let Ok(local_app_data) = std::env::var("LOCALAPPDATA") {
13+
PathBuf::from(local_app_data)
14+
.join("emmylua_ls")
15+
.join("resources")
16+
} else {
17+
// Fall back to the directory next to the executable
18+
let exe_path = std::env::current_exe().unwrap();
19+
let exe_dir = exe_path.parent().unwrap();
20+
exe_dir.join("resources")
21+
}
22+
} else {
23+
// On non-Windows platforms, try XDG_DATA_HOME first
24+
if let Ok(xdg_data_home) = std::env::var("XDG_DATA_HOME") {
25+
PathBuf::from(xdg_data_home)
26+
.join("emmylua_ls")
27+
.join("resources")
28+
} else {
29+
// If XDG_DATA_HOME is not set, use default XDG path
30+
if let Ok(home) = std::env::var("HOME") {
31+
PathBuf::from(home)
32+
.join(".local")
33+
.join("share")
34+
.join("emmylua_ls")
35+
.join("resources")
36+
} else {
37+
// Fall back to the directory next to the executable
38+
let exe_path = std::env::current_exe().unwrap();
39+
let exe_dir = exe_path.parent().unwrap();
40+
exe_dir.join("resources")
41+
}
42+
}
43+
}
44+
}

crates/emmylua_code_analysis/src/resources/mod.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
mod best_resource_path;
2+
13
use std::path::{Path, PathBuf};
24

5+
use best_resource_path::get_best_resources_dir;
36
use include_dir::{include_dir, Dir, DirEntry};
47

58
use crate::{load_workspace_files, LuaFileInfo};
@@ -10,10 +13,7 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
1013
pub fn load_resource_std(create_resources_dir: Option<String>) -> (PathBuf, Vec<LuaFileInfo>) {
1114
if let Some(create_resources_dir) = create_resources_dir {
1215
let resource_path = if create_resources_dir.is_empty() {
13-
let exe_path = std::env::current_exe().unwrap();
14-
let exe_dir = exe_path.parent().unwrap();
15-
let resoucres_dir = exe_dir.join("resources");
16-
resoucres_dir
16+
get_best_resources_dir()
1717
} else {
1818
PathBuf::from(&create_resources_dir)
1919
};
@@ -25,9 +25,7 @@ pub fn load_resource_std(create_resources_dir: Option<String>) -> (PathBuf, Vec<
2525
}
2626
}
2727

28-
let exe_path = std::env::current_exe().unwrap();
29-
let exe_dir = exe_path.parent().unwrap();
30-
let resoucres_dir = exe_dir.join("resources");
28+
let resoucres_dir = get_best_resources_dir();
3129
let std_dir = resoucres_dir.join("std");
3230
let files = load_resource_from_include_dir();
3331
let files = files
@@ -104,9 +102,7 @@ fn check_need_dump_to_file_system() -> bool {
104102
return true;
105103
}
106104

107-
let exe_path = std::env::current_exe().unwrap();
108-
let exe_dir = exe_path.parent().unwrap();
109-
let resoucres_dir = exe_dir.join("resources");
105+
let resoucres_dir = get_best_resources_dir();
110106
let version_path = resoucres_dir.join("version");
111107

112108
if !version_path.exists() {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::path::PathBuf;
2+
3+
pub fn get_best_log_dir() -> PathBuf {
4+
if cfg!(debug_assertions) {
5+
let exe_path = std::env::current_exe().unwrap();
6+
let exe_dir = exe_path.parent().unwrap();
7+
return exe_dir.join("logs");
8+
}
9+
10+
if cfg!(target_os = "windows") {
11+
// On Windows, try LOCALAPPDATA first
12+
if let Ok(local_app_data) = std::env::var("LOCALAPPDATA") {
13+
PathBuf::from(local_app_data)
14+
.join("emmylua_ls")
15+
.join("logs")
16+
} else {
17+
// Fall back to the directory next to the executable
18+
let exe_path = std::env::current_exe().unwrap();
19+
let exe_dir = exe_path.parent().unwrap();
20+
exe_dir.join("logs")
21+
}
22+
} else {
23+
// On non-Windows platforms, try XDG_DATA_HOME first
24+
if let Ok(xdg_data_home) = std::env::var("XDG_DATA_HOME") {
25+
PathBuf::from(xdg_data_home).join("emmylua_ls").join("logs")
26+
} else {
27+
// If XDG_DATA_HOME is not set, use default XDG path
28+
if let Ok(home) = std::env::var("HOME") {
29+
PathBuf::from(home)
30+
.join(".local")
31+
.join("share")
32+
.join("emmylua_ls")
33+
.join("logs")
34+
} else {
35+
// Fall back to the directory next to the executable
36+
let exe_path = std::env::current_exe().unwrap();
37+
let exe_dir = exe_path.parent().unwrap();
38+
exe_dir.join("logs")
39+
}
40+
}
41+
}
42+
}

crates/emmylua_ls/src/logger/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
mod best_log_path;
2+
13
use std::{env, fs, path::PathBuf};
24

5+
use best_log_path::get_best_log_dir;
36
use chrono::Local;
47
use emmylua_code_analysis::file_path_to_uri;
58
use fern::Dispatch;
@@ -36,10 +39,8 @@ pub fn init_logger(root: Option<&str>, cmd_args: &CmdArgs) {
3639
.join("_")
3740
};
3841

39-
let exe_path = env::current_exe().unwrap();
40-
let exe_dir = exe_path.parent().unwrap();
4142
let log_dir = if cmd_log_path.is_empty() {
42-
exe_dir.join("logs")
43+
get_best_log_dir()
4344
} else {
4445
PathBuf::from(cmd_log_path.as_str())
4546
};

0 commit comments

Comments
 (0)