1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-02 20:30:05 +08:00

Merge pull request #460 from SpaceVim/job

Use job in neovim or vim8
This commit is contained in:
Wang Shidong 2017-04-22 16:20:46 +08:00 committed by GitHub
commit 096a72df53
4 changed files with 169 additions and 70 deletions

View File

@ -6,10 +6,12 @@ endfunction
let s:self = {}
let s:self.jobs = {}
let s:self.nvim_job = has('nvim')
let s:self.vim_job = !has('nvim') && has('job') && has('patch-7.4.1590')
let s:self.vim_job = !has('nvim') && has('job') && has('patch-8.0.0027')
let s:self.vim_co = SpaceVim#api#import('vim#compatible')
function! s:self.warn(...) abort
if len(a:000) == 0
echohl WarningMsg | echom 'Current version do not support job feature!' | echohl None
echohl WarningMsg | echom 'Current version do not support job feature, fallback to sync system()' | echohl None
elseif len(a:000) == 1 && type(a:1) == type('')
echohl WarningMsg | echom a:1| echohl None
else
@ -88,7 +90,34 @@ function! s:self.start(argv, ...) abort
call extend(self.jobs, {id : job})
return id
else
call self.warn()
if len(a:000) > 0
let opts = a:1
else
let opts = {}
endif
if has_key(opts, 'cwd')
let old_wd = getcwd()
let cwd = expand(opts.cwd, 1)
exe 'cd' fnameescape(cwd)
endif
let output = self.vim_co.systemlist(a:argv)
if exists('old_wd')
exe 'cd' fnameescape(old_wd)
endif
let id = -1
if v:shell_error
if has_key(opts,'on_stderr')
call call(opts.on_stderr, [id, output, 'stderr'])
endif
else
if has_key(opts,'on_stdout')
call call(opts.on_stdout, [id, output, 'stdout'])
endif
endif
if has_key(opts,'on_exit')
call call(opts.on_exit, [id, v:shell_error, 'exit'])
endif
return id
endif
endfunction

View File

@ -2,6 +2,7 @@ function! SpaceVim#api#vim#compatible#get() abort
return map({
\ 'execute' : '',
\ 'system' : '',
\ 'systemlist' : '',
\ },
\ "function('s:' . v:key)"
\ )
@ -29,6 +30,9 @@ if has('nvim')
function! s:system(cmd, ...) abort
return a:0 == 0 ? system(a:cmd) : system(a:cmd, a:1)
endfunction
function! s:systemlist(cmd, ...) abort
return a:0 == 0 ? systemlist(a:cmd) : systemlist(a:cmd, a:1)
endfunction
else
function! s:system(cmd, ...) abort
if type(a:cmd) == 3
@ -39,6 +43,15 @@ else
return a:0 == 0 ? system(a:cmd) : system(a:cmd, a:1)
endif
endfunction
function! s:systemlist(cmd, ...) abort
if type(a:cmd) == 3
let cmd = map(a:cmd, 'shellescape(v:val)')
let cmd = join(cmd, ' ')
return a:0 == 0 ? systemlist(cmd) : systemlist(cmd, a:1)
else
return a:0 == 0 ? systemlist(a:cmd) : systemlist(a:cmd, a:1)
endif
endfunction
endif
" vim:set et sw=2 cc=80:

View File

