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