Merge pull request #361 from junegunn/snapshot-in-vimscript

PlugSnapshot output in Vim script format (#360)
This commit is contained in:
Junegunn Choi 2015-12-17 23:34:37 +09:00
commit 6843e5aeec
3 changed files with 52 additions and 46 deletions

View file

@ -105,7 +105,7 @@ Reload .vimrc and `:PlugInstall` to install plugins.
| `PlugUpgrade` | Upgrade vim-plug itself | | `PlugUpgrade` | Upgrade vim-plug itself |
| `PlugStatus` | Check the status of plugins | | `PlugStatus` | Check the status of plugins |
| `PlugDiff` | See the updated changes from the previous PlugUpdate | | `PlugDiff` | See the updated changes from the previous PlugUpdate |
| `PlugSnapshot [output path]` | Generate script for restoring the current snapshot of the plugins | | `PlugSnapshot[!] [output path]` | Generate script for restoring the current snapshot of the plugins |
### `Plug` options ### `Plug` options

View file

@ -117,13 +117,13 @@ function! s:define_commands()
if !executable('git') if !executable('git')
return s:err('`git` executable not found. vim-plug requires git.') return s:err('`git` executable not found. vim-plug requires git.')
endif endif
command! -nargs=* -bar -bang -complete=customlist,s:names PlugInstall call s:install('<bang>' == '!', [<f-args>]) command! -nargs=* -bar -bang -complete=customlist,s:names PlugInstall call s:install(<bang>0, [<f-args>])
command! -nargs=* -bar -bang -complete=customlist,s:names PlugUpdate call s:update('<bang>' == '!', [<f-args>]) command! -nargs=* -bar -bang -complete=customlist,s:names PlugUpdate call s:update(<bang>0, [<f-args>])
command! -nargs=0 -bar -bang PlugClean call s:clean('<bang>' == '!') command! -nargs=0 -bar -bang PlugClean call s:clean(<bang>0)
command! -nargs=0 -bar PlugUpgrade if s:upgrade() | execute 'source' s:esc(s:me) | endif command! -nargs=0 -bar PlugUpgrade if s:upgrade() | execute 'source' s:esc(s:me) | endif
command! -nargs=0 -bar PlugStatus call s:status() command! -nargs=0 -bar PlugStatus call s:status()
command! -nargs=0 -bar PlugDiff call s:diff() command! -nargs=0 -bar PlugDiff call s:diff()
command! -nargs=? -bar PlugSnapshot call s:snapshot(<f-args>) command! -nargs=? -bar -bang -complete=file PlugSnapshot call s:snapshot(<bang>0, <f-args>)
endfunction endfunction
function! s:to_a(v) function! s:to_a(v)
@ -146,6 +146,16 @@ function! s:assoc(dict, key, val)
let a:dict[a:key] = add(get(a:dict, a:key, []), a:val) let a:dict[a:key] = add(get(a:dict, a:key, []), a:val)
endfunction endfunction
function! s:ask(message)
call inputsave()
echohl WarningMsg
let proceed = input(a:message.' (y/N) ') =~? '^y'
echohl None
call inputrestore()
echo "\r"
return proceed
endfunction
function! plug#end() function! plug#end()
if !exists('g:plugs') if !exists('g:plugs')
return s:err('Call plug#begin() first') return s:err('Call plug#begin() first')
@ -1817,10 +1827,7 @@ function! s:clean(force)
if empty(todo) if empty(todo)
call append(line('$'), 'Already clean.') call append(line('$'), 'Already clean.')
else else
call inputsave() if a:force || s:ask('Proceed?')
let yes = a:force || (input('Proceed? (y/N) ') =~? '^y')
call inputrestore()
if yes
for dir in todo for dir in todo
call s:rm_rf(dir) call s:rm_rf(dir)
endfor endfor
@ -2034,42 +2041,35 @@ function! s:revert()
echo 'Reverted.' echo 'Reverted.'
endfunction endfunction
function! s:snapshot(...) abort function! s:snapshot(force, ...) abort
let home = get(s:, 'plug_home_org', g:plug_home)
let [type, var, header] = s:is_win ?
\ ['dosbatch', '%PLUG_HOME%',
\ ['@echo off', ':: Generated by vim-plug', ':: '.strftime("%c"), '',
\ ':: Make sure to PlugUpdate first with `let g:plug_shallow = 0`', '', 'set PLUG_HOME='.home]] :
\ ['sh', '$PLUG_HOME',
\ ['#!/bin/sh', '# Generated by vim-plug', '# '.strftime("%c"), '',
\ 'vim -c ''let g:plug_shallow = 0 | PlugUpdate | qa''', '', 'PLUG_HOME='.s:esc(home)]]
call s:prepare() call s:prepare()
execute 'setf' type setf vim
call append(0, header) call append(0, ['" Generated by vim-plug',
call append('$', '') \ '" '.strftime("%c"),
\ '" :source this file in vim to restore the snapshot',
\ '" or execute: vim -S snapshot.vim',
\ '', '', 'PlugUpdate!'])
1 1
redraw let anchor = line('$') - 3
let names = sort(keys(filter(copy(g:plugs),
let dirs = sort(map(values(filter(copy(g:plugs), \'has_key(v:val, "uri") && !has_key(v:val, "commit") && isdirectory(v:val.dir)')))
\'has_key(v:val, "uri") && !has_key(v:val, "commit") && isdirectory(v:val.dir)')), 'v:val.dir')) for name in reverse(names)
let anchor = line('$') - 1 let sha = s:system_chomp('git rev-parse --short HEAD', g:plugs[name].dir)
for dir in reverse(dirs)
let sha = s:system_chomp('git rev-parse --short HEAD', dir)
if !empty(sha) if !empty(sha)
call append(anchor, printf('cd %s && git reset --hard %s', call append(anchor, printf("silent! let g:plugs['%s'].commit = '%s'", name, sha))
\ substitute(dir, '^\V'.escape(g:plug_home, '\'), var, ''), sha))
redraw redraw
endif endif
endfor endfor
if a:0 > 0 if a:0 > 0
let fn = expand(a:1) let fn = expand(a:1)
let fne = s:esc(fn) if filereadable(fn) && !(a:force || s:ask(a:1.' already exists. Overwrite?'))
return
endif
call writefile(getline(1, '$'), fn) call writefile(getline(1, '$'), fn)
if !s:is_win | call s:system('chmod +x ' . fne) | endif echo 'Saved as '.a:1
echo 'Saved to '.a:1 silent execute 'e' s:esc(fn)
silent execute 'e' fne setf vim
endif endif
endfunction endfunction

View file

@ -1158,17 +1158,22 @@ Execute (PlugSnapshot / #154 issues with paths containing spaces):
PlugInstall PlugInstall
call plug#load('vim-easy-align') " Should properly handle paths with spaces call plug#load('vim-easy-align') " Should properly handle paths with spaces
PlugSnapshot PlugSnapshot
AssertEqual '#!/bin/sh', getline(1) AssertEqual '" Generated by vim-plug', getline(1)
AssertEqual '# Generated by vim-plug', getline(2) AssertEqual 0, stridx(getline(6), "silent! let g:plugs['seoul256.vim'].commit = '")
AssertEqual 'vim -c ''let g:plug_shallow = 0 | PlugUpdate | qa''', getline(5) AssertEqual 0, stridx(getline(7), "silent! let g:plugs['vim-easy-align'].commit = '")
AssertEqual 'PLUG_HOME=$TMPDIR/plug\ with\ spaces', getline(7) AssertEqual 'vim', &filetype
AssertEqual 0, stridx(getline(9), 'cd $PLUG_HOME/seoul256.vim/ && git reset --hard')
AssertEqual 0, stridx(getline(10), 'cd $PLUG_HOME/vim-easy-align/ && git reset --hard')
AssertEqual 'sh', &filetype
execute 'PlugSnapshot' g:plug_home.'/snapshot.sh' call delete(g:plug_home.'/snapshot.vim')
AssertEqual 'sh', &filetype execute 'PlugSnapshot' escape(g:plug_home.'/snapshot.vim', ' ')
AssertEqual 'snapshot.sh', fnamemodify(expand('%'), ':t') AssertEqual 'vim', &filetype
AssertEqual 'snapshot.vim', fnamemodify(expand('%'), ':t')
q
Execute(PlugSnapshot! to overwrite existing file):
call writefile(['foobar'], g:plug_home.'/snapshot.vim')
AssertEqual 'foobar', readfile(g:plug_home.'/snapshot.vim')[0]
execute 'PlugSnapshot!' escape(g:plug_home.'/snapshot.vim', ' ')
AssertEqual '" Generated by vim-plug', readfile(g:plug_home.'/snapshot.vim')[0]
q q
********************************************************************** **********************************************************************
@ -1259,7 +1264,8 @@ Execute (Commit hash support):
" Nor in PlugSnapshot output " Nor in PlugSnapshot output
PlugSnapshot PlugSnapshot
AssertEqual 9, line('$') Log getline(1, '$')
AssertEqual 8, line('$')
q q
Execute (Commit hash support - cleared): Execute (Commit hash support - cleared):