Skip to content

Commit 898b0e7

Browse files
committed
refactor: decouple build_queue from functions
1 parent e388faa commit 898b0e7

File tree

1 file changed

+48
-60
lines changed

1 file changed

+48
-60
lines changed

‎lua/pack.lua

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ M.status = {
2626
TO_RECLONE = 6,
2727
}
2828

29-
--- @nodoc
29+
--- List of packages to build
30+
local BuildQueue = {}
31+
3032
--- Table of pgks loaded from the lockfile
3133
--- @type pack.Package[]
3234
local Lock = {}
3335

34-
--- @nodoc
3536
--- Table of pkgs loaded from the user configuration
3637
--- @type pack.Package[]
3738
local Packages = {}
3839

39-
--- @nodoc
4040
--- @type table<string, string>
4141
local Path = {
4242
lock = vim.fs.joinpath(vim.fn.stdpath('state'), 'pack-lock.json'),
@@ -223,8 +223,7 @@ end
223223

224224
--- @param pkg pack.Package
225225
--- @param counter function
226-
--- @param build_queue table
227-
local function clone(pkg, counter, build_queue)
226+
local function clone(pkg, counter)
228227
local args = vim.list_extend({ 'git', 'clone', pkg.url }, Config.clone_args)
229228
if pkg.branch then
230229
vim.list_extend(args, { '-b', pkg.branch })
@@ -236,7 +235,7 @@ local function clone(pkg, counter, build_queue)
236235
pkg.status = M.status.CLONED
237236
lock_write()
238237
if pkg.build then
239-
table.insert(build_queue, pkg)
238+
table.insert(BuildQueue, pkg)
240239
end
241240
end
242241
counter(pkg.name, Messages.install, ok and 'ok' or 'err')
@@ -245,8 +244,7 @@ end
245244

246245
--- @param pkg pack.Package
247246
--- @param counter function
248-
--- @param build_queue table
249-
local function pull(pkg, counter, build_queue)
247+
local function pull(pkg, counter)
250248
local prev_hash = Lock[pkg.name] and Lock[pkg.name].hash or pkg.hash
251249
vim.system(vim.list_extend({ 'git', 'pull' }, Config.pull_args), { cwd = pkg.dir }, function(obj)
252250
if obj.code ~= 0 then
@@ -268,19 +266,18 @@ local function pull(pkg, counter, build_queue)
268266
lock_write()
269267
counter(pkg.name, Messages.update, 'ok')
270268
if pkg.build then
271-
table.insert(build_queue, pkg)
269+
table.insert(BuildQueue, pkg)
272270
end
273271
end)
274272
end
275273

276274
--- @param pkg pack.Package
277275
--- @param counter function
278-
--- @param build_queue table
279-
local function clone_or_pull(pkg, counter, build_queue)
276+
local function clone_or_pull(pkg, counter)
280277
if Filter.to_update(pkg) then
281-
pull(pkg, counter, build_queue)
278+
pull(pkg, counter)
282279
elseif Filter.to_install(pkg) then
283-
clone(pkg, counter, build_queue)
280+
clone(pkg, counter)
284281
end
285282
end
286283

@@ -295,34 +292,41 @@ local function move(src, dst)
295292
end
296293
end
297294

298-
--- @param pkg pack.Package
299-
local function run_build(pkg)
300-
local t = type(pkg.build)
301-
if t == 'function' then
302-
---@diagnostic disable-next-line: param-type-mismatch
303-
local ok = pcall(pkg.build)
304-
report(pkg.name, Messages.build, ok and 'ok' or 'err')
305-
elseif t == 'string' and pkg.build:sub(1, 1) == ':' then
306-
---@diagnostic disable-next-line: param-type-mismatch
307-
local ok = pcall(vim.cmd, pkg.build)
308-
report(pkg.name, Messages.build, ok and 'ok' or 'err')
309-
elseif t == 'string' then
310-
local args = {}
311-
for word in pkg.build:gmatch('%S+') do
312-
table.insert(args, word)
295+
local function process_build_queue()
296+
local failed = {}
297+
298+
local after = function(pkg, ok)
299+
report(pkg.name, Messages.build, ok)
300+
if not ok then
301+
table.insert(failed, pkg)
313302
end
314-
vim.system(
315-
args,
316-
{ cwd = pkg.dir },
317-
vim.schedule_wrap(
318-
function(obj) report(pkg.name, Messages.build, obj.code == 0 and 'ok' or 'err') end
303+
end
304+
305+
for _, pkg in ipairs(BuildQueue) do
306+
local t = type(pkg.build)
307+
if t == 'function' then
308+
---@diagnostic disable-next-line: param-type-mismatch
309+
local ok = pcall(pkg.build)
310+
after(pkg, ok)
311+
elseif t == 'string' and vim.startswith(pkg.build, ':') then
312+
---@diagnostic disable-next-line: param-type-mismatch
313+
local ok = pcall(vim.cmd, pkg.build)
314+
after(pkg, ok)
315+
elseif t == 'string' then
316+
local args = vim.split(pkg.build, '%s', { trimempty = true })
317+
vim.system(
318+
args,
319+
{ cwd = pkg.dir },
320+
vim.schedule_wrap(function(obj) after(pkg, obj.code == 0) end)
319321
)
320-
)
322+
end
321323
end
324+
325+
BuildQueue = failed
322326
end
323327

324328
--- @param pkg pack.Package
325-
local function reclone(pkg, _, build_queue)
329+
local function reclone(pkg)
326330
local ok = pcall(vim.fs.rm, pkg.dir, { recursive = true })
327331
if not ok then
328332
return
@@ -338,17 +342,17 @@ local function reclone(pkg, _, build_queue)
338342
pkg.hash = get_git_hash(pkg.dir)
339343
lock_write()
340344
if pkg.build then
341-
table.insert(build_queue, pkg)
345+
table.insert(BuildQueue, pkg)
342346
end
343347
end
344348
end)
345349
end
346350

347-
local function resolve(pkg, counter, build_queue)
351+
local function resolve(pkg, counter)
348352
if Filter.to_move(pkg) then
349353
move(pkg, Packages[pkg.name])
350354
elseif Filter.to_reclone(pkg) then
351-
reclone(Packages[pkg.name], counter, build_queue)
355+
reclone(Packages[pkg.name], counter)
352356
end
353357
end
354358

@@ -409,27 +413,22 @@ end
409413
--- @param op Operation
410414
--- @param fn function
411415
--- @param pkgs pack.Package[]
412-
--- @param silent boolean?
413-
local function exe_op(op, fn, pkgs, silent)
416+
local function exe_op(op, fn, pkgs)
414417
if vim.tbl_isempty(pkgs) then
415-
if not silent then
416-
vim.notify('Pack: Nothing to ' .. op)
417-
end
418+
vim.notify('Pack: Nothing to ' .. op)
418419

419420
vim.api.nvim_exec_autocmds('User', {
420421
pattern = 'PackDone' .. op:gsub('^%l', string.upper),
421422
})
422423
return
423424
end
424425

425-
local build_queue = {}
426-
427426
local function after(ok, err, nop)
428427
local summary = 'Pack: %s complete. %d ok; %d errors;' .. (nop > 0 and ' %d no-ops' or '')
429428
vim.notify(string.format(summary, op, ok, err, nop))
430429
vim.cmd('silent! helptags ALL')
431-
if #build_queue ~= 0 then
432-
exe_op('build', run_build, build_queue)
430+
if #BuildQueue ~= 0 then
431+
process_build_queue()
433432
end
434433

435434
vim.api.nvim_exec_autocmds('User', {
@@ -443,7 +442,7 @@ local function exe_op(op, fn, pkgs, silent)
443442
local counter = new_counter(#pkgs, after)
444443

445444
for _, pkg in ipairs(pkgs) do
446-
fn(pkg, counter, build_queue)
445+
fn(pkg, counter)
447446
end
448447
end
449448

@@ -505,7 +504,7 @@ function M.register(pkgs)
505504
end
506505

507506
lock_load()
508-
exe_op('resolve', resolve, calculate_diffs(), true)
507+
exe_op('resolve', resolve, calculate_diffs())
509508

510509
for _, pkg in ipairs(find_unlisted()) do
511510
vim.opt.runtimepath:remove(pkg.dir)
@@ -567,17 +566,6 @@ for cmd_name, fn in pairs {
567566
end
568567

569568
do
570-
vim.api.nvim_create_user_command('PackBuild', function(a) run_build(Packages[a.args]) end, {
571-
bar = true,
572-
nargs = 1,
573-
complete = function()
574-
return vim
575-
.iter(Packages)
576-
:map(function(name, pkg) return pkg.build and name or nil end)
577-
:totable()
578-
end,
579-
})
580-
581569
vim.api.nvim_create_user_command('PackList', function()
582570
local installed = {}
583571
local removed = {}

0 commit comments

Comments
 (0)