[macOS] Add support for loading shell environment from UI apps.

This commit is contained in:
bruvzg 2023-09-03 11:44:18 +03:00 committed by Pāvels Nadtočajevs
parent e45cc68092
commit ee181951b6
No known key found for this signature in database
GPG key ID: 8413210218EF35D2
5 changed files with 40 additions and 0 deletions

View file

@ -238,6 +238,32 @@ List<String> OS_MacOS::get_cmdline_platform_args() const {
return launch_service_args;
}
void OS_MacOS::load_shell_environment() const {
static bool shell_env_loaded = false;
if (unlikely(!shell_env_loaded)) {
shell_env_loaded = true;
if (OS::get_singleton()->has_environment("TERM") || OS::get_singleton()->has_environment("__GODOT_SHELL_ENV_SET")) {
return; // Already started from terminal, or other the instance with the shell environment, do nothing.
}
String pipe;
List<String> args;
args.push_back("-c");
args.push_back(". /etc/zshrc;. /etc/zprofile;. ~/.zshenv;. ~/.zshrc;. ~/.zprofile;env");
Error err = OS::get_singleton()->execute("zsh", args, &pipe);
if (err == OK) {
Vector<String> env_vars = pipe.split("\n");
for (const String &E : env_vars) {
Vector<String> tags = E.split("=", 2);
if (tags.size() != 2 || tags[0] == "SHELL" || tags[0] == "USER" || tags[0] == "COMMAND_MODE" || tags[0] == "TMPDIR" || tags[0] == "TERM_SESSION_ID" || tags[0] == "PWD" || tags[0] == "OLDPWD" || tags[0] == "SHLVL" || tags[0] == "HOME" || tags[0] == "DISPLAY" || tags[0] == "LOGNAME" || tags[0] == "TERM" || tags[0] == "COLORTERM" || tags[0] == "_" || tags[0].begins_with("__CF") || tags[0].begins_with("XPC_") || tags[0].begins_with("__GODOT")) {
continue;
}
OS::get_singleton()->set_environment(tags[0], tags[1]);
}
}
OS::get_singleton()->set_environment("__GODOT_SHELL_ENV_SET", "1");
}
}
String OS_MacOS::get_name() const {
return "macOS";
}