mirror of
https://github.com/boxpositron/absolute-vim.git
synced 2026-02-28 19:50:38 +00:00
feat(detect-os.lua): add script to detect the operating system and return
the corresponding value based on the environment variables and system commands available
This commit is contained in:
43
lua/absolute/utils/detect-os.lua
Normal file
43
lua/absolute/utils/detect-os.lua
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
SUPPORTED_OS = {
|
||||||
|
WINDOWS = "Windows",
|
||||||
|
UNIX = "Unix",
|
||||||
|
MACOS = "macOS",
|
||||||
|
}
|
||||||
|
|
||||||
|
local function isMacOS()
|
||||||
|
-- Try to execute `sw_vers` command which is specific to macOS
|
||||||
|
local result = os.execute("sw_vers > /dev/null 2>&1")
|
||||||
|
|
||||||
|
-- `os.execute` returns true on success for Lua versions 5.2 and above
|
||||||
|
-- For Lua 5.1 and below, it returns the exit code of the command. On success, it should be zero.
|
||||||
|
-- Adjust the check if you're using Lua 5.1 or below:
|
||||||
|
-- if result == 0 then
|
||||||
|
if result then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function detectOS()
|
||||||
|
if os.getenv("OS") ~= nil then
|
||||||
|
return os.getenv("OS") -- This can be useful in Windows environments
|
||||||
|
elseif os.getenv("PATH"):find(";") then
|
||||||
|
return SUPPORTED_OS.WINDOWS
|
||||||
|
else
|
||||||
|
local is_macos = isMacOS()
|
||||||
|
|
||||||
|
if is_macos then
|
||||||
|
return SUPPORTED_OS.MACOS
|
||||||
|
else
|
||||||
|
return SUPPORTED_OS.UNIX
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
M = {}
|
||||||
|
|
||||||
|
M.detect = detectOS
|
||||||
|
M.supported = SUPPORTED_OS
|
||||||
|
|
||||||
|
return M
|
||||||
Reference in New Issue
Block a user