Skip to content

Commit

Permalink
Add tasks options cwd support (#3385)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsdjeg committed Feb 29, 2020
1 parent 3796ceb commit 68f821b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .SpaceVim.d/tasks.toml
@@ -1 +1,6 @@
[file-run]
command = "dir"
isBackground = false
[file-run.options]
cwd = '${workspaceFolder}bin/'

25 changes: 16 additions & 9 deletions autoload/SpaceVim/plugins/runner.vim
Expand Up @@ -56,7 +56,7 @@ endfunction

let s:target = ''

function! s:async_run(runner) abort
function! s:async_run(runner, ...) abort
if type(a:runner) == type('')
" the runner is a string, the %s will be replaced as a file name.
try
Expand All @@ -68,11 +68,12 @@ function! s:async_run(runner) abort
call s:BUFFER.buf_set_lines(s:bufnr, s:lines , s:lines + 3, 0, ['[Running] ' . cmd, '', repeat('-', 20)])
let s:lines += 3
let s:start_time = reltime()
let s:job_id = s:JOB.start(cmd,{
let opts = get(a:000, 0, {})
let s:job_id = s:JOB.start(cmd,extend({
\ 'on_stdout' : function('s:on_stdout'),
\ 'on_stderr' : function('s:on_stderr'),
\ 'on_exit' : function('s:on_exit'),
\ })
\ }, opts))
elseif type(a:runner) ==# type([]) && len(a:runner) ==# 2
" the runner is a list with two items
" the first item is compile cmd, and the second one is running cmd.
Expand Down Expand Up @@ -222,9 +223,10 @@ function! SpaceVim#plugins#runner#open(...) abort
\ 'exit_code' : 0
\ }
let runner = get(a:000, 0, get(s:runners, &filetype, ''))
let opts = get(a:000, 1, {})
if !empty(runner)
call s:open_win()
call s:async_run(runner)
call s:async_run(runner, opts)
call s:update_statusline()
else
let s:selected_language = get(s:, 'selected_language', '')
Expand Down Expand Up @@ -390,13 +392,17 @@ function! SpaceVim#plugins#runner#run_task(task)
if !empty(a:task)
let cmd = get(a:task, 'command', '')
let args = get(a:task, 'args', [])
let opts = get(a:task, 'options', {})
if !empty(args) && !empty(cmd)
let cmd = cmd . ' ' . join(args, ' ')
endif
if !empty(opts) && has_key(opts, 'cwd') && !empty(opts.cwd)
let opts = {'cwd' : opts.cwd}
endif
if isBackground
call s:run_backgroud(cmd)
call s:run_backgroud(cmd, opts)
else
call SpaceVim#plugins#runner#open(cmd)
call SpaceVim#plugins#runner#open(cmd, opts)
endif
endif
endfunction
Expand All @@ -407,10 +413,11 @@ function! s:on_backgroud_exit(job_id, data, event) abort
echo 'task finished with code=' . a:data . ' in ' . s:STRING.trim(reltimestr(s:end_time)) . ' seconds'
endfunction

function! s:run_backgroud(cmd) abort
function! s:run_backgroud(cmd, ...) abort
echo "task running"
let opts = get(a:000, 0, {})
let s:start_time = reltime()
call s:JOB.start(a:cmd,{
call s:JOB.start(a:cmd,extend({
\ 'on_exit' : function('s:on_backgroud_exit'),
\ })
\ }, opts))
endfunction
Expand Down
7 changes: 6 additions & 1 deletion autoload/SpaceVim/plugins/tasks.vim
Expand Up @@ -102,6 +102,11 @@ function! SpaceVim#plugins#tasks#get()
if has_key(task, 'command') && type(task.command) ==# 1
let task.command = s:replace_variables(task.command)
endif
if has_key(task, 'options') && type(task.options) ==# 4
if has_key(task.options, 'cwd') && type(task.options.cwd) ==# 1
let task.options.cwd = s:replace_variables(task.options.cwd)
endif
endif
return task
endfunction

Expand Down Expand Up @@ -158,7 +163,7 @@ function! s:detect_npm_tasks() abort
let detect_task = {}
let conf = {}
if filereadable('package.json')
let conf = s:JSON.json_decode(join(readfile('package.json', ''), ''))
let conf = s:JSON.json_decode(join(readfile('package.json', ''), ''))
endif
if has_key(conf, 'scripts')
for task_name in keys(conf.scripts)
Expand Down

0 comments on commit 68f821b

Please sign in to comment.