From d69761f1dc75599b1314a368ae98590ff79bb4c8 Mon Sep 17 00:00:00 2001 From: Daniel Gavin Date: Sat, 23 Jul 2022 15:54:34 +0200 Subject: Make ols more robust by also looking in the path environment for odin --- src/common/util.odin | 37 +++++++++++++++++++++++++++++++++++++ src/server/requests.odin | 6 ++++++ 2 files changed, 43 insertions(+) create mode 100644 src/common/util.odin diff --git a/src/common/util.odin b/src/common/util.odin new file mode 100644 index 0000000..662d30d --- /dev/null +++ b/src/common/util.odin @@ -0,0 +1,37 @@ +package common + +import "core:fmt" +import "core:os" +import "core:strings" +import "core:path/filepath" + +when ODIN_OS == .Windows { + delimiter :: ";" +} else { + delimiter :: ":" +} + +//TODO(daniel): This is temporary and should not be needed after os2 +File_Mode_User_Executable :: os.File_Mode(1 << 8) + +lookup_in_path :: proc(name: string) -> (string, bool) { + path := os.get_env("path", context.temp_allocator) + + for directory in strings.split_iterator(&path, delimiter) { + when ODIN_OS == .Windows { + name := fmt.tprintf("%v/%v.exe", directory, name) + if os.exists(name) { + return name, true + } + } else { + name := fmt.tprintf("%v/%v", directory, name) + if os.exists(name) { + if info, err := os.stat(name, context.temp_allocator); err == os.ERROR_NONE && (File_Mode_User_Executable & info.mode) != 0 { + return name, true + } + } + } + } + + return "", false +} diff --git a/src/server/requests.odin b/src/server/requests.odin index af03d90..47f0d74 100644 --- a/src/server/requests.odin +++ b/src/server/requests.odin @@ -449,6 +449,12 @@ request_initialize :: proc (params: json.Value, id: RequestId, config: ^common.C odin_core_env := os.get_env("ODIN_ROOT", context.temp_allocator) + if odin_core_env == "" && "core" not_in config.collections { + if exe_path, ok := common.lookup_in_path("odin"); ok { + odin_core_env = path.dir(exe_path, context.temp_allocator) + } + } + if "core" not_in config.collections && odin_core_env != "" { forward_path, _ := filepath.to_slash(odin_core_env, context.temp_allocator) config.collections["core"] = path.join(elems = {forward_path, "core"}, allocator = context.allocator) -- cgit v1.2.3