robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
7.6 KiB272 linesraw
1#!/usr/bin/env -S nvim -l
2
3-- Usage:
4--    ./scripts/bump_deps.lua -h
5
6assert(vim.fn.executable('gh') == 1)
7assert(vim.fn.executable('sed') == 1)
8
9local required_branch_prefix = 'bump-'
10local commit_prefix = 'build(deps): '
11
12local repos = {
13  'luajit/luajit',
14  'libuv/libuv',
15  'luvit/luv',
16  'neovim/unibilium',
17  'juliastrings/utf8proc',
18  'tree-sitter/tree-sitter',
19  'tree-sitter/tree-sitter-c',
20  'tree-sitter-grammars/tree-sitter-diff',
21  'tree-sitter-grammars/tree-sitter-lua',
22  'tree-sitter-grammars/tree-sitter-vim',
23  'neovim/tree-sitter-vimdoc',
24  'tree-sitter-grammars/tree-sitter-query',
25  'tree-sitter-grammars/tree-sitter-markdown',
26  'bytecodealliance/wasmtime',
27  'uncrustify/uncrustify',
28}
29
30local zig_mode = {
31  luajit = false,
32  uncrustify = false,
33  wasmtime = false,
34  libuv = false,
35}
36
37local dependency_table = {} --- @type table<string, string>
38for _, repo in pairs(repos) do
39  dependency_table[vim.fs.basename(repo)] = repo
40end
41
42local function die(msg)
43  print(msg)
44  vim.cmd('cquit 1')
45end
46
47-- Executes and returns the output of `cmd`, or nil on failure.
48-- if die_on_fail is true, process dies with die_msg on failure
49local function _run(cmd, die_on_fail, die_msg)
50  local rv = vim.system(cmd):wait()
51  if rv.code ~= 0 then
52    if die_on_fail then
53      die(die_msg)
54    end
55    return nil
56  end
57  return vim.trim(rv.stdout)
58end
59
60-- Run a command, return nil on failure
61local function run(cmd)
62  return _run(cmd, false, '')
63end
64
65-- Run a command, die on failure with err_msg
66local function run_die(cmd, err_msg)
67  -- print(vim.inspect(table.concat(cmd, ' ')))
68  return _run(cmd, true, err_msg)
69end
70
71local nvim_src_dir = run({ 'git', 'rev-parse', '--show-toplevel' })
72local deps_file = nvim_src_dir .. '/' .. 'cmake.deps/deps.txt'
73
74--- @param repo string
75--- @param ref string
76local function get_archive_info(repo, ref)
77  local temp_dir = os.getenv('TMPDIR') or os.getenv('TEMP') or '/tmp'
78
79  local archive_name = ref .. '.tar.gz'
80  local archive_path = vim.fs.joinpath(temp_dir, archive_name)
81  local archive_url = ('https://github.com/%s/archive/%s'):format(repo, archive_name)
82  print(vim.inspect(archive_path), vim.inspect(archive_url))
83
84  run_die(
85    { 'curl', '-sfL', archive_url, '-o', archive_path },
86    'Failed to download archive from GitHub'
87  )
88
89  local shacmd = (
90    vim.fn.executable('sha256sum') == 1 and { 'sha256sum', archive_path }
91    or { 'shasum', '-a', '256', archive_path }
92  )
93  local archive_sha = run_die(shacmd):gmatch('%w+')()
94  return { url = archive_url, sha = archive_sha }
95end
96
97--- @param repo string
98--- @param ref string
99--- @param filename string
100local function get_release_artifact_info(repo, ref, filename)
101  local temp_dir = os.getenv('TMPDIR') or os.getenv('TEMP') or '/tmp'
102
103  local artifact_path = vim.fs.joinpath(temp_dir, filename)
104  local artifact_url = ('https://github.com/%s/releases/download/%s/%s'):format(repo, ref, filename)
105
106  run_die(
107    { 'curl', '-sfL', artifact_url, '-o', artifact_path },
108    'Failed to download release artifact from GitHub'
109  )
110
111  local shacmd = (
112    vim.fn.executable('sha256sum') == 1 and { 'sha256sum', artifact_path }
113    or { 'shasum', '-a', '256', artifact_path }
114  )
115  local artifact_sha = run_die(shacmd):gmatch('%w+')()
116  return { url = artifact_url, sha = artifact_sha }
117end
118
119local function get_gh_commit_sha(repo, ref)
120  local full_repo = string.format('https://github.com/%s.git', repo)
121  local tag_exists = run_die({ 'git', 'ls-remote', full_repo, 'refs/tags/' .. ref }) ~= ''
122  -- We'd rather use the git tag over commit sha if possible
123  if tag_exists then
124    return ref
125  end
126
127  local sha = assert(
128    run_die(
129      { 'gh', 'api', 'repos/' .. repo .. '/commits/' .. ref, '--jq', '.sha' },
130      'Failed to get commit hash from GitHub. Not a valid ref?'
131    )
132  )
133  return sha
134end
135
136local function update_deps_file(symbol, kind, value)
137  run_die({
138    'sed',
139    '-i',
140    '-e',
141    's/' .. symbol .. '_' .. kind .. '.*$' .. '/' .. symbol .. '_' .. kind .. ' ' .. value .. '/',
142    deps_file,
143  }, 'Failed to write ' .. deps_file)
144end
145
146local function ref(name, _ref)
147  local repo = dependency_table[name]
148  local symbol = string.gsub(name, 'tree%-sitter', 'treesitter'):gsub('%-', '_')
149  local symbol_upper = symbol:upper()
150
151  run_die(
152    { 'git', 'diff', '--quiet', 'HEAD', '--', deps_file },
153    deps_file .. ' has uncommitted changes'
154  )
155
156  _ref = get_gh_commit_sha(repo, _ref)
157
158  local archive = get_archive_info(repo, _ref)
159  local comment = string.sub(_ref, 1, 9)
160
161  local checked_out_branch = assert(run({ 'git', 'rev-parse', '--abbrev-ref', 'HEAD' }))
162  if not checked_out_branch:match('^' .. required_branch_prefix) then
163    print(
164      "Current branch '"
165        .. checked_out_branch
166        .. "' doesn't seem to start with "
167        .. required_branch_prefix
168    )
169    print('Checking out to bump-' .. name)
170    run_die({ 'git', 'checkout', '-b', 'bump-' .. name }, 'git failed to create branch')
171  end
172
173  print('Updating ' .. name .. ' to ' .. archive.url .. '\n')
174  update_deps_file(symbol_upper, 'URL', archive.url:gsub('/', '\\/'))
175  update_deps_file(symbol_upper, 'SHA256', archive.sha)
176
177  -- Keep wasm version in sync with native version
178  if name == 'tree-sitter-lua' then
179    local wasm = get_release_artifact_info(repo, _ref, 'tree-sitter-lua.wasm')
180    print('Updating ' .. name .. ' WASM to ' .. wasm.url .. '\n')
181    update_deps_file(symbol_upper .. '_WASM', 'URL', wasm.url:gsub('/', '\\/'))
182    update_deps_file(symbol_upper .. '_WASM', 'SHA256', wasm.sha)
183  end
184
185  run_die({ 'git', 'add', deps_file })
186
187  local zig = zig_mode[symbol]
188  if zig ~= false then
189    if zig == 'nested' then
190      -- don't care about un-cding, "git commit" doesn't care and then we die
191      vim.fn.chdir('deps/' .. symbol)
192    end
193    -- note: get_gh_commit_sha is likely superfluous with zig. but use the same resolved hash, for consistency.
194    run_die({
195      'zig',
196      'fetch',
197      '--save=' .. symbol,
198      'git+https://github.com/' .. repo .. '#' .. _ref,
199    })
200    run_die({ 'git', 'add', 'build.zig.zon' })
201  end
202
203  run_die({
204    'git',
205    'commit',
206    '-m',
207    commit_prefix .. 'bump ' .. name .. ' to ' .. comment,
208  }, 'git failed to commit')
209end
210
211local function usage()
212  local this_script = tostring(vim.fs.basename(_G.arg[0]))
213  local script_exe = './' .. this_script
214  local help = ([=[
215    Bump Nvim dependencies
216
217    Usage:  %s [options]
218        Bump to HEAD, tagged version or commit:
219            %s luv --head
220            %s luv --ref 1.43.0-0
221            %s luv --ref abc123
222
223    Options:
224        -h, --help            show this message and exit.
225        --list                list all dependencies
226
227    Dependency Options:
228        --ref <ref>           bump to a specific commit or tag.
229        --head                bump to a current head.
230  ]=]):format(script_exe, script_exe, script_exe, script_exe)
231  print(help)
232end
233
234local function list_deps()
235  local l = 'Dependencies:\n'
236  for k in vim.spairs(dependency_table) do
237    l = string.format('%s\n%s%s', l, string.rep(' ', 2), k)
238  end
239  print(l)
240end
241
242do
243  local args = {}
244  local i = 1
245  while i <= #_G.arg do
246    if _G.arg[i] == '-h' or _G.arg[i] == '--help' then
247      args.h = true
248    elseif _G.arg[i] == '--list' then
249      args.list = true
250    elseif _G.arg[i] == '--ref' then
251      args.ref = _G.arg[i + 1]
252      i = i + 1
253    elseif _G.arg[i] == '--head' then
254      args.ref = 'HEAD'
255    elseif vim.startswith(_G.arg[i], '--') then
256      die(string.format('Invalid argument %s\n', _G.arg[i]))
257    else
258      args.dep = _G.arg[i]
259    end
260    i = i + 1
261  end
262
263  if args.h then
264    usage()
265  elseif args.list then
266    list_deps()
267  elseif args.ref then
268    ref(args.dep, args.ref)
269  else
270    die('missing required arg\n')
271  end
272end