Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor run
Browse files Browse the repository at this point in the history
- Rename `call_proc` to `run`.
- Store all process output in a table before writing to log file.
savq committed Aug 6, 2023

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent 973e15c commit af4f0e8
Showing 1 changed file with 26 additions and 35 deletions.
61 changes: 26 additions & 35 deletions lua/paq.lua
Original file line number Diff line number Diff line change
@@ -125,23 +125,35 @@ local function new_counter(total, callback)
end)
end

local function call_proc(process, args, cwd, cb, print_stdout)
local log = uv.fs_open(logfile, "a+", 0x1A4)
local stderr = uv.new_pipe(false)
stderr:open(log)
local function run(command, args, cwd, callback, output)
-- Create table that will collect command output
output = output or {}
local stdout = uv.new_pipe()
local stderr = uv.new_pipe()
stdout:read_start(function(err, data)
assert(not err, err)
return data and table.insert(output, data) or stdout:close()
end)
stderr:read_start(function(err, data)
assert(not err, err)
return data and table.insert(output, data) or stderr:close()
end)

local handle, pid
handle, pid = uv.spawn(
process,
{ args = args, cwd = cwd, stdio = { nil, print_stdout and stderr, stderr }, env = env },
command,
{ args = args, cwd = cwd, stdio = { nil, stdout, stderr }, env = env },
vim.schedule_wrap(function(code)
-- Flush output to log
local log = uv.fs_open(logfile, "a+", 0x1A4)
uv.fs_write(log, output)
uv.fs_close(log)
stderr:close()
handle:close()
cb(code == 0)
return callback and callback(code == 0)
end)
)
if not handle then
vim.notify(string.format(" Paq: Failed to spawn %s (%s)", process, pid))
vim.notify(string.format(" Paq: Failed to spawn %s (%s)", command, pid))
end
end

@@ -158,7 +170,7 @@ local function run_build(pkg)
for word in pkg.build:gmatch("%S+") do
table.insert(args, word)
end
call_proc(table.remove(args, 1), args, pkg.dir, function(ok)
run(table.remove(args, 1), args, pkg.dir, function(ok)
report(pkg.name, messages.build, ok and "ok" or "err")
end)
end
@@ -170,7 +182,7 @@ local function clone(pkg, counter, build_queue)
vim.list_extend(args, { "-b", pkg.branch })
end
vim.list_extend(args, { pkg.dir })
call_proc("git", args, nil, function(ok)
run("git", args, nil, function(ok)
if ok then
pkg.exists = true
pkg.status = status.INSTALLED
@@ -195,37 +207,16 @@ local function get_git_hash(dir)
return head_ref and first_line(dir .. "/.git/" .. head_ref:gsub("ref: ", ""))
end

local function log_update_changes(pkg, prev_hash, cur_hash)
local output = { "\n\n" .. pkg.name .. " updated:\n" }
local stdout = uv.new_pipe()
local options = {
args = { "log", "--pretty=format:* %s", prev_hash .. ".." .. cur_hash },
cwd = pkg.dir,
stdio = { nil, stdout, nil },
}
local handle
handle, _ = uv.spawn("git", options, function(code)
assert(code == 0, "Exited(" .. code .. ")")
handle:close()
local log = uv.fs_open(logfile, "a+", 0x1A4)
uv.fs_write(log, output, nil, nil)
uv.fs_close(log)
end)
stdout:read_start(function(err, data)
assert(not err, err)
table.insert(output, data)
end)
end

local function pull(pkg, counter, build_queue)
local prev_hash = lock[pkg.name] and lock[pkg.name].hash or pkg.hash
call_proc("git", { "pull", "--recurse-submodules", "--update-shallow" }, pkg.dir, function(ok)
run("git", { "pull", "--recurse-submodules", "--update-shallow" }, pkg.dir, function(ok)
if not ok then
counter(pkg.name, messages.update, "err")
else
local cur_hash = pkg.hash
if cur_hash ~= prev_hash then
log_update_changes(pkg, prev_hash, cur_hash)
local range = prev_hash .. ".." .. cur_hash
run("git", { "log", "--oneline", range }, pkg.dir, nil, { "\nUpdated " .. pkg.name .. ":" })
pkg.status = status.UPDATED
counter(pkg.name, messages.update, "ok")
if pkg.build then

0 comments on commit af4f0e8

Please sign in to comment.