Эх сурвалжийг харах

PlugSnapshot output in Vim script format (#360)

- The output file is no longer executable but a source-able vim script
- PlugSnapshot FILENAME to prompt the user if the file already exists
- Add PlugSnapshot! FILENAME variant to force overwrite existing file
- Apply -complete=file option to PlugSnapshot command
Junegunn Choi 10 жил өмнө
parent
commit
0cfa683cd0
3 өөрчлөгдсөн 52 нэмэгдсэн , 46 устгасан
  1. 1 1
      README.md
  2. 34 34
      plug.vim
  3. 17 11
      test/workflow.vader

+ 1 - 1
README.md

@@ -105,7 +105,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  |
+| `PlugSnapshot[!] [output path]`     | Generate script for restoring the current snapshot of the plugins  |
 
 ### `Plug` options
 

+ 34 - 34
plug.vim

@@ -117,13 +117,13 @@ function! s:define_commands()
   if !executable('git')
     return s:err('`git` executable not found. vim-plug requires git.')
   endif
-  command! -nargs=* -bar -bang -complete=customlist,s:names PlugInstall call s:install('<bang>' == '!', [<f-args>])
-  command! -nargs=* -bar -bang -complete=customlist,s:names PlugUpdate  call s:update('<bang>' == '!', [<f-args>])
-  command! -nargs=0 -bar -bang PlugClean call s:clean('<bang>' == '!')
+  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>0, [<f-args>])
+  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 PlugStatus  call s:status()
   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
 
 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)
 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()
   if !exists('g:plugs')
     return s:err('Call plug#begin() first')
@@ -1817,10 +1827,7 @@ function! s:clean(force)
   if empty(todo)
     call append(line('$'), 'Already clean.')
   else
-    call inputsave()
-    let yes = a:force || (input('Proceed? (y/N) ') =~? '^y')
-    call inputrestore()
-    if yes
+    if a:force || s:ask('Proceed?')
       for dir in todo
         call s:rm_rf(dir)
       endfor
@@ -2034,42 +2041,35 @@ function! s:revert()
   echo 'Reverted.'
 endfunction
 
-function! s:snapshot(...) 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)]]
-
+function! s:snapshot(force, ...) abort
   call s:prepare()
-  execute 'setf' type
-  call append(0, header)
-  call append('$', '')
+  setf vim
+  call append(0, ['" Generated by vim-plug',
+                \ '" '.strftime("%c"),
+                \ '" :source this file in vim to restore the snapshot',
+                \ '" or execute: vim -S snapshot.vim',
+                \ '', '', 'PlugUpdate!'])
   1
-  redraw
-
-  let dirs = sort(map(values(filter(copy(g:plugs),
-        \'has_key(v:val, "uri") && !has_key(v:val, "commit") && isdirectory(v:val.dir)')), 'v:val.dir'))
-  let anchor = line('$') - 1
-  for dir in reverse(dirs)
-    let sha = s:system_chomp('git rev-parse --short HEAD', dir)
+  let anchor = line('$') - 3
+  let names = sort(keys(filter(copy(g:plugs),
+        \'has_key(v:val, "uri") && !has_key(v:val, "commit") && isdirectory(v:val.dir)')))
+  for name in reverse(names)
+    let sha = s:system_chomp('git rev-parse --short HEAD', g:plugs[name].dir)
     if !empty(sha)
-      call append(anchor, printf('cd %s && git reset --hard %s',
-            \ substitute(dir, '^\V'.escape(g:plug_home, '\'), var, ''), sha))
+      call append(anchor, printf("silent! let g:plugs['%s'].commit = '%s'", name, sha))
       redraw
     endif
   endfor
 
   if a:0 > 0
     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)
-    if !s:is_win | call s:system('chmod +x ' . fne) | endif
-    echo 'Saved to '.a:1
-    silent execute 'e' fne
+    echo 'Saved as '.a:1
+    silent execute 'e' s:esc(fn)
+    setf vim
   endif
 endfunction
 

+ 17 - 11
test/workflow.vader

@@ -1140,17 +1140,22 @@ Execute (PlugSnapshot / #154 issues with paths containing spaces):
   PlugInstall
   call plug#load('vim-easy-align') " Should properly handle paths with spaces
   PlugSnapshot
-  AssertEqual '#!/bin/sh', getline(1)
-  AssertEqual '# Generated by vim-plug', getline(2)
-  AssertEqual 'vim -c ''let g:plug_shallow = 0 | PlugUpdate | qa''', getline(5)
-  AssertEqual 'PLUG_HOME=$TMPDIR/plug\ with\ spaces', getline(7)
-  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
+  AssertEqual '" Generated by vim-plug', getline(1)
+  AssertEqual 0, stridx(getline(6), "silent! let g:plugs['seoul256.vim'].commit = '")
+  AssertEqual 0, stridx(getline(7), "silent! let g:plugs['vim-easy-align'].commit = '")
+  AssertEqual 'vim', &filetype
 
-  execute 'PlugSnapshot' g:plug_home.'/snapshot.sh'
-  AssertEqual 'sh', &filetype
-  AssertEqual 'snapshot.sh', fnamemodify(expand('%'), ':t')
+  call delete(g:plug_home.'/snapshot.vim')
+  execute 'PlugSnapshot' escape(g:plug_home.'/snapshot.vim', ' ')
+  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
 
 **********************************************************************
@@ -1241,7 +1246,8 @@ Execute (Commit hash support):
 
   " Nor in PlugSnapshot output
   PlugSnapshot
-  AssertEqual 9, line('$')
+  Log getline(1, '$')
+  AssertEqual 8, line('$')
   q
 
 Execute (Commit hash support - cleared):