aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/path.lua
blob: 1e0038be49a145877688d0eb00905ae3f8df8fa7 (plain) (blame)
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
local sep = (function()
    ---@diagnostic disable-next-line: undefined-global
    if jit then
        ---@diagnostic disable-next-line: undefined-global
        local os = string.lower(jit.os)
        if os == "linux" or os == "osx" or os == "bsd" then
            return "/"
        else
            return "\\"
        end
    else
        return string.sub(package.config, 1, 1)
    end
end)()

local M = {}

---@param path_components string[]
---@return string
function M.concat(path_components)
    return table.concat(path_components, sep)
end

---@path root_path string
---@path path string
function M.is_subdirectory(root_path, path)
    return root_path == path or path:sub(1, #root_path + 1) == root_path .. sep
end

---@param dir string?
function M.install_prefix(dir)
    local settings = require "mason.settings"
    return M.concat { settings.current.install_root_dir, dir }
end

---@param executable string?
function M.bin_prefix(executable)
    return M.concat { M.install_prefix "bin", executable }
end

---@param file string?
function M.share_prefix(file)
    return M.concat { M.install_prefix "share", file }
end

---@param file string?
function M.opt_prefix(file)
    return M.concat { M.install_prefix "opt", file }
end

---@param name string?
function M.package_prefix(name)
    return M.concat { M.install_prefix "packages", name }
end

---@param name string?
function M.package_build_prefix(name)
    return M.concat { M.install_prefix ".packages", name }
end

function M.registry_prefix()
    return M.install_prefix "registries"
end

return M