Initial commit
This commit is contained in:
59
lua/dep/log.lua
Normal file
59
lua/dep/log.lua
Normal file
@ -0,0 +1,59 @@
|
||||
local logger = {
|
||||
path = vim.fn.stdpath("cache") .. "/dep.log",
|
||||
silent = false,
|
||||
}
|
||||
|
||||
local colors = {
|
||||
install = "MoreMsg",
|
||||
update = "WarningMsg",
|
||||
delete = "Directory",
|
||||
error = "ErrorMsg",
|
||||
}
|
||||
|
||||
function logger:open(path)
|
||||
self:close()
|
||||
|
||||
self.path = path or self.path
|
||||
self.handle = vim.loop.fs_open(self.path, "w", 0x1A4) -- 0644
|
||||
self.pipe = vim.loop.new_pipe()
|
||||
|
||||
self.pipe:open(self.handle)
|
||||
end
|
||||
|
||||
function logger:close()
|
||||
if self.pipe then
|
||||
self.pipe:close()
|
||||
self.pipe = nil
|
||||
end
|
||||
|
||||
if self.handle then
|
||||
vim.loop.fs_close(self.handle)
|
||||
self.handle = nil
|
||||
end
|
||||
end
|
||||
|
||||
function logger:log(op, message, cb)
|
||||
if not self.silent and colors[op] then
|
||||
vim.api.nvim_echo({
|
||||
{ "[dep]", "Identifier" },
|
||||
{ " " },
|
||||
{ message, colors[op] },
|
||||
}, false, {})
|
||||
end
|
||||
|
||||
if self.pipe then
|
||||
local source = debug.getinfo(2, "Sl").short_src
|
||||
local message = string.format("[%s] %s: %s\n", os.date(), source, message)
|
||||
|
||||
self.pipe:write(
|
||||
message,
|
||||
vim.schedule_wrap(function(err)
|
||||
if cb then
|
||||
cb(err)
|
||||
end
|
||||
end)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
return logger
|
74
lua/dep/proc.lua
Normal file
74
lua/dep/proc.lua
Normal file
@ -0,0 +1,74 @@
|
||||
local logger = require("dep/log")
|
||||
local proc = {}
|
||||
|
||||
function proc.exec(process, args, cwd, env, cb)
|
||||
local out = vim.loop.new_pipe()
|
||||
local buffer = {}
|
||||
|
||||
local handle = vim.loop.spawn(
|
||||
process,
|
||||
{ args = args, cwd = cwd, env = env, stdio = { nil, out, out } },
|
||||
vim.schedule_wrap(function(code)
|
||||
handle:close()
|
||||
|
||||
local output = table.concat(buffer)
|
||||
|
||||
logger:log(
|
||||
process,
|
||||
string.format('executed `%s` with args: "%s"\n%s', process, table.concat(args, '", "'), output)
|
||||
)
|
||||
|
||||
cb(code, output)
|
||||
end)
|
||||
)
|
||||
|
||||
vim.loop.read_start(
|
||||
out,
|
||||
vim.schedule_wrap(function(_, data)
|
||||
if data then
|
||||
table.insert(buffer, data)
|
||||
else
|
||||
out:close()
|
||||
end
|
||||
end)
|
||||
)
|
||||
end
|
||||
|
||||
local git_env = { "GIT_TERMINAL_PROMPT=0" }
|
||||
|
||||
function proc.git_current_commit(dir, cb)
|
||||
exec("git", { "rev-parse", "HEAD" }, dir, git_env, cb)
|
||||
end
|
||||
|
||||
function proc.git_clone(dir, url, branch, cb)
|
||||
local args = { "--depth=1", "--recurse-submodules", "--shallow-submodules", url, dir }
|
||||
|
||||
if branch then
|
||||
table.insert(args, "--branch=" .. branch)
|
||||
end
|
||||
|
||||
exec("git", args, nil, git_env, cb)
|
||||
end
|
||||
|
||||
function proc.git_fetch(dir, branch, cb)
|
||||
local args = { "--depth=1", "--recurse-submodules" }
|
||||
|
||||
if branch then
|
||||
table.insert(args, "origin")
|
||||
table.insert(args, branch)
|
||||
end
|
||||
|
||||
exec("git", args, dir, git_env, cb)
|
||||
end
|
||||
|
||||
function proc.git_reset(dir, branch, cb)
|
||||
local args = { "--hard", "--recurse-submodules" }
|
||||
|
||||
if branch then
|
||||
table.insert("origin/" .. branch)
|
||||
end
|
||||
|
||||
exec("git", args, dir, git_env, cb)
|
||||
end
|
||||
|
||||
return proc
|
Reference in New Issue
Block a user