diff --git a/README.md b/README.md index d4fea11..757d469 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ Reload .vimrc and `:PlugInstall` to install plugins. | `PlugUpgrade` | Upgrade vim-plug itself | | `PlugStatus` | Check the status of plugins | | `PlugDiff` | See the updated changes from the previous PlugUpdate | +| `PlugSnapshot [output path]` | Generate script for restoring the current snapshot of the plugins | ### `Plug` options diff --git a/plug.vim b/plug.vim index 8b40490..ea7fa74 100644 --- a/plug.vim +++ b/plug.vim @@ -113,6 +113,7 @@ function! s:define_commands() command! -nargs=0 -bar PlugUpgrade if s:upgrade() | execute 'source' s:me | endif command! -nargs=0 -bar PlugStatus call s:status() command! -nargs=0 -bar PlugDiff call s:diff() + command! -nargs=? -bar PlugSnapshot call s:snapshot() endfunction function! s:to_a(v) @@ -1280,6 +1281,45 @@ function! s:revert() echo 'Reverted.' endfunction +function! s:snapshot(...) abort + let [type, var, header] = s:is_win ? + \ ['dosbatch', '%PLUG_HOME%', + \ ['@echo off', ':: Generated by vim-plug', ':: '.strftime("%c"), '', + \ ':: Make sure to PlugUpdate first', '', 'set PLUG_HOME='.s:esc(g:plug_home)]] : + \ ['sh', '$PLUG_HOME', + \ ['#!/bin/bash', '# Generated by vim-plug', '# '.strftime("%c"), '', + \ 'vim +PlugUpdate +qa', '', 'PLUG_HOME='.s:esc(g:plug_home)]] + + call s:prepare() + execute 'setf' type + call append(0, header) + call append('$', '') + 1 + redraw + + let dirs = sort(map(values(filter(copy(g:plugs), + \'has_key(v:val, "uri") && isdirectory(v:val.dir)')), 'v:val.dir')) + let anchor = line('$') - 1 + for dir in reverse(dirs) + execute 'cd' dir + let sha = substitute(system('git rev-parse --short HEAD'), '\n', '', '') + cd - + if !v:shell_error + call append(anchor, printf('cd %s && git reset --hard %s', + \ substitute(dir, '^'.g:plug_home, var, ''), sha)) + redraw + endif + endfor + + if a:0 > 0 + let fn = s:esc(expand(a:1)) + call writefile(getline(1, '$'), fn) + if !s:is_win | call system('chmod +x ' . fn) | endif + echo 'Saved to '.a:1 + silent execute 'e' fn + endif +endfunction + function! s:split_rtp() return split(&rtp, '\\\@