-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.cppm
More file actions
80 lines (66 loc) · 2.58 KB
/
platform.cppm
File metadata and controls
80 lines (66 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
export module d2x.platform;
import std;
export import :windows;
export import :linux;
export import :macos;
namespace d2x {
namespace platform {
static std::string gRundir = std::filesystem::current_path().string();
export using platform_impl::get_xlings_bin;
export using platform_impl::XLINGS_INSTALL_CMD;
export using platform_impl::clear_console;
export using platform_impl::get_home_dir;
//export using platform_impl::xlings_install;
export using platform_impl::set_env_variable;
export using platform_impl::println;
export [[nodiscard]] std::string get_rundir() {
return gRundir;
}
export [[nodiscard]] std::string get_system_language() {
try {
// Get the system's default locale
auto loc = std::locale("");
auto name = loc.name();
// Extract language code from locale name
// Format examples: "zh_CN.UTF-8", "en_US.UTF-8", "C", "POSIX"
if (name.empty() || name == "C" || name == "POSIX") {
return "en";
}
// Find first delimiter and extract language code
if (auto pos = name.find_first_of("_-.@"); pos != std::string::npos) {
return name.substr(0, pos);
}
return name;
} catch (const std::runtime_error&) {
// Locale initialization failed, fallback to English
return "en";
}
}
export std::pair<int, std::string> run_command_capture(const std::string& cmd) {
set_env_variable("LD_LIBRARY_PATH", "");
return platform_impl::run_command_capture(cmd);
}
export int exec(const std::string& cmd) {
// TODO: fix return 139 issue (on linux)
// workaround by clear LD_LIBRARY_PATH
set_env_variable("LD_LIBRARY_PATH", "");
return std::system(cmd.c_str());
}
export bool xlings_install() {
std::println("正在安装 xlings...");
int status = platform::exec(std::string(XLINGS_INSTALL_CMD));
if (status == 0) {
std::println("xlings 安装成功!");
std::string xlings_path { std::filesystem::path(get_xlings_bin()).parent_path().string() };
char* path_env = std::getenv("PATH");
if (path_env) {
std::string new_path = std::string(path_env) + ";" + xlings_path;
set_env_variable("PATH", new_path.c_str());
}
return true;
}
std::println("xlings 安装失败");
return false;
}
} // namespace platform
} // namespace d2x