bump_deps.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. #!/usr/bin/env -S nvim -l
  2. -- Usage:
  3. -- ./scripts/bump_deps.lua -h
  4. local M = {}
  5. local _trace = false
  6. local required_branch_prefix = 'bump-'
  7. local commit_prefix = 'build(deps): '
  8. -- Print message
  9. local function p(s)
  10. vim.cmd('set verbose=1')
  11. vim.api.nvim_echo({ { s, '' } }, false, {})
  12. vim.cmd('set verbose=0')
  13. end
  14. local function die()
  15. p('')
  16. vim.cmd('cquit 1')
  17. end
  18. -- Executes and returns the output of `cmd`, or nil on failure.
  19. -- if die_on_fail is true, process dies with die_msg on failure
  20. --
  21. -- Prints `cmd` if `trace` is enabled.
  22. local function _run(cmd, die_on_fail, die_msg)
  23. if _trace then
  24. p('run: ' .. vim.inspect(cmd))
  25. end
  26. local rv = vim.trim(vim.fn.system(cmd)) or ''
  27. if vim.v.shell_error ~= 0 then
  28. if die_on_fail then
  29. if _trace then
  30. p(rv)
  31. end
  32. p(die_msg)
  33. die()
  34. end
  35. return nil
  36. end
  37. return rv
  38. end
  39. -- Run a command, return nil on failure
  40. local function run(cmd)
  41. return _run(cmd, false, '')
  42. end
  43. -- Run a command, die on failure with err_msg
  44. local function run_die(cmd, err_msg)
  45. return _run(cmd, true, err_msg)
  46. end
  47. local function require_executable(cmd)
  48. local cmd_path = run_die({ 'sh', '-c', 'command -v ' .. cmd }, cmd .. ' not found!')
  49. run_die({ 'test', '-x', cmd_path }, cmd .. ' is not executable')
  50. end
  51. local function rm_file_if_present(path_to_file)
  52. run({ 'rm', '-f', path_to_file })
  53. end
  54. local nvim_src_dir = vim.fn.getcwd()
  55. local deps_file = nvim_src_dir .. '/' .. 'cmake.deps/deps.txt'
  56. local temp_dir = nvim_src_dir .. '/tmp'
  57. run({ 'mkdir', '-p', temp_dir })
  58. local function get_dependency(dependency_name)
  59. local dependency_table = {
  60. ['luajit'] = {
  61. repo = 'LuaJIT/LuaJIT',
  62. symbol = 'LUAJIT',
  63. },
  64. ['libuv'] = {
  65. repo = 'libuv/libuv',
  66. symbol = 'LIBUV',
  67. },
  68. ['luv'] = {
  69. repo = 'luvit/luv',
  70. symbol = 'LUV',
  71. },
  72. ['unibilium'] = {
  73. repo = 'neovim/unibilium',
  74. symbol = 'UNIBILIUM',
  75. },
  76. ['utf8proc'] = {
  77. repo = 'JuliaStrings/utf8proc',
  78. symbol = 'UTF8PROC',
  79. },
  80. ['tree-sitter'] = {
  81. repo = 'tree-sitter/tree-sitter',
  82. symbol = 'TREESITTER',
  83. },
  84. ['tree-sitter-c'] = {
  85. repo = 'tree-sitter/tree-sitter-c',
  86. symbol = 'TREESITTER_C',
  87. },
  88. ['tree-sitter-lua'] = {
  89. repo = 'tree-sitter-grammars/tree-sitter-lua',
  90. symbol = 'TREESITTER_LUA',
  91. },
  92. ['tree-sitter-vim'] = {
  93. repo = 'tree-sitter-grammars/tree-sitter-vim',
  94. symbol = 'TREESITTER_VIM',
  95. },
  96. ['tree-sitter-vimdoc'] = {
  97. repo = 'neovim/tree-sitter-vimdoc',
  98. symbol = 'TREESITTER_VIMDOC',
  99. },
  100. ['tree-sitter-query'] = {
  101. repo = 'tree-sitter-grammars/tree-sitter-query',
  102. symbol = 'TREESITTER_QUERY',
  103. },
  104. ['tree-sitter-markdown'] = {
  105. repo = 'tree-sitter-grammars/tree-sitter-markdown',
  106. symbol = 'TREESITTER_MARKDOWN',
  107. },
  108. ['wasmtime'] = {
  109. repo = 'bytecodealliance/wasmtime',
  110. symbol = 'WASMTIME',
  111. },
  112. ['uncrustify'] = {
  113. repo = 'uncrustify/uncrustify',
  114. symbol = 'UNCRUSTIFY',
  115. },
  116. }
  117. local dependency = dependency_table[dependency_name]
  118. if dependency == nil then
  119. p('Not a dependency: ' .. dependency_name)
  120. die()
  121. end
  122. dependency.name = dependency_name
  123. return dependency
  124. end
  125. local function get_gh_commit_sha(repo, ref)
  126. require_executable('gh')
  127. local sha = run_die(
  128. { 'gh', 'api', 'repos/' .. repo .. '/commits/' .. ref, '--jq', '.sha' },
  129. 'Failed to get commit hash from GitHub. Not a valid ref?'
  130. )
  131. return sha
  132. end
  133. local function get_archive_info(repo, ref)
  134. require_executable('curl')
  135. local archive_name = ref .. '.tar.gz'
  136. local archive_path = temp_dir .. '/' .. archive_name
  137. local archive_url = 'https://github.com/' .. repo .. '/archive/' .. archive_name
  138. rm_file_if_present(archive_path)
  139. run_die(
  140. { 'curl', '-sL', archive_url, '-o', archive_path },
  141. 'Failed to download archive from GitHub'
  142. )
  143. local shacmd = (
  144. vim.fn.executable('sha256sum') == 1 and { 'sha256sum', archive_path }
  145. or { 'shasum', '-a', '256', archive_path }
  146. )
  147. local archive_sha = run(shacmd):gmatch('%w+')()
  148. return { url = archive_url, sha = archive_sha }
  149. end
  150. local function write_cmakelists_line(symbol, kind, value)
  151. require_executable('sed')
  152. run_die({
  153. 'sed',
  154. '-i',
  155. '-e',
  156. 's/' .. symbol .. '_' .. kind .. '.*$' .. '/' .. symbol .. '_' .. kind .. ' ' .. value .. '/',
  157. deps_file,
  158. }, 'Failed to write ' .. deps_file)
  159. end
  160. local function explicit_create_branch(dep)
  161. require_executable('git')
  162. local checked_out_branch = run({ 'git', 'rev-parse', '--abbrev-ref', 'HEAD' })
  163. if checked_out_branch ~= 'master' then
  164. p('Not on master!')
  165. die()
  166. end
  167. run_die({ 'git', 'checkout', '-b', 'bump-' .. dep }, 'git failed to create branch')
  168. end
  169. local function verify_branch(new_branch_suffix)
  170. require_executable('git')
  171. local checked_out_branch = assert(run({ 'git', 'rev-parse', '--abbrev-ref', 'HEAD' }))
  172. if not checked_out_branch:match('^' .. required_branch_prefix) then
  173. p(
  174. "Current branch '"
  175. .. checked_out_branch
  176. .. "' doesn't seem to start with "
  177. .. required_branch_prefix
  178. )
  179. p('Checking out to bump-' .. new_branch_suffix)
  180. explicit_create_branch(new_branch_suffix)
  181. end
  182. end
  183. local function update_cmakelists(dependency, archive, comment)
  184. require_executable('git')
  185. verify_branch(dependency.name)
  186. p('Updating ' .. dependency.name .. ' to ' .. archive.url .. '\n')
  187. write_cmakelists_line(dependency.symbol, 'URL', archive.url:gsub('/', '\\/'))
  188. write_cmakelists_line(dependency.symbol, 'SHA256', archive.sha)
  189. run_die({
  190. 'git',
  191. 'commit',
  192. deps_file,
  193. '-m',
  194. commit_prefix .. 'bump ' .. dependency.name .. ' to ' .. comment,
  195. }, 'git failed to commit')
  196. end
  197. local function verify_cmakelists_committed()
  198. require_executable('git')
  199. run_die(
  200. { 'git', 'diff', '--quiet', 'HEAD', '--', deps_file },
  201. deps_file .. ' has uncommitted changes'
  202. )
  203. end
  204. local function warn_luv_symbol()
  205. p('warning: ' .. get_dependency('Luv').symbol .. '_VERSION will not be updated')
  206. end
  207. -- return first 9 chars of commit
  208. local function short_commit(commit)
  209. return string.sub(commit, 1, 9)
  210. end
  211. -- TODO: remove hardcoded fork
  212. local function gh_pr(pr_title, pr_body)
  213. require_executable('gh')
  214. local pr_url = run_die({
  215. 'gh',
  216. 'pr',
  217. 'create',
  218. '--title',
  219. pr_title,
  220. '--body',
  221. pr_body,
  222. }, 'Failed to create PR')
  223. return pr_url
  224. end
  225. local function find_git_remote(fork)
  226. require_executable('git')
  227. local remotes = assert(run({ 'git', 'remote', '-v' }))
  228. local git_remote = ''
  229. for remote in remotes:gmatch('[^\r\n]+') do
  230. local words = {}
  231. for word in remote:gmatch('%w+') do
  232. table.insert(words, word)
  233. end
  234. local match = words[1]:match('/github.com[:/]neovim/neovim/')
  235. if fork == 'fork' then
  236. match = not match
  237. end
  238. if match and words[3] == '(fetch)' then
  239. git_remote = words[0]
  240. break
  241. end
  242. end
  243. if git_remote == '' then
  244. git_remote = 'origin'
  245. end
  246. return git_remote
  247. end
  248. local function create_pr(pr_title, pr_body)
  249. require_executable('git')
  250. local push_first = true
  251. local checked_out_branch = run({ 'git', 'rev-parse', '--abbrev-ref', 'HEAD' })
  252. if push_first then
  253. local push_remote =
  254. run({ 'git', 'config', '--get', 'branch.' .. checked_out_branch .. '.pushRemote' })
  255. if push_remote == nil then
  256. push_remote = run({ 'git', 'config', '--get', 'remote.pushDefault' })
  257. if push_remote == nil then
  258. push_remote =
  259. run({ 'git', 'config', '--get', 'branch.' .. checked_out_branch .. '.remote' })
  260. if push_remote == nil or push_remote == find_git_remote(nil) then
  261. push_remote = find_git_remote('fork')
  262. end
  263. end
  264. end
  265. p('Pushing to ' .. push_remote .. '/' .. checked_out_branch)
  266. run_die({ 'git', 'push', push_remote, checked_out_branch }, 'Git failed to push')
  267. end
  268. local pr_url = gh_pr(pr_title, pr_body)
  269. p('\nCreated PR: ' .. pr_url .. '\n')
  270. end
  271. function M.commit(dependency_name, commit)
  272. local dependency = assert(get_dependency(dependency_name))
  273. verify_cmakelists_committed()
  274. local commit_sha = get_gh_commit_sha(dependency.repo, commit)
  275. if commit_sha ~= commit then
  276. p('Not a commit: ' .. commit .. '. Did you mean version?')
  277. die()
  278. end
  279. local archive = get_archive_info(dependency.repo, commit)
  280. if dependency_name == 'Luv' then
  281. warn_luv_symbol()
  282. end
  283. update_cmakelists(dependency, archive, short_commit(commit))
  284. end
  285. function M.version(dependency_name, version)
  286. vim.validate('dependency_name', dependency_name, 'string')
  287. vim.validate('version', version, 'string')
  288. local dependency = assert(get_dependency(dependency_name))
  289. verify_cmakelists_committed()
  290. local commit_sha = get_gh_commit_sha(dependency.repo, version)
  291. if commit_sha == version then
  292. p('Not a version: ' .. version .. '. Did you mean commit?')
  293. die()
  294. end
  295. local archive = get_archive_info(dependency.repo, version)
  296. if dependency_name == 'Luv' then
  297. write_cmakelists_line(dependency.symbol, 'VERSION', version)
  298. end
  299. update_cmakelists(dependency, archive, version)
  300. end
  301. function M.head(dependency_name)
  302. local dependency = assert(get_dependency(dependency_name))
  303. verify_cmakelists_committed()
  304. local commit_sha = get_gh_commit_sha(dependency.repo, 'HEAD')
  305. local archive = get_archive_info(dependency.repo, commit_sha)
  306. if dependency_name == 'Luv' then
  307. warn_luv_symbol()
  308. end
  309. update_cmakelists(dependency, archive, 'HEAD - ' .. short_commit(commit_sha))
  310. end
  311. function M.create_branch(dep)
  312. explicit_create_branch(dep)
  313. end
  314. function M.submit_pr()
  315. require_executable('git')
  316. verify_branch('deps')
  317. local nvim_remote = find_git_remote(nil)
  318. local relevant_commit = assert(run_die({
  319. 'git',
  320. 'log',
  321. '--grep=' .. commit_prefix,
  322. '--reverse',
  323. "--format='%s'",
  324. nvim_remote .. '/master..HEAD',
  325. '-1',
  326. }, 'Failed to fetch commits'))
  327. local pr_title
  328. local pr_body
  329. if relevant_commit == '' then
  330. pr_title = commit_prefix .. 'bump some dependencies'
  331. pr_body = 'bump some dependencies'
  332. else
  333. relevant_commit = relevant_commit:gsub("'", '')
  334. pr_title = relevant_commit
  335. pr_body = relevant_commit:gsub(commit_prefix:gsub('%(', '%%('):gsub('%)', '%%)'), '')
  336. end
  337. pr_body = pr_body .. '\n\n(add explanations if needed)'
  338. p(pr_title .. '\n' .. pr_body .. '\n')
  339. create_pr(pr_title, pr_body)
  340. end
  341. local function usage()
  342. local this_script = _G.arg[0]:match('[^/]*.lua$')
  343. print(([=[
  344. Bump Nvim dependencies
  345. Usage: nvim -l %s [options]
  346. Bump to HEAD, tagged version, commit, or branch:
  347. nvim -l %s --dep Luv --head
  348. nvim -l %s --dep Luv --version 1.43.0-0
  349. nvim -l %s --dep Luv --commit abc123
  350. nvim -l %s --dep Luv --branch
  351. Create a PR:
  352. nvim -l %s --pr
  353. Options:
  354. -h show this message and exit.
  355. --pr submit pr for bumping deps.
  356. --branch <dep> create a branch bump-<dep> from current branch.
  357. --dep <dependency> bump to a specific release or tag.
  358. Dependency Options:
  359. --version <tag> bump to a specific release or tag.
  360. --commit <hash> bump to a specific commit.
  361. --HEAD bump to a current head.
  362. <dependency> is one of:
  363. "LuaJIT", "libuv", "Luv", "tree-sitter"
  364. ]=]):format(this_script, this_script, this_script, this_script, this_script, this_script))
  365. end
  366. local function parseargs()
  367. local args = {}
  368. for i = 1, #_G.arg do
  369. if _G.arg[i] == '-h' then
  370. args.h = true
  371. elseif _G.arg[i] == '--pr' then
  372. args.pr = true
  373. elseif _G.arg[i] == '--branch' then
  374. args.branch = _G.arg[i + 1]
  375. elseif _G.arg[i] == '--dep' then
  376. args.dep = _G.arg[i + 1]
  377. elseif _G.arg[i] == '--version' then
  378. args.version = _G.arg[i + 1]
  379. elseif _G.arg[i] == '--commit' then
  380. args.commit = _G.arg[i + 1]
  381. elseif _G.arg[i] == '--head' then
  382. args.head = true
  383. end
  384. end
  385. return args
  386. end
  387. local is_main = _G.arg[0]:match('bump_deps.lua')
  388. if is_main then
  389. local args = parseargs()
  390. if args.h then
  391. usage()
  392. elseif args.pr then
  393. M.submit_pr()
  394. elseif args.head then
  395. M.head(args.dep)
  396. elseif args.branch then
  397. M.create_branch(args.dep)
  398. elseif args.version then
  399. M.version(args.dep, args.version)
  400. elseif args.commit then
  401. M.commit(args.dep, args.commit)
  402. elseif args.pr then
  403. M.submit_pr()
  404. else
  405. print('missing required arg\n')
  406. os.exit(1)
  407. end
  408. else
  409. return M
  410. end