Skip to content

Commit 6843e5a

Browse files
committed
Merge pull request #361 from junegunn/snapshot-in-vimscript
PlugSnapshot output in Vim script format (#360)
2 parents e929534 + 0cfa683 commit 6843e5a

File tree

3 files changed

+52
-46
lines changed

3 files changed

+52
-46
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Reload .vimrc and `:PlugInstall` to install plugins.
105105
| `PlugUpgrade` | Upgrade vim-plug itself |
106106
| `PlugStatus` | Check the status of plugins |
107107
| `PlugDiff` | See the updated changes from the previous PlugUpdate |
108-
| `PlugSnapshot [output path]` | Generate script for restoring the current snapshot of the plugins |
108+
| `PlugSnapshot[!] [output path]` | Generate script for restoring the current snapshot of the plugins |
109109

110110
### `Plug` options
111111

plug.vim

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ function! s:define_commands()
117117
if !executable('git')
118118
return s:err('`git` executable not found. vim-plug requires git.')
119119
endif
120-
command! -nargs=* -bar -bang -complete=customlist,s:names PlugInstall call s:install('<bang>' == '!', [<f-args>])
121-
command! -nargs=* -bar -bang -complete=customlist,s:names PlugUpdate call s:update('<bang>' == '!', [<f-args>])
122-
command! -nargs=0 -bar -bang PlugClean call s:clean('<bang>' == '!')
120+
command! -nargs=* -bar -bang -complete=customlist,s:names PlugInstall call s:install(<bang>0, [<f-args>])
121+
command! -nargs=* -bar -bang -complete=customlist,s:names PlugUpdate call s:update(<bang>0, [<f-args>])
122+
command! -nargs=0 -bar -bang PlugClean call s:clean(<bang>0)
123123
command! -nargs=0 -bar PlugUpgrade if s:upgrade() | execute 'source' s:esc(s:me) | endif
124124
command! -nargs=0 -bar PlugStatus call s:status()
125125
command! -nargs=0 -bar PlugDiff call s:diff()
126-
command! -nargs=? -bar PlugSnapshot call s:snapshot(<f-args>)
126+
command! -nargs=? -bar -bang -complete=file PlugSnapshot call s:snapshot(<bang>0, <f-args>)
127127
endfunction
128128

129129
function! s:to_a(v)
@@ -146,6 +146,16 @@ function! s:assoc(dict, key, val)
146146
let a:dict[a:key] = add(get(a:dict, a:key, []), a:val)
147147
endfunction
148148

149+
function! s:ask(message)
150+
call inputsave()
151+
echohl WarningMsg
152+
let proceed = input(a:message.' (y/N) ') =~? '^y'
153+
echohl None
154+
call inputrestore()
155+
echo "\r"
156+
return proceed
157+
endfunction
158+
149159
function! plug#end()
150160
if !exists('g:plugs')
151161
return s:err('Call plug#begin() first')
@@ -1817,10 +1827,7 @@ function! s:clean(force)
18171827
if empty(todo)
18181828
call append(line('$'), 'Already clean.')
18191829
else
1820-
call inputsave()
1821-
let yes = a:force || (input('Proceed? (y/N) ') =~? '^y')
1822-
call inputrestore()
1823-
if yes
1830+
if a:force || s:ask('Proceed?')
18241831
for dir in todo
18251832
call s:rm_rf(dir)
18261833
endfor
@@ -2034,42 +2041,35 @@ function! s:revert()
20342041
echo 'Reverted.'
20352042
endfunction
20362043

2037-
function! s:snapshot(...) abort
2038-
let home = get(s:, 'plug_home_org', g:plug_home)
2039-
let [type, var, header] = s:is_win ?
2040-
\ ['dosbatch', '%PLUG_HOME%',
2041-
\ ['@echo off', ':: Generated by vim-plug', ':: '.strftime("%c"), '',
2042-
\ ':: Make sure to PlugUpdate first with `let g:plug_shallow = 0`', '', 'set PLUG_HOME='.home]] :
2043-
\ ['sh', '$PLUG_HOME',
2044-
\ ['#!/bin/sh', '# Generated by vim-plug', '# '.strftime("%c"), '',
2045-
\ 'vim -c ''let g:plug_shallow = 0 | PlugUpdate | qa''', '', 'PLUG_HOME='.s:esc(home)]]
2046-
2044+
function! s:snapshot(force, ...) abort
20472045
call s:prepare()
2048-
execute 'setf' type
2049-
call append(0, header)
2050-
call append('$', '')
2046+
setf vim
2047+
call append(0, ['" Generated by vim-plug',
2048+
\ '" '.strftime("%c"),
2049+
\ '" :source this file in vim to restore the snapshot',
2050+
\ '" or execute: vim -S snapshot.vim',
2051+
\ '', '', 'PlugUpdate!'])
20512052
1
2052-
redraw
2053-
2054-
let dirs = sort(map(values(filter(copy(g:plugs),
2055-
\'has_key(v:val, "uri") && !has_key(v:val, "commit") && isdirectory(v:val.dir)')), 'v:val.dir'))
2056-
let anchor = line('$') - 1
2057-
for dir in reverse(dirs)
2058-
let sha = s:system_chomp('git rev-parse --short HEAD', dir)
2053+
let anchor = line('$') - 3
2054+
let names = sort(keys(filter(copy(g:plugs),
2055+
\'has_key(v:val, "uri") && !has_key(v:val, "commit") && isdirectory(v:val.dir)')))
2056+
for name in reverse(names)
2057+
let sha = s:system_chomp('git rev-parse --short HEAD', g:plugs[name].dir)
20592058
if !empty(sha)
2060-
call append(anchor, printf('cd %s && git reset --hard %s',
2061-
\ substitute(dir, '^\V'.escape(g:plug_home, '\'), var, ''), sha))
2059+
call append(anchor, printf("silent! let g:plugs['%s'].commit = '%s'", name, sha))
20622060
redraw
20632061
endif
20642062
endfor
20652063

20662064
if a:0 > 0
20672065
let fn = expand(a:1)
2068-
let fne = s:esc(fn)
2066+
if filereadable(fn) && !(a:force || s:ask(a:1.' already exists. Overwrite?'))
2067+
return
2068+
endif
20692069
call writefile(getline(1, '$'), fn)
2070-
if !s:is_win | call s:system('chmod +x ' . fne) | endif
2071-
echo 'Saved to '.a:1
2072-
silent execute 'e' fne
2070+
echo 'Saved as '.a:1
2071+
silent execute 'e' s:esc(fn)
2072+
setf vim
20732073
endif
20742074
endfunction
20752075

test/workflow.vader

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,17 +1158,22 @@ Execute (PlugSnapshot / #154 issues with paths containing spaces):
11581158
PlugInstall
11591159
call plug#load('vim-easy-align') " Should properly handle paths with spaces
11601160
PlugSnapshot
1161-
AssertEqual '#!/bin/sh', getline(1)
1162-
AssertEqual '# Generated by vim-plug', getline(2)
1163-
AssertEqual 'vim -c ''let g:plug_shallow = 0 | PlugUpdate | qa''', getline(5)
1164-
AssertEqual 'PLUG_HOME=$TMPDIR/plug\ with\ spaces', getline(7)
1165-
AssertEqual 0, stridx(getline(9), 'cd $PLUG_HOME/seoul256.vim/ && git reset --hard')
1166-
AssertEqual 0, stridx(getline(10), 'cd $PLUG_HOME/vim-easy-align/ && git reset --hard')
1167-
AssertEqual 'sh', &filetype
1161+
AssertEqual '" Generated by vim-plug', getline(1)
1162+
AssertEqual 0, stridx(getline(6), "silent! let g:plugs['seoul256.vim'].commit = '")
1163+
AssertEqual 0, stridx(getline(7), "silent! let g:plugs['vim-easy-align'].commit = '")
1164+
AssertEqual 'vim', &filetype
11681165

1169-
execute 'PlugSnapshot' g:plug_home.'/snapshot.sh'
1170-
AssertEqual 'sh', &filetype
1171-
AssertEqual 'snapshot.sh', fnamemodify(expand('%'), ':t')
1166+
call delete(g:plug_home.'/snapshot.vim')
1167+
execute 'PlugSnapshot' escape(g:plug_home.'/snapshot.vim', ' ')
1168+
AssertEqual 'vim', &filetype
1169+
AssertEqual 'snapshot.vim', fnamemodify(expand('%'), ':t')
1170+
q
1171+
1172+
Execute(PlugSnapshot! to overwrite existing file):
1173+
call writefile(['foobar'], g:plug_home.'/snapshot.vim')
1174+
AssertEqual 'foobar', readfile(g:plug_home.'/snapshot.vim')[0]
1175+
execute 'PlugSnapshot!' escape(g:plug_home.'/snapshot.vim', ' ')
1176+
AssertEqual '" Generated by vim-plug', readfile(g:plug_home.'/snapshot.vim')[0]
11721177
q
11731178

11741179
**********************************************************************
@@ -1259,7 +1264,8 @@ Execute (Commit hash support):
12591264

12601265
" Nor in PlugSnapshot output
12611266
PlugSnapshot
1262-
AssertEqual 9, line('$')
1267+
Log getline(1, '$')
1268+
AssertEqual 8, line('$')
12631269
q
12641270

12651271
Execute (Commit hash support - cleared):

0 commit comments

Comments
 (0)