Prototype implementation of dependency resolution (#2)

This commit is contained in:
Junegunn Choi 2013-09-25 02:08:42 +09:00
parent 1aea9fed65
commit a9d5912b4d
2 changed files with 131 additions and 50 deletions

View File

@ -14,6 +14,7 @@ Somewhere between [Pathogen](https://github.com/tpope/vim-pathogen) and
- Parallel installation/update (requires - Parallel installation/update (requires
[+ruby](http://junegunn.kr/2013/09/installing-vim-with-ruby-support/)) [+ruby](http://junegunn.kr/2013/09/installing-vim-with-ruby-support/))
- Smallest possible feature set - Smallest possible feature set
- Dependency resolution using `Plugfile` (experimental)
### Cons. ### Cons.
@ -64,6 +65,34 @@ plugins with `plug#begin(path)` call.
(Default number of threads = `g:plug_threads` or 16) (Default number of threads = `g:plug_threads` or 16)
### Dependency resolution
If a Vim plugin specifies its dependent plugins in `Plugfile` in its root
directory, vim-plug will automatically source it recursively during the
installation.
A `Plugfile` should contain a set of `Plug` commands for the dependent plugins.
I've created two dummy repositories with Plugfiles as an example to this scheme.
- [junegunn/dummy1](https://github.com/junegunn/dummy1)
- `Plug 'junegunn/vim-scroll-position'`
- `Plug 'junegunn/dummy2'`
- [junegunn/dummy2](https://github.com/junegunn/dummy2)
- `Plug 'junegunn/Zenburn'`
- `Plug 'junegunn/jellybeans.vim'`
- `Plug 'junegunn/dummy1'`
- (Circular dependencies are ignored)
If you put `Plug 'junegunn/dummy1'` in your configuration file, and run
`:PlugInstall`,
1. vim-plug first installs dummy1
2. And sees if the repository has Plugfile
3. Plugfile is loaded and vim-plug discovers dependent plugins
4. Dependent plugins are then installed as well, and their Plugfiles are
examined and their dependencies are resolved recursively.
### Articles ### Articles
- [Writing my own Vim plugin manager](http://junegunn.kr/2013/09/writing-my-own-vim-plugin-manager) - [Writing my own Vim plugin manager](http://junegunn.kr/2013/09/writing-my-own-vim-plugin-manager)

152
plug.vim
View File

@ -51,6 +51,7 @@ endif
let g:loaded_plug = 1 let g:loaded_plug = 1
let s:plug_source = 'https://raw.github.com/junegunn/vim-plug/master/plug.vim' let s:plug_source = 'https://raw.github.com/junegunn/vim-plug/master/plug.vim'
let s:plug_file = 'Plugfile'
let s:plug_win = 0 let s:plug_win = 0
let s:is_win = has('win32') || has('win64') let s:is_win = has('win32') || has('win64')
let s:me = expand('<sfile>:p') let s:me = expand('<sfile>:p')
@ -74,7 +75,7 @@ function! plug#begin(...)
let g:plug_home = home let g:plug_home = home
let g:plugs = {} let g:plugs = {}
command! -nargs=+ Plug call s:add(<args>) command! -nargs=+ Plug call s:add(1, <args>)
command! -nargs=* PlugInstall call s:install(<f-args>) command! -nargs=* PlugInstall call s:install(<f-args>)
command! -nargs=* PlugUpdate call s:update(<f-args>) command! -nargs=* PlugUpdate call s:update(<f-args>)
command! -nargs=0 -bang PlugClean call s:clean('<bang>' == '!') command! -nargs=0 -bang PlugClean call s:clean('<bang>' == '!')
@ -83,6 +84,11 @@ function! plug#begin(...)
endfunction endfunction
function! plug#end() function! plug#end()
let keys = keys(g:plugs)
while !empty(keys)
let keys = keys(s:extend(keys))
endwhile
set nocompatible set nocompatible
filetype off filetype off
for plug in values(g:plugs) for plug in values(g:plugs)
@ -97,9 +103,10 @@ function! plug#end()
endfunction endfunction
function! s:add(...) function! s:add(...)
if a:0 == 1 let force = a:1
let [plugin, branch] = [a:1, 'master'] if a:0 == 2
elseif a:0 == 2 let [plugin, branch] = [a:2, 'master']
elseif a:0 == 3
let [plugin, branch] = a:000 let [plugin, branch] = a:000
else else
echoerr "Invalid number of arguments (1..2)" echoerr "Invalid number of arguments (1..2)"
@ -116,6 +123,8 @@ function! s:add(...)
endif endif
let name = substitute(split(plugin, '/')[-1], '\.git$', '', '') let name = substitute(split(plugin, '/')[-1], '\.git$', '', '')
if !force && has_key(g:plugs, name) | return | endif
let dir = fnamemodify(join([g:plug_home, name], '/'), ':p') let dir = fnamemodify(join([g:plug_home, name], '/'), ':p')
let spec = { 'dir': dir, 'uri': uri, 'branch': branch } let spec = { 'dir': dir, 'uri': uri, 'branch': branch }
@ -229,47 +238,79 @@ function! s:update_impl(pull, args)
call s:finish() call s:finish()
endfunction endfunction
function! s:update_serial(pull) function! s:extend(names)
let st = reltime() let prev = copy(g:plugs)
let base = g:plug_home try
let cnt = 0 command! -nargs=+ Plug call s:add(0, <args>)
let total = len(g:plugs) for name in a:names
let spec = g:plugs[name]
let plugfile = spec.dir . '/'. s:plug_file
if filereadable(plugfile)
execute "source ". plugfile
endif
endfor
finally
command! -nargs=+ Plug call s:add(1, <args>)
endtry
return filter(copy(g:plugs), '!has_key(prev, v:key)')
endfunction
for [name, spec] in items(g:plugs) function! s:update_progress(cnt, total)
let cnt += 1 call setline(1, "Updating plugins (".a:cnt."/".a:total.")")
let d = shellescape(spec.dir) call s:progress_bar(2, a:cnt, a:total)
if isdirectory(spec.dir) normal! 2G
execute 'cd '.spec.dir redraw
if s:git_valid(spec, 0) endfunction
let result = a:pull ?
\ s:system( function! s:update_serial(pull)
\ printf('git checkout -q %s && git pull origin %s 2>&1', let st = reltime()
\ spec.branch, spec.branch)) : 'Already installed' let base = g:plug_home
let error = a:pull ? v:shell_error != 0 : 0 let todo = copy(g:plugs)
let total = len(todo)
let done = {}
while !empty(todo)
for [name, spec] in items(todo)
let done[name] = 1
let d = shellescape(spec.dir)
if isdirectory(spec.dir)
execute 'cd '.spec.dir
if s:git_valid(spec, 0)
let result = a:pull ?
\ s:system(
\ printf('git checkout -q %s && git pull origin %s 2>&1',
\ spec.branch, spec.branch)) : 'Already installed'
let error = a:pull ? v:shell_error != 0 : 0
else
let result = "PlugClean required. Invalid remote."
let error = 1
endif
else else
let result = "PlugClean required. Invalid remote." if !isdirectory(base)
let error = 1 call mkdir(base, 'p')
endif
execute 'cd '.base
let result = s:system(
\ printf('git clone --recursive %s -b %s %s 2>&1',
\ shellescape(spec.uri), shellescape(spec.branch), d))
let error = v:shell_error != 0
endif endif
cd -
if error
let result = '(x) ' . result
endif
call append(3, '- ' . name . ': ' . result)
call s:update_progress(len(done), total)
endfor
if !empty(s:extend(keys(todo)))
let todo = filter(copy(g:plugs), '!has_key(done, v:key)')
let total += len(todo)
call s:update_progress(len(done), total)
else else
if !isdirectory(base) break
call mkdir(base, 'p')
endif
execute 'cd '.base
let result = s:system(
\ printf('git clone --recursive %s -b %s %s 2>&1',
\ shellescape(spec.uri), shellescape(spec.branch), d))
let error = v:shell_error != 0
endif endif
cd - endwhile
if error
let result = '(x) ' . result
endif
call setline(1, "Updating plugins (".cnt."/".total.")")
call s:progress_bar(2, cnt, total)
call append(3, '- ' . name . ': ' . result)
normal! 2G
redraw
endfor
call setline(1, "Updated. Elapsed time: " . split(reltimestr(reltime(st)))[0] . ' sec.') call setline(1, "Updated. Elapsed time: " . split(reltimestr(reltime(st)))[0] . ' sec.')
endfunction endfunction
@ -282,22 +323,32 @@ function! s:update_parallel(pull, threads)
cd = VIM::evaluate('s:is_win').to_i == 1 ? 'cd /d' : 'cd' cd = VIM::evaluate('s:is_win').to_i == 1 ? 'cd /d' : 'cd'
pull = VIM::evaluate('a:pull').to_i == 1 pull = VIM::evaluate('a:pull').to_i == 1
base = VIM::evaluate('g:plug_home') base = VIM::evaluate('g:plug_home')
all = VIM::evaluate('g:plugs') all = VIM::evaluate('copy(g:plugs)')
total = all.length done = {}
cnt = 0
skip = 'Already installed' skip = 'Already installed'
mtx = Mutex.new mtx = Mutex.new
take1 = proc { mtx.synchronize { all.shift } } take1 = proc { mtx.synchronize { all.shift } }
log = proc { |name, result, ok| logh = proc {
mtx.synchronize { cnt, tot = done.length, VIM::evaluate('len(g:plugs)')
$curbuf[1] = "Updating plugins (#{cnt}/#{tot})"
$curbuf[2] = '[' + ('=' * cnt).ljust(tot) + ']'
VIM::command('normal! 2G')
VIM::command('redraw')
}
log = proc { |name, result, ok|
mtx.synchronize do
done[name] = true
result = '(x) ' + result unless ok result = '(x) ' + result unless ok
result = "- #{name}: #{result}" result = "- #{name}: #{result}"
$curbuf[1] = "Updating plugins (#{cnt += 1}/#{total})"
$curbuf[2] = '[' + ('=' * cnt).ljust(total) + ']'
$curbuf.append 3, result $curbuf.append 3, result
VIM::command('normal! 2G') logh.call
VIM::command('redraw') end
} }
refill = proc { |name|
mtx.synchronize do
all.merge! VIM::evaluate("s:extend(['#{name}'])")
logh.call
end
} }
VIM::evaluate('a:threads').to_i.times.map { |i| VIM::evaluate('a:threads').to_i.times.map { |i|
Thread.new(i) do |ii| Thread.new(i) do |ii|
@ -323,6 +374,7 @@ function! s:update_parallel(pull, threads)
end end
result = result.lines.to_a.last.strip result = result.lines.to_a.last.strip
log.call name, result, ok log.call name, result, ok
refill.call name
end end
end end
}.each(&:join) }.each(&:join)