robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local M ={} 2 3local health =vim . health 4 5local function get_lockfile_path () 6return vim . pack . _plugin_lock_path or vim . go . packlockfile 7end 8 9local function get_plug_dir () 10return vim . fs . joinpath ( vim . fn . stdpath ( 'data' ), 'site' , 'pack' , 'core' , 'opt' ) 11end 12 13--- @param cmd string[] 14--- @param cwd string 15local function git_cmd ( cmd , cwd ) 16cmd =vim . list_extend ( { 'git' , '-c' , 'gc.auto=0' } , cmd ) 17local env =vim . fn . environ () --- @type table<string,string> 18env . GIT_DIR , env . GIT_WORK_TREE , env . GIT_INDEX_FILE =nil , nil , nil 19local sys_opts ={ cwd =cwd , text =true , env =env , clear_env =true } 20local out =vim . system ( cmd , sys_opts ): wait () --- @type vim.SystemCompleted 21if out . code ~= 0 then 22return false , (( out . stderr or '' ): gsub ( '\n+$' , '' )) 23end 24return true , (( out . stdout or '' ): gsub ( '\n+$' , '' )) 25end 26 27local function check_basics () 28health . start ( 'vim.pack: basics' ) 29 30-- Requirements 31if vim . fn . executable ( 'git' ) == 0 then 32health . warn ( '`git` executable is required. Install it using your package manager' ) 33return false , false 34end 35 36-- Detect if not used 37local lockfile_path =get_lockfile_path () 38local has_lockfile =vim . fn . filereadable ( lockfile_path ) == 1 39local plug_dir =get_plug_dir () 40local has_plug_dir =vim . fn . isdirectory ( plug_dir ) == 1 41if not has_lockfile and not has_plug_dir then 42health . ok ( '`vim.pack` is not used' ) 43return false , false 44end 45 46-- General info 47local git =vim . fn . exepath ( 'git' ) 48local _ , version =git_cmd ( { 'version' } , vim . uv . cwd ()) 49health . info (( 'Git: %s (%s)' ): format ( version : gsub ( '^git%s*' , '' ), git )) 50health . info ( 'Lockfile: ' .. lockfile_path ) 51health . info ( 'Plugin directory: ' .. plug_dir ) 52 53if has_lockfile and has_plug_dir then 54health . ok ( '' ) 55else 56local lockfile_absent =has_lockfile and 'present' or 'absent' 57local plug_dir_absent =has_plug_dir and 'present' or 'absent' 58local msg =( 'Lockfile is %s, plugin directory is %s.' ): format ( lockfile_absent , plug_dir_absent ) 59.. ' Restart Nvim and run `vim.pack.add({})` to ' 60.. ( has_lockfile and 'install plugins from the lockfile' or 'regenerate the lockfile' ) 61health . warn ( msg ) 62end 63 64return has_lockfile , has_plug_dir 65end 66 67--- @param x any 68local function is_version ( x ) 69return type ( x ) == 'string' or ( type ( x ) == 'table' and pcall ( x . has , x , '1' )) 70end 71 72--- @param plug_name string 73--- @param plug_path string 74local function failed_git_cmd ( plug_name , plug_path ) 75local msg =( 'Failed Git command inside plugin %s.' ): format ( vim . inspect ( plug_name )) 76.. ' This is unexpected (like after interrupted `git clone`) and should not happen.' 77.. ( ' Manually delete directory %s and reinstall plugin' ): format ( plug_path ) 78health . error ( msg ) 79return false 80end 81 82--- @param plug_name any 83--- @param lock_data any 84--- @return boolean Whether a check is successful 85local function check_plugin_lock_data ( plug_name , lock_data ) 86local name_str =vim . inspect ( plug_name ) 87--- @param reason string 88local function error_with_del_advice ( reason ) 89local msg =( '%s %s.' ): format ( name_str , reason ) 90.. ( ' Delete %s entry (do not create trailing comma) and ' ): format ( name_str ) 91.. 'restart Nvim to regenerate lockfile data' 92health . error ( msg ) 93return false 94end 95 96-- Types 97if type ( plug_name ) ~= 'string' then 98return error_with_del_advice ( 'is not a valid plugin name' ) 99end 100if type ( lock_data ) ~= 'table' then 101return error_with_del_advice ( 'entry is not a valid type' ) 102end 103if type ( lock_data . rev ) ~= 'string' then 104local reason ='`rev` entry is ' .. ( lock_data . rev and 'not a valid type' or 'missing' ) 105return error_with_del_advice ( reason ) 106end 107if type ( lock_data . src ) ~= 'string' then 108local reason ='`src` entry is ' .. ( lock_data . src and 'not a valid type' or 'missing' ) 109return error_with_del_advice ( reason ) 110end 111if lock_data . version and not is_version ( lock_data . version ) then 112return error_with_del_advice ( '`version` entry is not a valid type' ) 113end 114 115-- Alignment with what is actually present on disk 116local plug_path =vim . fs . joinpath ( get_plug_dir (), plug_name ) 117if vim . fn . isdirectory ( plug_path ) ~= 1 then 118health . warn ( 119( 'Plugin %s is not installed but present in the lockfile.' ): format ( name_str ) 120.. ' Restart Nvim and run `vim.pack.add({})` to autoinstall.' 121.. ( ' To fully delete, run `vim.pack.del({ %s }, { force = true })`' ): format ( name_str ) 122) 123return false 124end 125 126-- NOTE: `vim.pack` currently only supports Git repos as plugins 127if not git_cmd ( { 'rev-parse' , '--git-dir' } , plug_path ) then 128return true 129end 130 131local has_head , head =git_cmd ( { 'rev-list' , '-1' , 'HEAD' } , plug_path ) 132if not has_head then 133return failed_git_cmd ( plug_name , plug_path ) 134elseif lock_data . rev ~= head then 135health . error ( 136( 'Plugin %s is not at expected revision\n' ): format ( name_str ) 137.. ( 'Expected: %s\nActual: %s\n' ): format ( lock_data . rev , head ) 138.. 'To synchronize, restart Nvim and run ' 139.. ( '`vim.pack.update({ %s }, { offline = true })`\n' ): format ( name_str ) 140.. 'If there are no updates, delete `rev` lockfile entry (do not create trailing comma) ' 141.. 'and restart Nvim to regenerate lockfile data\n' 142.. 'This can happen after updating plugins with read-only `$XDG_CONFIG_HOME`' 143) 144return false 145end 146 147local has_origin , origin =git_cmd ( { 'remote' , 'get-url' , 'origin' } , plug_path ) 148if not has_origin then 149return failed_git_cmd ( plug_name , plug_path ) 150elseif lock_data . src ~= origin then 151-- Check if lockfile source relies on "insteadOf" Git config 152local ok , src_resolved =git_cmd ( { 'ls-remote' , '--get-url' , lock_data . src } , plug_path ) 153if not ( ok and src_resolved == origin ) then 154health . error ( 155( 'Plugin %s has not expected source\n' ): format ( name_str ) 156.. ( 'Expected: %s\nActual: %s\n' ): format ( lock_data . src , origin ) 157.. 'Delete `src` lockfile entry (do not create trailing comma) and ' 158.. 'restart Nvim to regenerate lockfile data' 159) 160return false 161end 162end 163 164return true 165end 166 167local function check_lockfile () 168health . start ( 'vim.pack: lockfile' ) 169 170local path =get_lockfile_path () 171local can_read , text =pcall ( vim . fn . readblob , path ) 172if not can_read then 173health . error ( 'Could not read lockfile. Delete it and restart Nvim.' ) 174return 175end 176 177--- @cast text string 178local can_parse , data =pcall ( vim . json . decode , text ) 179if not can_parse then 180health . error (( 'Could not parse lockfile: %s\nDelete it and restart Nvim' ): format ( data )) 181return 182end 183 184if type ( data . plugins ) ~= 'table' then 185health . error ( 'Field `plugins` is not proper type. Delete lockfile and restart Nvim' ) 186return 187end 188 189local is_good =true 190if path ~= vim . go . packlockfile then 191health . warn ( 192"Lockfile path is not the same as 'packlockfile' option value. " 193.. "Set 'packlockfile' before the first usage of `vim.pack` function." 194) 195is_good =false 196end 197if path ~= vim . fs . abspath ( path ) then 198health . warn ( 'Lockfile path is not absolute. Make sure that this is intentional.' ) 199is_good =false 200end 201 202--- @cast data { plugins: table<any, any> } 203for plug_name , lock_data in pairs ( data . plugins ) do 204is_good =check_plugin_lock_data ( plug_name , lock_data ) and is_good 205end 206 207if is_good then 208health . ok ( '' ) 209end 210end 211 212--- @param manifest vim.pack.Manifest 213--- @param plug_name string 214--- @param plug_path string 215local function check_manifest ( manifest , plug_name , plug_path ) 216local name_str =vim . inspect ( plug_name ) 217if vim . tbl_count ( manifest ) == 0 then 218health . warn (( 'Plugin %s has empty or malformed manifest file' ): format ( name_str )) 219return false 220end 221 222local nvim_engine =( manifest . engines or {} ). nvim or '*' 223local ok_version , nvim_version_range =pcall ( vim . version . range , nvim_engine ) 224if not ok_version then 225health . warn (( 'Plugin %s has malformed `engines.nvim` in manifest file' ): format ( name_str )) 226return false 227end 228--- @cast nvim_version_range vim.VersionRange 229if not nvim_version_range : has ( vim . version ()) then 230health . warn ( 231( 'Plugin %s Nvim version requirement %s' ): format ( name_str , tostring ( nvim_version_range )) 232.. ( ' does not match current version %s' ): format ( tostring ( vim . version ())) 233) 234return false 235end 236 237local ok_scripts =true 238---@diagnostic disable-next-line: no-unknown 239for name , script_path in pairs ( manifest . scripts or {} ) do 240if vim . fn . filereadable ( vim . fs . joinpath ( plug_path , script_path )) == 0 then 241health . warn (( 'Plugin %s has no %s script at %s path' ): format ( name_str , name , script_path )) 242ok_scripts =false 243end 244end 245if not ok_scripts then 246return false 247end 248 249return true 250end 251 252--- @param plug_name string 253--- @return boolean Whether a check is successful 254local function check_installed_plugin ( plug_name ) 255local name_str =vim . inspect ( plug_name ) 256local plug_path =vim . fs . joinpath ( get_plug_dir (), plug_name ) 257 258if vim . fn . isdirectory ( plug_path ) ~= 1 then 259health . error (( '%s is not a directory. Delete it' ): format ( plug_name )) 260return false 261end 262 263if not git_cmd ( { 'rev-parse' , '--git-dir' } , plug_path ) then 264health . error ( 265( '%s is not a Git repository.' ): format ( name_str ) 266.. ' It was not installed by `vim.pack` and should not be present in the plugin directory.' 267.. ' If installed manually, use dedicated `:h packages`' 268) 269return false 270end 271 272-- Detached HEAD is a sign that plugin is managed by `vim.pack` 273local has_head_ref , head_ref =git_cmd ( { 'rev-parse' , '--abbrev-ref' , 'HEAD' } , plug_path ) 274if not has_head_ref then 275return failed_git_cmd ( plug_name , plug_path ) 276elseif head_ref ~= 'HEAD' then 277health . warn ( 278( 'Plugin %s is not at state which is a result of `vim.pack` operation.\n' ): format ( name_str ) 279.. 'If it was intentional, make sure you know what you are doing.\n' 280.. 'Otherwise, restart Nvim and run ' 281.. ( '`vim.pack.update({ %s }, { offline = true })`.\n' ): format ( name_str ) 282.. 'If nothing is updated, plugin is at correct revision and will be managed as expected' 283) 284return false 285end 286 287-- Usage data 288local has_pack_info , info =pcall ( vim . pack . get , { plug_name } ) 289if not has_pack_info then 290health . error ( 'Could not get `vim.pack` usage information for plugin ' .. name_str ) 291return false 292end 293local plug =assert ( info [ 1 ]) 294 295if not plug . active then 296health . info ( 297( 'Plugin %s is not active.' ): format ( name_str ) 298.. ' Is it lazy loaded or did you forget to run `vim.pack.del()`?' 299) 300end 301 302-- Manifest 303if plug . manifest then 304return check_manifest ( plug . manifest , plug_name , plug_path ) 305end 306 307return true 308end 309 310local function check_plug_dir () 311health . start ( 'vim.pack: plugin directory' ) 312 313local is_good =true 314local plug_dir =get_plug_dir () 315for plug_name , _ , err in vim . fs . dir ( plug_dir , { err =true } ) do 316if err then 317health . error ( err ) 318is_good =false 319else 320is_good =check_installed_plugin ( plug_name ) and is_good 321end 322end 323 324if is_good then 325health . ok ( '' ) 326end 327end 328 329function M . check () 330local has_lockfile , has_plug_dir =check_basics () 331if has_lockfile then 332check_lockfile () 333end 334if has_plug_dir then 335check_plug_dir () 336end 337end 338 339return M