update installer with sync and some fixes

- add sync method for installing using `system`
- remove `descriptions` in command configs
- use install(lang) in ensure_installed and make it compatible
This commit is contained in:
kiyan42 2020-06-29 16:07:16 +02:00
parent b184f1cafb
commit d84c03cb1b
3 changed files with 92 additions and 65 deletions

View file

@ -129,32 +129,28 @@ M.commands = {
args = { args = {
"-nargs=1", "-nargs=1",
"-complete=custom,v:lua.ts_available_modules" "-complete=custom,v:lua.ts_available_modules"
}, }
description = '`:TSBufEnable module_name` enable a specified module on the current buffer'
}, },
TSBufDisable = { TSBufDisable = {
run = disable_module, run = disable_module,
args = { args = {
"-nargs=1", "-nargs=1",
"-complete=custom,v:lua.ts_available_modules" "-complete=custom,v:lua.ts_available_modules"
}, }
description = '`:TSBufDisable module_name` disable a specified module on the current buffer'
}, },
TSEnableAll = { TSEnableAll = {
run = enable_all, run = enable_all,
args = { args = {
"-nargs=+", "-nargs=+",
"-complete=custom,v:lua.ts_available_modules" "-complete=custom,v:lua.ts_available_modules"
}, }
description = '`:TSEnableAll module_name (filetype)` enables a specified module on all buffers. If filetype is specified, enable only for specified filetype'
}, },
TSDisableAll = { TSDisableAll = {
run = disable_all, run = disable_all,
args = { args = {
"-nargs=+", "-nargs=+",
"-complete=custom,v:lua.ts_available_modules" "-complete=custom,v:lua.ts_available_modules"
}, }
description = '`:TSDisableAll module_name (filetype)` disables a specified module on all buffers. If filetype is specified, disable only for specified filetype'
}, },
} }

View file

@ -81,16 +81,14 @@ M.commands = {
run = install_info, run = install_info,
args = { args = {
"-nargs=0", "-nargs=0",
}, }
description = '`:TSInstallInfo` print installation state for every filetype'
}, },
TSModuleInfo = { TSModuleInfo = {
run = module_info, run = module_info,
args = { args = {
"-nargs=?", "-nargs=?",
"-complete=custom,v:lua.ts_available_modules" "-complete=custom,v:lua.ts_available_modules"
}, }
description = '`:TSModuleInfo` print module state for every filetype, if module is specified, only for current module'
} }
} }

View file

