mirror of
https://github.com/chenasraf/nvim-treesitter.git
synced 2026-05-18 01:39:00 +00:00
Add lockfile and make lockfile default install revision
This commit is contained in:
committed by
Kiyan Yazdani
parent
98c12ec23a
commit
0a51d492a5
1
lockfile.json
Normal file
1
lockfile.json
Normal file
@@ -0,0 +1 @@
|
||||
{"jsdoc": {"revision": "77e7785739ad3e90c3de8ed5a55418a5fd2b8225"}, "tsx": {"revision": "07a12bdf024d66d267bd7f96870f8bbbaceaa5d9"}, "html": {"revision": "7f442e1c6163d450c69c75c7a621badc3a0ea98f"}, "typescript": {"revision": "07a12bdf024d66d267bd7f96870f8bbbaceaa5d9"}, "go": {"revision": "34181774b3e86b7801c939c79c7b80a82df91a2b"}, "nix": {"revision": "791b5ff0e4f0da358cbb941788b78d436a2ca621"}, "vue": {"revision": "da1c6e1e3832fc6bbc90431f1fcf6bde1a5d39f2"}, "lua": {"revision": "278b0ea1f1f3d86afc86faaca0cbbf6c01182a54"}, "bash": {"revision": "8ece09ca4c0b5e59b124cd19fa92c76b1a9e9dd4"}, "yaml": {"revision": "b26d567070cfb40cb3138a57cf12a4e2e3c854ef"}, "ocaml": {"revision": "4ec9ee414dadc2b6e7325a9f8124d02f6cd8c250"}, "python": {"revision": "30f538aa1f932a2864c102694761d3127a61dc68"}, "ocaml_interface": {"revision": "4ec9ee414dadc2b6e7325a9f8124d02f6cd8c250"}, "c": {"revision": "99151b1e9293c9e025498fee7e6691e1a52e1d03"}, "haskell": {"revision": "2a0aa1cb5f1b787a4056a29fa0791e87846e33fb"}, "julia": {"revision": "6a0863f1ce3fcf6f99dc0addb7886dcbd27c5a48"}, "elm": {"revision": "327de4eb2d2d04a14e4294d4e93ebc47bae8f691"}, "swift": {"revision": "a22fa5e19bae50098e2252ea96cba3aba43f4c58"}, "toml": {"revision": "42c9ff20c0371bed7f514036e823f10793caacec"}, "java": {"revision": "ee0a2a076785145e350fbd413775d1e003f79315"}, "javascript": {"revision": "3d5493495b62b4ff8e8c24aee7519dd904e25813"}, "fennel": {"revision": "ccfabc8cd3e18a049924fcaea26b05190c7a5ede"}, "css": {"revision": "23f2cb97d47860c517f67f03e1f4b621d5bd2085"}, "scala": {"revision": "211bb726bb5857f872247b600c7c1808e641a8d4"}, "php": {"revision": "b0c0367d4b7058921fdc4ba11e257441a64ab809"}, "query": {"revision": "0ec086c6fcd3528a134eb6172c3a96cf669a355f"}, "markdown": {"revision": "01c3ff6c45366771c78aaebdc58da13b6a8c90d5"}, "c_sharp": {"revision": "8d96d21c5414afff0ed6d6ae96c85a172991d79e"}, "rst": {"revision": "aa61d0e2930de134089326973686ac547cca9e03"}, "json": {"revision": "d3976b27df8622ed17bef6dd5e358b398e73c676"}, "ruby": {"revision": "14a5e56a6fff1f2d40c151ae38b5581fc5c44574"}, "dart": {"revision": "db6935a5e0a05565603112b7ae162acf954656dd"}, "cpp": {"revision": "5e7476bd014445abdae879661e9caf299215478a"}, "rust": {"revision": "40620bf4097cbc9cea79504d7e877865df43a19e"}, "regex": {"revision": "be2e415b5716615530234d179dc27c32b7a1d86b"}}
|
||||
@@ -20,7 +20,8 @@ end
|
||||
|
||||
local config = {
|
||||
modules = {},
|
||||
ensure_installed = {}
|
||||
ensure_installed = {},
|
||||
update_strategy = 'lockfile',
|
||||
}
|
||||
-- List of modules that need to be setup on initialization.
|
||||
local queued_modules_defs = {}
|
||||
@@ -415,4 +416,8 @@ function M.init()
|
||||
end
|
||||
end
|
||||
|
||||
function M.get_update_strategy()
|
||||
return config.update_strategy
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -5,11 +5,42 @@ local luv = vim.loop
|
||||
local utils = require'nvim-treesitter.utils'
|
||||
local parsers = require'nvim-treesitter.parsers'
|
||||
local info = require'nvim-treesitter.info'
|
||||
local configs = require'nvim-treesitter.configs'
|
||||
|
||||
local M = {}
|
||||
local lockfile = {}
|
||||
|
||||
M.compilers = { vim.fn.getenv('CC'), "cc", "gcc", "clang" }
|
||||
|
||||
local function get_revision(lang)
|
||||
if #lockfile == 0 then
|
||||
lockfile = vim.fn.json_decode(vim.fn.readfile(utils.join_paths(utils.get_package_path(), 'lockfile.json')))
|
||||
end
|
||||
return (lockfile[lang] and lockfile[lang].revision)
|
||||
end
|
||||
|
||||
local function 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.iter_cmd(cmd_list, i, lang, success_message)
|
||||
if i == #cmd_list + 1 then return print(success_message) end
|
||||
|
||||
@@ -104,20 +135,83 @@ local function select_install_rm_cmd(cache_folder, project_name)
|
||||
end
|
||||
end
|
||||
|
||||
local function select_mv_cmd(compile_location, parser_lib_name)
|
||||
local function select_mv_cmd(from, to, cwd)
|
||||
if fn.has('win32') == 1 then
|
||||
return {
|
||||
cmd = 'cmd',
|
||||
opts = {
|
||||
args = { '/C', 'move', '/Y', compile_location..'\\parser.so', parser_lib_name },
|
||||
args = { '/C', 'move', '/Y', from, to },
|
||||
cwd = cwd,
|
||||
}
|
||||
}
|
||||
else
|
||||
return {
|
||||
cmd = 'mv',
|
||||
opts = {
|
||||
args = { compile_location..'/parser.so', parser_lib_name }
|
||||
}
|
||||
args = { from, to },
|
||||
cwd = cwd,
|
||||
},
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
local function select_download_commands(repo, project_name, cache_folder, revision)
|
||||
if vim.fn.executable('unzip') == 1 and vim.fn.executable('curl') == 1 and repo.url:find("github.com", 1, true) then
|
||||
|
||||
revision = revision or repo.branch or "master"
|
||||
local path_sep = utils.get_path_sep()
|
||||
return {
|
||||
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
|
||||
repo.url.."/archive/"..revision..".zip",
|
||||
'--output',
|
||||
project_name..".zip"
|
||||
},
|
||||
cwd = cache_folder,
|
||||
},
|
||||
},
|
||||
{
|
||||
cmd = 'unzip',
|
||||
info = 'Extracting...',
|
||||
err = 'Error during unzipping',
|
||||
opts = {
|
||||
args = {
|
||||
'-o',
|
||||
project_name..".zip",
|
||||
'-d',
|
||||
project_name..'-tmp',
|
||||
},
|
||||
cwd = cache_folder,
|
||||
},
|
||||
},
|
||||
select_rm_file_cmd(cache_folder..path_sep..project_name..".zip"),
|
||||
select_mv_cmd(utils.join_paths(project_name..'-tmp', repo.url:match('[^/]-$')..'-'..revision),
|
||||
project_name,
|
||||
cache_folder),
|
||||
select_install_rm_cmd(cache_folder, project_name..'-tmp')
|
||||
}
|
||||
else
|
||||
return {
|
||||
cmd = 'git',
|
||||
info = 'Downloading...',
|
||||
err = 'Error during download, please verify your internet connection',
|
||||
opts = {
|
||||
args = {
|
||||
'clone',
|
||||
'--single-branch',
|
||||
'--branch', repo.branch or 'master',
|
||||
'--depth', '1',
|
||||
repo.url,
|
||||
project_name
|
||||
},
|
||||
cwd = cache_folder,
|
||||
},
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -139,25 +233,12 @@ local function run_install(cache_folder, install_folder, lang, repo, with_sync)
|
||||
..'" are not executable.')
|
||||
return
|
||||
end
|
||||
local revision = configs.get_update_strategy() == 'lockfile' and get_revision(lang)
|
||||
|
||||
local command_list = {
|
||||
select_install_rm_cmd(cache_folder, project_name),
|
||||
{
|
||||
cmd = 'git',
|
||||
info = 'Downloading...',
|
||||
err = 'Error during download, please verify your internet connection',
|
||||
opts = {
|
||||
args = {
|
||||
'clone',
|
||||
'--single-branch',
|
||||
'--branch', repo.branch or 'master',
|
||||
'--depth', '1',
|
||||
repo.url,
|
||||
project_name
|
||||
},
|
||||
cwd = cache_folder,
|
||||
},
|
||||
},
|
||||
local command_list = {}
|
||||
vim.list_extend(command_list, { select_install_rm_cmd(cache_folder, project_name) })
|
||||
vim.list_extend(command_list, select_download_commands(repo, project_name, cache_folder, revision))
|
||||
vim.list_extend(command_list, {
|
||||
{
|
||||
cmd = cc,
|
||||
info = 'Compiling...',
|
||||
@@ -167,9 +248,9 @@ local function run_install(cache_folder, install_folder, lang, repo, with_sync)
|
||||
cwd = compile_location
|
||||
}
|
||||
},
|
||||
select_mv_cmd(compile_location, parser_lib_name),
|
||||
select_mv_cmd('parser.so', parser_lib_name, compile_location),
|
||||
select_install_rm_cmd(cache_folder, project_name)
|
||||
}
|
||||
})
|
||||
|
||||
if with_sync then
|
||||
if iter_cmd_sync(command_list) == true then
|
||||
@@ -245,28 +326,6 @@ function M.update(lang)
|
||||
end
|
||||
end
|
||||
|
||||
local function select_uninstall_rm_cmd(lang, parser_lib)
|
||||
if fn.has('win32') == 1 then
|
||||
return {
|
||||
cmd = 'cmd',
|
||||
opts = {
|
||||
args = { '/C', 'if', 'exist', parser_lib, 'del', parser_lib },
|
||||
},
|
||||
info = "Uninstalling parser for "..lang,
|
||||
err = "Could not delete "..parser_lib,
|
||||
}
|
||||
else
|
||||
return {
|
||||
cmd = 'rm',
|
||||
opts = {
|
||||
args = { parser_lib },
|
||||
},
|
||||
info = "Uninstalling parser for "..lang,
|
||||
err = "Could not delete "..parser_lib,
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
function M.uninstall(lang)
|
||||
local path_sep = '/'
|
||||
if fn.has('win32') == 1 then
|
||||
@@ -285,12 +344,36 @@ function M.uninstall(lang)
|
||||
local parser_lib = install_dir..path_sep..lang..".so"
|
||||
|
||||
local command_list = {
|
||||
select_uninstall_rm_cmd(lang, parser_lib)
|
||||
select_rm_file_cmd(parser_lib, "Uninstalling parser for "..lang)
|
||||
}
|
||||
M.iter_cmd(command_list, 1, lang, 'Treesitter parser for '..lang..' has been uninstalled')
|
||||
end
|
||||
end
|
||||
|
||||
function M.write_lockfile(verbose)
|
||||
local sorted_parsers = {}
|
||||
|
||||
for k, v in pairs(parsers.get_parser_configs()) do
|
||||
table.insert(sorted_parsers, {name = k, parser = v})
|
||||
end
|
||||
|
||||
table.sort(sorted_parsers, function(a, b) return a.name < b.name end)
|
||||
|
||||
for _, v in ipairs(sorted_parsers) do
|
||||
-- I'm sure this can be done in aync way with iter_cmd
|
||||
local sha = vim.split(vim.fn.systemlist('git ls-remote '..v.parser.install_info.url)[1], '\t')[1]
|
||||
lockfile[v.name] = { revision = sha }
|
||||
if verbose then
|
||||
print(v.name..': '..sha)
|
||||
end
|
||||
end
|
||||
|
||||
if verbose then
|
||||
print(vim.inspect(lockfile))
|
||||
end
|
||||
vim.fn.writefile(vim.fn.split(vim.fn.json_encode(lockfile), '\n'), utils.get_package_path().."/lockfile.json")
|
||||
end
|
||||
|
||||
M.ensure_installed = install(false, false)
|
||||
|
||||
M.commands = {
|
||||
|
||||
@@ -21,6 +21,11 @@ function M.get_path_sep()
|
||||
return fn.has('win32') == 1 and '\\' or '/'
|
||||
end
|
||||
|
||||
function M.join_paths(...)
|
||||
local sep = M.get_path_sep()
|
||||
return table.concat({...}, sep)
|
||||
end
|
||||
|
||||
-- Returns a function that joins the given arguments with separator. Arguments
|
||||
-- can't be nil. Example:
|
||||
--[[
|
||||
|
||||
3
scripts/write-lockfile.lua
Normal file
3
scripts/write-lockfile.lua
Normal file
@@ -0,0 +1,3 @@
|
||||
-- Execute as `nvim --headless -c "luafile ./scripts/write-lockfile.lua"`
|
||||
require 'nvim-treesitter.install'.write_lockfile('verbose')
|
||||
vim.cmd('q')
|
||||
Reference in New Issue
Block a user