robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
dce8bdd247
master
1local mpack =vim . mpack 2 3assert ( # arg == 7 ) 4local funcs_metadata_inputf =arg [ 1 ] -- exported functions metadata 5local ui_metadata_inputf =arg [ 2 ] -- ui events metadata 6local ui_options_inputf =arg [ 3 ] -- for ui options 7local git_version_inputf =arg [ 4 ] -- git version header 8local nvim_version_inputf =arg [ 5 ] -- nvim version 9local dump_bin_array_inputf =arg [ 6 ] 10local api_metadata_outputf =arg [ 7 ] 11 12local version =loadfile ( nvim_version_inputf )() --[[@as {[1]: string, [2]: any}[] ]] 13local git_version =io . open ( git_version_inputf ): read '*a' 14local version_build =git_version : match ( '#define NVIM_VERSION_BUILD "([^"]+)"' ) or vim . NIL 15 16local text =assert ( io . open ( ui_options_inputf )): read '*a' 17local ui_options_text =assert ( text : match ( 'ui_ext_names%[][^{]+{([^}]+)}' )) --[[@as string]] 18local ui_options ={ 'rgb' } 19for x in ui_options_text : gmatch ( '"([a-z][a-z_]+)"' ) do 20table . insert ( ui_options , x ) 21end 22 23local pieces ={} --- @type string[] 24 25-- Naively using mpack.encode({foo=x, bar=y}) will make the build 26-- "non-reproducible". Emit maps directly as FIXDICT(2) "foo" x "bar" y instead 27local function fixdict ( num ) 28if num > 15 then 29error 'implement more dict codes' 30end 31pieces [ # pieces + 1 ] =string . char ( 128 + num ) 32end 33 34local function put ( item , item2 ) 35table . insert ( pieces , mpack . encode ( item )) 36if item2 ~= nil then 37table . insert ( pieces , mpack . encode ( item2 )) 38end 39end 40 41fixdict ( 6 ) 42 43put ( 'version' ) 44fixdict ( 1 + # version ) 45for _ , item in ipairs ( version ) do 46-- NB: all items are mandatory. But any error will be less confusing 47-- with placeholder vim.NIL (than invalid mpack data) 48local val =item [ 2 ] == nil and vim . NIL or item [ 2 ] 49put ( item [ 1 ], val ) 50end 51put ( 'build' , version_build ) 52 53put ( 'functions' ) 54table . insert ( pieces , io . open ( funcs_metadata_inputf , 'rb' ): read ( '*all' )) 55put ( 'ui_events' ) 56table . insert ( pieces , io . open ( ui_metadata_inputf , 'rb' ): read ( '*all' )) 57put ( 'ui_options' , ui_options ) 58 59put ( 'error_types' ) 60fixdict ( 2 ) 61put ( 'Exception' , { id =0 } ) 62put ( 'Validation' , { id =1 } ) 63 64put ( 'types' ) 65local types = 66{ { 'Buffer' , 'nvim_buf_' } , { 'Window' , 'nvim_win_' } , { 'Tabpage' , 'nvim_tabpage_' } } 67fixdict ( # types ) 68for i , item in ipairs ( types ) do 69put ( item [ 1 ]) 70fixdict ( 2 ) 71put ( 'id' , i - 1 ) 72put ( 'prefix' , item [ 2 ]) 73end 74 75local packed =table . concat ( pieces ) 76--- @type fun(api_metadata: file*, name: string, packed: string) 77local dump_bin_array =loadfile ( dump_bin_array_inputf )() 78 79-- serialize the API metadata using msgpack and embed into the resulting 80-- binary for easy querying by clients 81local api_metadata_output =assert ( io . open ( api_metadata_outputf , 'wb' )) 82dump_bin_array ( api_metadata_output , 'packed_api_metadata' , packed ) 83api_metadata_output : close ()