@ -22,7 +22,42 @@ local function iter_cmd(cmd_list, i, lang)
end)) end))
end end
local function run_install(cache_folder, package_path, lang, repo) local function get_command(cmd)
local ret = ''
if cmd.opts and cmd.opts.cwd then
ret = string.format('cd %s;\n', cmd.opts.cwd)
end
ret = string.format('%s%s ', ret, cmd.cmd)
local options = ""
if cmd.opts and cmd.opts.args then
for _, opt in ipairs(cmd.opts.args) do
options = string.format("%s %s", options, opt)
end
end
return string.format('%s%s', ret, options)
end
local function iter_cmd_sync(cmd_list)
for _, cmd in ipairs(cmd_list) do
if cmd.info then
print(cmd.info)
end
vim.fn.system(get_command(cmd))
if vim.v.shell_error ~= 0 then
api.nvim_err_writeln(cmd.err)
return false
end
end
return true
end
local function run_install(cache_folder, package_path, lang, repo, with_sync)
local project_name = 'tree-sitter-'..lang local project_name = 'tree-sitter-'..lang
local project_repo = cache_folder..'/'..project_name local project_repo = cache_folder..'/'..project_name
-- compile_location only needed for typescript installs. -- compile_location only needed for typescript installs.
@ -33,7 +68,7 @@ local function run_install(cache_folder, package_path, lang, repo)
cmd = 'rm', cmd = 'rm',
opts = { opts = {
args = { '-rf', project_repo }, args = { '-rf', project_repo },
} },
}, },
{ {
cmd = 'git', cmd = 'git',
@ -76,83 +111,81 @@ local function run_install(cache_folder, package_path, lang, repo)
} }
} }
iter_cmd(command_list, 1, lang) if with_sync then
if iter_cmd_sync(command_list, lang) == true then
print('Treesitter parser for '..lang..' has been installed')
end
else
iter_cmd(command_list, 1, lang)
end
end end
-- TODO(kyazdani): this should work on windows too -- TODO(kyazdani): this should work on windows too
local function install(...) local function install(with_sync)
if fn.has('win32') == 1 then return function (...)
return api.nvim_err_writeln('This command is not available on windows at the moment.') if fn.has('win32') == 1 then
end return api.nvim_err_writeln('This command is not available on windows at the moment.')
end
if fn.executable('git') == 0 then if fn.executable('git') == 0 then
return api.nvim_err_writeln('Git is required on your system to run this command') return api.nvim_err_writeln('Git is required on your system to run this command')
end end
local package_path, err = utils.get_package_path() local package_path, err = utils.get_package_path()
if err then return api.nvim_err_writeln(err) end if err then return api.nvim_err_writeln(err) end
local cache_folder, err = utils.get_cache_dir() local cache_folder, err = utils.get_cache_dir()
if err then return api.nvim_err_writeln(err) end if err then return api.nvim_err_writeln(err) end
local languages = { ... } local languages = vim.tbl_flatten({...})
local check_installed = true local ask_reinstall = true
if ... == 'all' then if ... == 'all' then
languages = parsers.available_parsers() languages = parsers.available_parsers()
check_installed = false ask_reinstall = false
end end
for _, lang in ipairs(languages) do for _, lang in ipairs(languages) do
if check_installed then
if #api.nvim_get_runtime_file('parser/'..lang..'.so', false) > 0 then if #api.nvim_get_runtime_file('parser/'..lang..'.so', false) > 0 then
if not ask_reinstall then goto continue end
local yesno = fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ') local yesno = fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ')
print('\n ') -- mandatory to avoid messing up command line print('\n ') -- mandatory to avoid messing up command line
if not string.match(yesno, '^y.*') then goto continue end if not string.match(yesno, '^y.*') then goto continue end
end end
end
local parser_config = parsers.get_parser_configs()[lang] local parser_config = parsers.get_parser_configs()[lang]
if not parser_config then if not parser_config then
return api.nvim_err_writeln('Parser not available for language '..lang) return api.nvim_err_writeln('Parser not available for language '..lang)
end end
local install_info = parser_config.install_info local install_info = parser_config.install_info
vim.validate { vim.validate {
url={ install_info.url, 'string' }, url={ install_info.url, 'string' },
files={ install_info.files, 'table' } files={ install_info.files, 'table' }
} }
run_install(cache_folder, package_path, lang, install_info) run_install(cache_folder, package_path, lang, install_info, with_sync)
::continue:: ::continue::
end
end
M.ensure_installed = function(languages)
if type(languages) == 'string' then
if languages == 'all' then
languages = parsers.available_parsers()
else
languages = {languages}
end
end
for _, lang in ipairs(languages) do
if not parsers.has_parser(lang) then
install(lang)
end end
end end
end end
M.ensure_installed = install(false)
M.commands = { M.commands = {
TSInstall = { TSInstall = {
run = install, run = install(false),
args = { args = {
"-nargs=+", "-nargs=+",
"-complete=custom,v:lua.ts_installable_parsers" "-complete=custom,v:lua.ts_installable_parsers"
}, }
description = '`:TSInstall {lang}` installs a parser under nvim-treesitter/parser/{lang}.so' },
TSInstallSync = {
run = install(true),
args = {
"-nargs=+",
"-complete=custom,v:lua.ts_installable_parsers"
}
} }
} }