2020-10-06 02:22:31 +05:30
|
|
|
local fn = vim.fn
|
|
|
|
|
local utils = require'nvim-treesitter.utils'
|
|
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
|
|
function M.select_mkdir_cmd(directory, cwd, info_msg)
|
|
|
|
|
if fn.has('win32') == 1 then
|
|
|
|
|
return {
|
|
|
|
|
cmd = 'cmd',
|
|
|
|
|
opts = {
|
|
|
|
|
args = { '/C', 'mkdir', directory},
|
2021-02-22 23:01:12 +01:00
|
|
|
cwd = cwd,
|
2020-10-06 02:22:31 +05:30
|
|
|
},
|
|
|
|
|
info = info_msg,
|
|
|
|
|
err = "Could not create "..directory,
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return {
|
|
|
|
|
cmd = 'mkdir',
|
|
|
|
|
opts = {
|
|
|
|
|
args = { directory },
|
2021-02-22 23:01:12 +01:00
|
|
|
cwd = cwd,
|
2020-10-06 02:22:31 +05:30
|
|
|
},
|
|
|
|
|
info = info_msg,
|
|
|
|
|
err = "Could not create "..directory,
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.select_rm_file_cmd(file, info_msg)
|
|
|
|
|
if fn.has('win32') == 1 then
|
|
|
|
|
return {
|
|
|
|
|
cmd = 'cmd',
|
|
|
|
|
opts = {
|
|
|
|
|
args = { '/C', 'if', 'exist', file, 'del', file },
|
|
|
|
|
},
|
|
|
|
|
info = info_msg,
|
|
|
|
|
err = "Could not delete "..file,
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return {
|
|
|
|
|
cmd = 'rm',
|
|
|
|
|
opts = {
|
|
|
|
|
args = { file },
|
|
|
|
|
},
|
|
|
|
|
info = info_msg,
|
|
|
|
|
err = "Could not delete "..file,
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.select_executable(executables)
|
2020-12-12 10:30:15 +01:00
|
|
|
return vim.tbl_filter(function(c) return c ~= vim.NIL and fn.executable(c) == 1 end, executables)[1]
|
2020-10-06 02:22:31 +05:30
|
|
|
end
|
|
|
|
|
|
2021-02-26 20:41:05 +01:00
|
|
|
function M.select_compiler_args(repo, compiler)
|
|
|
|
|
if (string.match(compiler, 'cl$') or string.match(compiler, 'cl.exe$')) then
|
|
|
|
|
return {
|
|
|
|
|
'/Fe:',
|
|
|
|
|
'parser.so',
|
|
|
|
|
'/Isrc',
|
|
|
|
|
repo.files,
|
|
|
|
|
'-Os',
|
|
|
|
|
'/LD',
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
local args = {
|
|
|
|
|
'-o',
|
|
|
|
|
'parser.so',
|
|
|
|
|
'-I./src',
|
|
|
|
|
repo.files,
|
|
|
|
|
'-shared',
|
|
|
|
|
'-Os',
|
|
|
|
|
'-lstdc++',
|
|
|
|
|
}
|
|
|
|
|
if fn.has('win32') == 0 then
|
|
|
|
|
table.insert(args, '-fPIC')
|
|
|
|
|
end
|
|
|
|
|
return args
|
2020-10-06 02:22:31 +05:30
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.select_install_rm_cmd(cache_folder, project_name)
|
|
|
|
|
if fn.has('win32') == 1 then
|
|
|
|
|
local dir = cache_folder ..'\\'.. project_name
|
|
|
|
|
return {
|
|
|
|
|
cmd = 'cmd',
|
|
|
|
|
opts = {
|
|
|
|
|
args = { '/C', 'if', 'exist', dir, 'rmdir', '/s', '/q', dir },
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return {
|
|
|
|
|
cmd = 'rm',
|
|
|
|
|
opts = {
|
|
|
|
|
args = { '-rf', cache_folder..'/'..project_name },
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.select_mv_cmd(from, to, cwd)
|
|
|
|
|
if fn.has('win32') == 1 then
|
|
|
|
|
return {
|
|
|
|
|
cmd = 'cmd',
|
|
|
|
|
opts = {
|
|
|
|
|
args = { '/C', 'move', '/Y', from, to },
|
|
|
|
|
cwd = cwd,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return {
|
|
|
|
|
cmd = 'mv',
|
|
|
|
|
opts = {
|
|
|
|
|
args = { from, to },
|
|
|
|
|
cwd = cwd,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.select_download_commands(repo, project_name, cache_folder, revision)
|
2021-03-01 17:18:51 +01:00
|
|
|
|
2021-04-15 08:56:01 +02:00
|
|
|
local can_use_tar = vim.fn.executable('tar') == 1 and vim.fn.executable('curl') == 1
|
2021-04-15 10:49:25 +02:00
|
|
|
local is_github = repo.url:find("github.com", 1, true)
|
|
|
|
|
local is_gitlab = repo.url:find("gitlab.com", 1, true)
|
|
|
|
|
|
2021-01-04 13:28:26 -05:00
|
|
|
local is_windows = fn.has('win32') == 1
|
|
|
|
|
|
|
|
|
|
revision = revision or repo.branch or "master"
|
2021-03-01 17:18:51 +01:00
|
|
|
|
2021-04-15 10:49:25 +02:00
|
|
|
if can_use_tar and (is_github or is_gitlab) and not is_windows then
|
2020-10-06 02:22:31 +05:30
|
|
|
|
|
|
|
|
local path_sep = utils.get_path_sep()
|
2021-03-02 17:44:20 +01:00
|
|
|
local url = repo.url:gsub('.git$', '')
|
2021-03-01 17:18:51 +01:00
|
|
|
|
2020-10-06 02:22:31 +05:30
|
|
|
return {
|
|
|
|
|
M.select_install_rm_cmd(cache_folder, project_name..'-tmp'),
|
|
|
|
|
{
|
|
|
|
|
cmd = 'curl',
|
|
|
|
|
info = 'Downloading...',
|
|
|
|
|
err = 'Error during download, please verify your internet connection',
|
|
|
|
|
opts = {
|
|
|
|
|
args = {
|
|
|
|
|
'-L', -- follow redirects
|
2021-04-15 10:49:25 +02:00
|
|
|
is_github and url.."/archive/"..revision..".tar.gz"
|
2021-03-02 17:44:20 +01:00
|
|
|
or url.."/-/archive/"..revision.."/"..project_name.."-"..revision..".tar.gz",
|
2020-10-06 02:22:31 +05:30
|
|
|
'--output',
|
|
|
|
|
project_name..".tar.gz"
|
|
|
|
|
},
|
|
|
|
|
cwd = cache_folder,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
M.select_mkdir_cmd(project_name..'-tmp', cache_folder, 'Creating temporary directory'),
|
|
|
|
|
{
|
|
|
|
|
cmd = 'tar',
|
|
|
|
|
info = 'Extracting...',
|
|
|
|
|
err = 'Error during tarball extraction.',
|
|
|
|
|
opts = {
|
|
|
|
|
args = {
|
|
|
|
|
'-xvf',
|
|
|
|
|
project_name..".tar.gz",
|
|
|
|
|
'-C',
|
|
|
|
|
project_name..'-tmp',
|
|
|
|
|
},
|
|
|
|
|
cwd = cache_folder,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
M.select_rm_file_cmd(cache_folder..path_sep..project_name..".tar.gz"),
|
2021-03-02 17:44:20 +01:00
|
|
|
M.select_mv_cmd(utils.join_path(project_name..'-tmp', url:match('[^/]-$')..'-'..revision),
|
2021-02-22 23:01:12 +01:00
|
|
|
project_name,
|
|
|
|
|
cache_folder),
|
2020-10-06 02:22:31 +05:30
|
|
|
M.select_install_rm_cmd(cache_folder, project_name..'-tmp')
|
|
|
|
|
}
|
|
|
|
|
else
|
2021-01-21 21:11:43 -05:00
|
|
|
local git_folder = utils.join_path(cache_folder, project_name)
|
2021-01-04 13:28:26 -05:00
|
|
|
local clone_error = 'Error during download, please verify your internet connection'
|
|
|
|
|
|
2020-10-06 02:22:31 +05:30
|
|
|
return {
|
|
|
|
|
{
|
|
|
|
|
cmd = 'git',
|
|
|
|
|
info = 'Downloading...',
|
2021-01-04 13:28:26 -05:00
|
|
|
err = clone_error,
|
2020-10-06 02:22:31 +05:30
|
|
|
opts = {
|
|
|
|
|
args = {
|
|
|
|
|
'clone',
|
|
|
|
|
repo.url,
|
|
|
|
|
project_name
|
|
|
|
|
},
|
|
|
|
|
cwd = cache_folder,
|
|
|
|
|
},
|
2021-01-04 13:28:26 -05:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
cmd = 'git',
|
|
|
|
|
info = 'Checking out locked revision',
|
|
|
|
|
err = 'Error while checking out revision',
|
|
|
|
|
opts = {
|
|
|
|
|
args = {
|
2021-04-15 10:15:47 +02:00
|
|
|
'checkout', revision,
|
2021-01-04 13:28:26 -05:00
|
|
|
},
|
|
|
|
|
cwd = git_folder
|
|
|
|
|
}
|
2020-10-06 02:22:31 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-26 20:41:05 +01:00
|
|
|
function M.make_directory_change_for_command(dir, command)
|
|
|
|
|
if fn.has('win32') == 1 then
|
|
|
|
|
return string.format("pushd %s & %s & popd", dir, command)
|
|
|
|
|
else
|
|
|
|
|
return string.format("cd %s;\n %s", dir, command)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-10-06 02:22:31 +05:30
|
|
|
return M
|