refactor(install): inline, rename, annotate

This commit is contained in:
Christian Clason 2025-05-31 15:45:59 +02:00 committed by Christian Clason
parent 057e845518
commit f976acdc9c

View file

@ -40,7 +40,7 @@ end
---@async ---@async
---@param path string ---@param path string
local function rmdir(path) local function rmpath(path)
local stat = uv.fs_lstat(path) local stat = uv.fs_lstat(path)
if not stat then if not stat then
return return
@ -48,7 +48,7 @@ local function rmdir(path)
if stat.type == 'directory' then if stat.type == 'directory' then
for file in fs.dir(path) do for file in fs.dir(path) do
rmdir(fs.joinpath(path, file)) rmpath(fs.joinpath(path, file))
end end
return uv_rmdir(path) return uv_rmdir(path)
else else
@ -61,16 +61,16 @@ local INSTALL_TIMEOUT = 60000
--- @async --- @async
--- @param max_jobs integer --- @param max_jobs integer
--- @param task_funs async.TaskFun[] --- @param tasks async.TaskFun[]
local function join(max_jobs, task_funs) local function join(max_jobs, tasks)
if #task_funs == 0 then if #tasks == 0 then
return return
end end
max_jobs = math.min(max_jobs, #task_funs) max_jobs = math.min(max_jobs, #tasks)
local remaining = { select(max_jobs + 1, unpack(task_funs)) } local remaining = { select(max_jobs + 1, unpack(tasks)) }
local to_go = #task_funs local to_go = #tasks
a.await(1, function(finish) a.await(1, function(finish)
local function cb() local function cb()
@ -84,7 +84,7 @@ local function join(max_jobs, task_funs)
end end
for i = 1, max_jobs do for i = 1, max_jobs do
task_funs[i]():await(cb) tasks[i]():await(cb)
end end
end) end)
end end
@ -108,26 +108,6 @@ local function system(cmd, opts)
return r return r
end end
---@async
---@param url string
---@param output string
---@return string? err
local function download_file(url, output)
local r = system({
'curl',
'--silent',
'--fail',
'--show-error',
'-L', -- follow redirects
url,
'--output',
output,
})
if r.code > 0 then
return r.stderr
end
end
local M = {} local M = {}
--- ---
@ -220,7 +200,7 @@ local function do_download(logger, url, project_name, cache_dir, revision, outpu
local tmp = output_dir .. '-tmp' local tmp = output_dir .. '-tmp'
rmdir(tmp) rmpath(tmp)
a.schedule() a.schedule()
url = url:gsub('.git$', '') url = url:gsub('.git$', '')
@ -232,9 +212,18 @@ local function do_download(logger, url, project_name, cache_dir, revision, outpu
do -- Download tarball do -- Download tarball
logger:info('Downloading %s...', project_name) logger:info('Downloading %s...', project_name)
local err = download_file(target, tarball_path) local r = system({
if err then 'curl',
return logger:error('Error during download: %s', err) '--silent',
'--fail',
'--show-error',
'-L', -- follow redirects
target,
'--output',
tarball_path,
})
if r.code > 0 then
return logger:error('Error during download: %s', r.stderr)
end end
end end
@ -280,7 +269,7 @@ local function do_download(logger, url, project_name, cache_dir, revision, outpu
end end
end end
rmdir(tmp) rmpath(tmp)
a.schedule() a.schedule()
end end
@ -343,7 +332,7 @@ end
---@param query_dir string ---@param query_dir string
---@return string? err ---@return string? err
local function do_copy_queries(logger, query_src, query_dir) local function do_copy_queries(logger, query_src, query_dir)
rmdir(query_dir) rmpath(query_dir)
local err = uv_mkdir(query_dir, 493) -- tonumber('755', 8) local err = uv_mkdir(query_dir, 493) -- tonumber('755', 8)
for f in fs.dir(query_src) do for f in fs.dir(query_src) do
@ -374,7 +363,7 @@ local function try_install_lang(lang, cache_dir, install_dir, generate)
compile_location = fs.normalize(repo.path) compile_location = fs.normalize(repo.path)
else else
local project_dir = fs.joinpath(cache_dir, project_name) local project_dir = fs.joinpath(cache_dir, project_name)
rmdir(project_dir) rmpath(project_dir)
revision = revision or repo.branch or 'main' revision = revision or repo.branch or 'main'
@ -443,7 +432,7 @@ local function try_install_lang(lang, cache_dir, install_dir, generate)
-- clean up -- clean up
if repo and not repo.path then if repo and not repo.path then
rmdir(fs.joinpath(cache_dir, project_name)) rmpath(fs.joinpath(cache_dir, project_name))
a.schedule() a.schedule()
end end
@ -517,10 +506,10 @@ local function install(languages, options)
local install_dir = config.get_install_dir('parser') local install_dir = config.get_install_dir('parser')
local task_funs = {} ---@type async.TaskFun[] local tasks = {} ---@type async.TaskFun[]
local done = 0 local done = 0
for _, lang in ipairs(languages) do for _, lang in ipairs(languages) do
task_funs[#task_funs + 1] = a.async(function() tasks[#tasks + 1] = a.async(--[[@async]] function()
a.schedule() a.schedule()
local status = install_lang(lang, cache_dir, install_dir, options.force, options.generate) local status = install_lang(lang, cache_dir, install_dir, options.force, options.generate)
if status ~= 'failed' then if status ~= 'failed' then
@ -529,16 +518,17 @@ local function install(languages, options)
end) end)
end end
join(options and options.max_jobs or MAX_JOBS, task_funs) join(options and options.max_jobs or MAX_JOBS, tasks)
if #task_funs > 1 then if #tasks > 1 then
a.schedule() a.schedule()
if options and options.summary then if options and options.summary then
log.info('Installed %d/%d languages', done, #task_funs) log.info('Installed %d/%d languages', done, #tasks)
end end
end end
return done == #task_funs return done == #tasks
end end
---@async
---@param languages string[]|string ---@param languages string[]|string
---@param options? InstallOptions ---@param options? InstallOptions
M.install = a.async(function(languages, options) M.install = a.async(function(languages, options)
@ -547,6 +537,7 @@ M.install = a.async(function(languages, options)
return install(languages, options) return install(languages, options)
end) end)
---@async
---@param languages? string[]|string ---@param languages? string[]|string
---@param options? InstallOptions ---@param options? InstallOptions
M.update = a.async(function(languages, options) M.update = a.async(function(languages, options)
@ -597,7 +588,7 @@ local function uninstall_lang(logger, lang, parser, queries)
if stat.type == 'link' then if stat.type == 'link' then
qerr = uv_unlink(queries) qerr = uv_unlink(queries)
else else
qerr = rmdir(queries) qerr = rmpath(queries)
end end
a.schedule() a.schedule()
if qerr then if qerr then
@ -608,6 +599,7 @@ local function uninstall_lang(logger, lang, parser, queries)
logger:info('Language uninstalled') logger:info('Language uninstalled')
end end
---@async
---@param languages string[]|string ---@param languages string[]|string
---@param options? InstallOptions ---@param options? InstallOptions
M.uninstall = a.async(function(languages, options) M.uninstall = a.async(function(languages, options)
@ -618,7 +610,7 @@ M.uninstall = a.async(function(languages, options)
local query_dir = config.get_install_dir('queries') local query_dir = config.get_install_dir('queries')
local installed = config.get_installed() local installed = config.get_installed()
local task_funs = {} ---@type async.TaskFun[] local tasks = {} ---@type async.TaskFun[]
local done = 0 local done = 0
for _, lang in ipairs(languages) do for _, lang in ipairs(languages) do
local logger = log.new('uninstall/' .. lang) local logger = log.new('uninstall/' .. lang)
@ -627,7 +619,7 @@ M.uninstall = a.async(function(languages, options)
else else
local parser = fs.joinpath(parser_dir, lang) .. '.so' local parser = fs.joinpath(parser_dir, lang) .. '.so'
local queries = fs.joinpath(query_dir, lang) local queries = fs.joinpath(query_dir, lang)
task_funs[#task_funs + 1] = a.async(function() tasks[#tasks + 1] = a.async(--[[@async]] function()
local err = uninstall_lang(logger, lang, parser, queries) local err = uninstall_lang(logger, lang, parser, queries)
if not err then if not err then
done = done + 1 done = done + 1
@ -636,11 +628,11 @@ M.uninstall = a.async(function(languages, options)
end end
end end
join(MAX_JOBS, task_funs) join(MAX_JOBS, tasks)
if #task_funs > 1 then if #tasks > 1 then
a.schedule() a.schedule()
if options and options.summary then if options and options.summary then
log.info('Uninstalled %d/%d languages', done, #task_funs) log.info('Uninstalled %d/%d languages', done, #tasks)
end end
end end
end) end)