olexsmir.xyz/lua/lego/file.lua(view raw)
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 |
local file = {}
local _writes = {}
function file.report_duplicates()
local freq = {}
local duplicates = {}
for _, v in ipairs(_writes) do
freq[v] = (freq[v] or 0) + 1
end
for value, count in pairs(freq) do
if count > 1 then
duplicates[#duplicates + 1] = value
end
end
if #duplicates > 0 then
vim.print("duplicates " .. vim.inspect(duplicates))
end
end
---@alias lego.FilePath string|string[]
---@param p lego.FilePath
---@return string
function file.to_path(p)
if type(p) == "table" then
return vim.fs.joinpath(unpack(p))
end
return p
end
---@param path lego.FilePath
---@return string[]
function file.list_dir(path)
return vim.fn.readdir(file.to_path(path))
end
---@param path lego.FilePath
function file.read(path)
return vim.fn.readfile(file.to_path(path))
end
---@param path lego.FilePath
---@param content string
function file.write(path, content)
path = file.to_path(path)
table.insert(_writes, path)
vim.print("writing " .. path)
vim.fn.writefile(vim.split(content, "\n", { plain = true }), path)
end
---@param path lego.FilePath
function file.rm(path)
path = file.to_path(path)
vim.print("deleting " .. path)
vim.fs.rm(path, { force = true, recursive = true })
end
---@param path string
function file.mkdir(path)
vim.fn.mkdir(file.to_path(path), "p")
end
---@param path string
function file.is_dir(path)
local build_dir_stats = vim.uv.fs_stat(file.to_path(path))
return not build_dir_stats or build_dir_stats.type == "directory"
end
---@param from string
---@param to string
function file.copy_dir(from, to)
from = file.to_path(from)
to = file.to_path(to)
vim.print("copying " .. to)
file.mkdir(to)
for _, f in ipairs(vim.fn.readdir(from)) do
vim.uv.fs_copyfile(vim.fs.joinpath(from, f), vim.fs.joinpath(to, f))
end
end
return file
|