@ -6,9 +6,6 @@
" License: MIT license
"=============================================================================
" TODO add airline support for plugin manager
" TODO add install function for plugin manager
" Load SpaceVim api
let s:VIM_CO = SpaceVim#api#import('vim#compatible')
let s:JOB = SpaceVim#api#import('job')
@ -23,6 +20,7 @@ let s:building_repos = {}
let s:ui_buf = {}
let s:plugin_manager_buffer = 0
let s:plugin_manager_buffer_lines = []
let s:jobpid = 0
" install plugin manager
function! s:install_manager() abort
@ -168,8 +166,12 @@ function! SpaceVim#plugins#manager#update(...) abort
elseif status == 1
return
endif
redraw!
let s:pct = 0
let s:pct_done = 0
if exists('s:recache_done')
unlet s:recache_done
endif
let s:plugins = a:0 == 0 ? sort(keys(dein#get())) : sort(copy(a:1))
if a:0 == 0
call add(s:plugins, 'SpaceVim')
@ -217,19 +219,27 @@ endfunction
" here if a:data == 0, git pull succeed
function! s:on_pull_exit(id, data, event) abort
if a:data == 0 && a:event ==# 'exit'
call s:msg_on_updated_done(s:pulling_repos[a:id].name)
if a:id == -1
let id = s:jobpid
else
call s:msg_on_updated_failed(s:pulling_repos[a:id].name)
let id = a:id
endif
if get(s:pulling_repos[a:id], 'build', '') !=# ''
call s:build(s:pulling_repos[a:id])
if a:data == 0 && a:event ==# 'exit'
call s:msg_on_updated_done(s:pulling_repos[id].name)
else
call s:msg_on_updated_failed(s:pulling_repos[id].name)
endif
if a:id == -1
redraw!
endif
if get(s:pulling_repos[id], 'build', '') !=# ''
call s:build(s:pulling_repos[id])
else
let s:pct_done += 1
call s:set_buf_line(s:plugin_manager_buffer, 1, 'Updating plugins (' . s:pct_done . '/' . s:total . ')')
call s:set_buf_line(s:plugin_manager_buffer, 2, s:status_bar())
endif
call remove(s:pulling_repos, string(a:id))
call remove(s:pulling_repos, string(id))
if !empty(s:plugins)
let name = s:LIST.shift(s:plugins)
if name ==# 'SpaceVim'
@ -242,7 +252,12 @@ function! s:on_pull_exit(id, data, event) abort
endif
call s:pull(repo)
endif
if empty(s:pulling_repos) && empty(s:building_repos)
call s:recache_rtp(a:id)
endfunction
function! s:recache_rtp(id) abort
if empty(s:pulling_repos) && empty(s:building_repos) && !exists('s:recache_done')
" TODO add elapsed time info.
call s:set_buf_line(s:plugin_manager_buffer, 1, 'Updated. Elapsed time: '
\ . split(reltimestr(reltime(s:start_time)))[0] . ' sec.')
@ -250,16 +265,24 @@ function! s:on_pull_exit(id, data, event) abort
if g:spacevim_plugin_manager ==# 'dein'
call dein#recache_runtimepath()
endif
if a:id == -1
let s:recache_done = 1
endif
endif
endfunction
" @vimlint(EVL103, 1, a:event)
function! s:on_install_stdout(id, data, event) abort
if a:id == -1
let id = s:jobpid
else
let id = a:id
endif
for str in a:data
let status = matchstr(str,'\d\+%\s(\d\+/\d\+)')
if !empty(status)
call s:msg_on_install_process(s:pulling_repos[a:id].name, status)
call s:msg_on_install_process(s:pulling_repos[id].name, status)
endif
endfor
endfunction
@ -271,68 +294,73 @@ function! s:lock_revision(repo) abort
endfunction
function! s:on_build_exit(id, data, event) abort
if a:data == 0 && a:event ==# 'exit'
call s:msg_on_build_done(s:building_repos[a:id].name)
if a:id == -1
let id = s:jobpid
else
call s:msg_on_build_failed(s:building_repos[a:id].name)
let id = a:id
endif
if a:data == 0 && a:event ==# 'exit'
call s:msg_on_build_done(s:building_repos[id].name)
else
call s:msg_on_build_failed(s:building_repos[id].name)
endif
let s:pct_done += 1
call s:set_buf_line(s:plugin_manager_buffer, 1, 'Updating plugins (' . s:pct_done . '/' . s:total . ')')
call s:set_buf_line(s:plugin_manager_buffer, 2, s:status_bar())
call remove(s:building_repos, string(a:id))
if empty(s:pulling_repos) && empty(s:building_repos)
" TODO add elapsed time info.
call s:set_buf_line(s:plugin_manager_buffer, 1, 'Installed. Elapsed time: '
\ . split(reltimestr(reltime(s:start_time)))[0] . ' sec.')
let s:plugin_manager_buffer = 0
if g:spacevim_plugin_manager ==# 'dein'
call dein#recache_runtimepath()
endif
endif
call remove(s:building_repos, string(id))
call s:recache_rtp(a:id)
endfunction
" here if a:data == 0, git pull succeed
function! s:on_install_exit(id, data, event) abort
if a:data == 0 && a:event ==# 'exit'
call s:msg_on_install_done(s:pulling_repos[a:id].name)
if a:id == -1
let id = s:jobpid
else
call s:msg_on_install_failed(s:pulling_repos[a:id].name)
let id = a:id
endif
if get(s:pulling_repos[a:id], 'rev', '') !=# ''
call s:lock_revision(s:pulling_repos[a:id])
if a:data == 0 && a:event ==# 'exit'
call s:msg_on_install_done(s:pulling_repos[id].name)
else
call s:msg_on_install_failed(s:pulling_repos[id].name)
endif
if get(s:pulling_repos[a:id], 'build', '') !=# ''
call s:build(s:pulling_repos[a:id])
if get(s:pulling_repos[id], 'rev', '') !=# ''
call s:lock_revision(s:pulling_repos[id])
endif
if get(s:pulling_repos[id], 'build', '') !=# ''
call s:build(s:pulling_repos[id])
else
let s:pct_done += 1
call s:set_buf_line(s:plugin_manager_buffer, 1, 'Updating plugins (' . s:pct_done . '/' . s:total . ')')
call s:set_buf_line(s:plugin_manager_buffer, 2, s:status_bar())
endif
call remove(s:pulling_repos, string(a:id))
call remove(s:pulling_repos, string(id))
if !empty(s:plugins)
call s:install(dein#get(s:LIST.shift(s:plugins)))
endif
if empty(s:pulling_repos) && empty(s:building_repos)
" TODO add elapsed time info.
call s:set_buf_line(s:plugin_manager_buffer, 1, 'Installed. Elapsed time: '
\ . split(reltimestr(reltime(s:start_time)))[0] . ' sec.')
let s:plugin_manager_buffer = 0
if g:spacevim_plugin_manager ==# 'dein'
call dein#recache_runtimepath()
endif
endif
call s:recache_rtp(a:id)
endfunction
function! s:pull(repo) abort
let s:pct += 1
let s:ui_buf[a:repo.name] = s:pct
let argv = ['git', '-C', a:repo.path, 'pull']
let jobid = s:JOB.start(argv,{
\ 'on_exit' : function('s:on_pull_exit')
\ })
if jobid != 0
let s:pulling_repos[jobid] = a:repo
if s:JOB.vim_job || s:JOB.nvim_job
let jobid = s:JOB.start(argv,{
\ 'on_exit' : function('s:on_pull_exit')
\ })
if jobid != 0
let s:pulling_repos[jobid] = a:repo
call s:msg_on_start(a:repo.name)
endif
else
let s:jobpid += 1
let s:pulling_repos[s:jobpid] = a:repo
call s:msg_on_start(a:repo.name)
redraw!
call s:JOB.start(argv,{
\ 'on_exit' : function('s:on_pull_exit')
\ })
endif
endfunction
@ -341,25 +369,48 @@ function! s:install(repo) abort
let s:ui_buf[a:repo.name] = s:pct
let url = 'https://github.com/' . a:repo.repo
let argv = ['git', 'clone', '--progress', url, a:repo.path]
let jobid = s:JOB.start(argv,{
\ 'on_stderr' : function('s:on_install_stdout'),
\ 'on_exit' : function('s:on_install_exit')
\ })
if jobid != 0
let s:pulling_repos[jobid] = a:repo
call s:msg_on_install_start(a:repo.name)
if s:JOB.vim_job || s:JOB.nvim_job
let jobid = s:JOB.start(argv,{
\ 'on_stderr' : function('s:on_install_stdout'),
\ 'on_exit' : function('s:on_install_exit')
\ })
if jobid != 0
let s:pulling_repos[jobid] = a:repo
call s:msg_on_install_start(a:repo.name)
endif
else
let s:jobpid += 1
let s:pulling_repos[s:jobpid] = a:repo
call s:msg_on_start(a:repo.name)
redraw!
call s:JOB.start(argv,{
\ 'on_stderr' : function('s:on_install_stdout'),
\ 'on_exit' : function('s:on_install_exit')
\ })
endif
endfunction
function! s:build(repo) abort
let argv = type(a:repo.build) != 4 ? a:repo.build : s:get_build_argv(a:repo.build)
let jobid = s:JOB.start(argv,{
\ 'on_exit' : function('s:on_build_exit'),
\ 'cwd' : a:repo.path,
\ })
if jobid != 0
let s:building_repos[jobid] = a:repo
if s:JOB.vim_job || s:JOB.nvim_job
let jobid = s:JOB.start(argv,{
\ 'on_exit' : function('s:on_build_exit'),
\ 'cwd' : a:repo.path,
\ })
if jobid != 0
let s:building_repos[jobid] = a:repo
call s:msg_on_build_start(a:repo.name)
endif
else
let s:building_repos[s:jobpid] = a:repo
call s:msg_on_build_start(a:repo.name)
redraw!
call s:JOB.start(argv,{
\ 'on_exit' : function('s:on_build_exit'),
\ 'cwd' : a:repo.path,
\ })
endif
endfunction

View File

@ -3,7 +3,7 @@ Execute ( SpaceVim api: job ):
let argv = ['cat']
let g:stdout = ''
let stderr = ''
let exit = 1
let exit_data = 1
function! s:on_stdout(id, data, event) abort
if a:event ==# 'stdout'
for a in a:data
@ -12,7 +12,7 @@ Execute ( SpaceVim api: job ):
endif
endfor
elseif a:event ==# 'exit'
let g:exit = a:data
let g:exit_data = a:data
else
let g:stderr = a:data
endif
@ -23,11 +23,17 @@ Execute ( SpaceVim api: job ):
\ 'on_exit' : function('s:on_stdout'),
\ }
let jobid = job.start(argv,opt)
call job.send(jobid, 'foo')
sleep 10m
AssertEqual stdout, 'foo'
AssertEqual job.status(jobid), 'run'
call job.stop(jobid)
AssertEqual exit, 1
if jobid >= 0
call job.send(jobid, 'foo')
sleep 10m
AssertEqual stdout, 'foo'
AssertEqual job.status(jobid), 'run'
call job.stop(jobid)
AssertEqual exit_data, 1
else
let jobid = job.start(['echo', 'foo'],opt)
AssertEqual stdout, 'foo'
AssertEqual exit_data, 0
endif