aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/installer/context/fs.lua
blob: 93379017c4683ff2377b27409603ddcaf5293707 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
local fs = require "mason-core.fs"
local log = require "mason-core.log"
local path = require "mason-core.path"

---@class InstallContextFs
---@field private cwd InstallContextCwd
local InstallContextFs = {}
InstallContextFs.__index = InstallContextFs

---@param cwd InstallContextCwd
function InstallContextFs:new(cwd)
    ---@type InstallContextFs
    local instance = {}
    setmetatable(instance, InstallContextFs)
    instance.cwd = cwd
    return instance
end

---@async
---@param rel_path string The relative path from the current working directory to the file to append.
---@param contents string
function InstallContextFs:append_file(rel_path, contents)
    return fs.async.append_file(path.concat { self.cwd:get(), rel_path }, contents)
end

---@async
---@param rel_path string The relative path from the current working directory to the file to write.
---@param contents string
function InstallContextFs:write_file(rel_path, contents)
    return fs.async.write_file(path.concat { self.cwd:get(), rel_path }, contents)
end

---@async
---@param rel_path string The relative path from the current working directory to the file to read.
function InstallContextFs:read_file(rel_path)
    return fs.async.read_file(path.concat { self.cwd:get(), rel_path })
end

---@async
---@param rel_path string The relative path from the current working directory.
function InstallContextFs:file_exists(rel_path)
    return fs.async.file_exists(path.concat { self.cwd:get(), rel_path })
end

---@async
---@param rel_path string The relative path from the current working directory.
function InstallContextFs:dir_exists(rel_path)
    return fs.async.dir_exists(path.concat { self.cwd:get(), rel_path })
end

---@async
---@param rel_path string The relative path from the current working directory.
function InstallContextFs:rmrf(rel_path)
    return fs.async.rmrf(path.concat { self.cwd:get(), rel_path })
end

---@async
---@param rel_path string The relative path from the current working directory.
function InstallContextFs:unlink(rel_path)
    return fs.async.unlink(path.concat { self.cwd:get(), rel_path })
end

---@async
---@param old_path string
---@param new_path string
function InstallContextFs:rename(old_path, new_path)
    return fs.async.rename(path.concat { self.cwd:get(), old_path }, path.concat { self.cwd:get(), new_path })
end

---@async
---@param dir_path string
function InstallContextFs:mkdir(dir_path)
    return fs.async.mkdir(path.concat { self.cwd:get(), dir_path })
end

---@async
---@param dir_path string
function InstallContextFs:mkdirp(dir_path)
    return fs.async.mkdirp(path.concat { self.cwd:get(), dir_path })
end

---@async
---@param file_path string
function InstallContextFs:chmod_exec(file_path)
    local bit = require "bit"
    -- see chmod(2)
    local USR_EXEC = 0x40
    local GRP_EXEC = 0x8
    local ALL_EXEC = 0x1
    local EXEC = bit.bor(USR_EXEC, GRP_EXEC, ALL_EXEC)
    local fstat = self:fstat(file_path)
    if bit.band(fstat.mode, EXEC) ~= EXEC then
        local plus_exec = bit.bor(fstat.mode, EXEC)
        log.fmt_debug("Setting exec flags on file %s %o -> %o", file_path, fstat.mode, plus_exec)
        self:chmod(file_path, plus_exec) -- chmod +x
    end
end

---@async
---@param file_path string
---@param mode integer
function InstallContextFs:chmod(file_path, mode)
    return fs.async.chmod(path.concat { self.cwd:get(), file_path }, mode)
end

---@async
---@param file_path string
function InstallContextFs:fstat(file_path)
    return fs.async.fstat(path.concat { self.cwd:get(), file_path })
end

return InstallContextFs