diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2024-04-16 00:05:17 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-16 00:05:17 +0200 |
| commit | 93e92be7084e0c2d96c88e8f97b6238c5e476c5d (patch) | |
| tree | df7d90fb98bedd39f386a1d3c535e7801e4f5267 /src/common | |
| parent | 4c4b525a9730bcb2c932b8be678d7eaef96e3ff1 (diff) | |
| parent | 761539fd7a8f9b6e4c1407c7487ce5b9d4b3427b (diff) | |
Merge pull request #349 from laytan/use-odin-root-command
use the new `odin root` command to resolve the builtin collections
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/util.odin | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/src/common/util.odin b/src/common/util.odin index 0466b6e..784a193 100644 --- a/src/common/util.odin +++ b/src/common/util.odin @@ -1,12 +1,14 @@ package common +import "core:bytes" import "core:fmt" +import "core:log" +import "core:mem" import "core:os" -import "core:strings" import "core:path/filepath" +import "core:strings" + foreign import libc "system:c" -import "core:mem" -import "core:bytes" when ODIN_OS == .Windows { delimiter :: ";" @@ -22,23 +24,24 @@ lookup_in_path :: proc(name: string) -> (string, bool) { for directory in strings.split_iterator(&path, delimiter) { when ODIN_OS == .Windows { - name := filepath.join( + possibility := filepath.join( elems = {directory, fmt.tprintf("%v.exe", name)}, allocator = context.temp_allocator, ) - if os.exists(name) { - return name, true + if os.exists(possibility) { + return possibility, true } } else { - name := filepath.join( + possibility := filepath.join( elems = {directory, name}, allocator = context.temp_allocator, ) - if os.exists(name) { - if info, err := os.stat(name, context.temp_allocator); + possibility = resolve_home_dir(possibility, context.temp_allocator) + if os.exists(possibility) { + if info, err := os.stat(possibility, context.temp_allocator); err == os.ERROR_NONE && (File_Mode_User_Executable & info.mode) != 0 { - return name, true + return possibility, true } } } @@ -47,6 +50,32 @@ lookup_in_path :: proc(name: string) -> (string, bool) { return "", false } +resolve_home_dir :: proc( + path: string, + allocator := context.allocator, +) -> ( + resolved: string, + allocated: bool, +) #optional_ok { + when ODIN_OS == .Windows { + return path, false + } else { + if !strings.has_prefix(path, "~") { + return path, false + } + + home := os.get_env("HOME", context.temp_allocator) + if home == "" { + log.error( + "could not find $HOME in the environment to be able to resolve ~ in collection paths", + ) + return path, false + } + + return filepath.join({home, path[1:]}, allocator), true + } +} + when ODIN_OS == .Darwin || ODIN_OS == .Linux { FILE :: struct {} |