Skip to content

Commit d777253

Browse files
authored
Merge pull request #4 from zanfire/GH-1
GH-1: Initial support of -cwd option in the RunInDocker command.
2 parents 9b076f2 + af4274e commit d777253

File tree

3 files changed

+102
-14
lines changed

3 files changed

+102
-14
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ Run a command in a container
3232
:RunInDocker container1 make -C build
3333
```
3434

35+
### RunInDocker options
36+
37+
```
38+
:RunInDocker -cwd=<root> container1 make -C build
39+
```
40+
41+
RunInDocker support ```-cwd``` option with these values: <root>, <file>, <cwd>, path
42+
43+
- <root> : guess the root folder.
44+
- <file> : path of the current file
45+
- <cwd> : current working directory
46+
- path : an absolute path
47+
3548
## BETA VERSION
3649

3750
This is a early beta version developed and tested only on Linux.

autoload/build_in_docker.vim

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
"{{{ Init
1212

13-
if v:version < '800'"{{{
13+
if v:version < '800'
1414
function! s:DidNotLoad()
1515
echohl WarningMsg|echomsg "vim-build-in-docker unavailable: requires Vim 8.0+"|echohl None
1616
endfunction
1717
command! -nargs=* RunInDocker call s:DidNotLoad()
1818
finish
19-
endif"}}}
19+
endif
2020

2121
" TODO: Check docker is available.
2222

@@ -46,19 +46,68 @@ let s:plugin_path = escape(expand('<sfile>:p:h'), '\')
4646
function! s:GetUserID()
4747
"return 1000
4848
let l:output = system('id -u')
49-
"return substitute(l:output, '^\s*\(.\{-}\)\s*$', '\1', '')
49+
" Filter only numbers (id -u will returns also \n).
5050
return matchstr(l:output,'[0-9]*')
5151
endfunction
5252

53-
function! s:GetWorkingDir()
54-
let l:base = expand('<sfile>:p:h')
55-
return l:base
53+
"}}}
54+
55+
"{{{ path manipulation functions
56+
57+
function! s:PathJoin(a, b)
58+
" TODO: Add windows
59+
" TODO: Check i there is a /
60+
return a:a . '/' . a:b
5661
endfunction
5762

63+
5864
"}}}
5965

6066
"{{{ build-in-docker utility functions
61-
"
67+
68+
69+
70+
function! s:SearchRoot(basepath, filter)
71+
" Split in tokens
72+
let l:tokens = split(a:basepath, '/')
73+
let l:base = ''
74+
for el in l:tokens
75+
let l:base = s:PathJoin(l:base, el)
76+
for f in a:filter
77+
let l:candidate = s:PathJoin(l:base, f)
78+
echom 'Testing ' . l:candidate
79+
if isdirectory(l:candidate)
80+
return l:base
81+
elseif filereadable(l:candidate)
82+
return l:base
83+
endif
84+
endfor
85+
endfor
86+
return a:basepath
87+
endfunction
88+
89+
function! s:GetWorkingDir(opts)
90+
let l:base = expand('<sfile>:p:h')
91+
let l:optsCount = len(a:opts)
92+
" No options
93+
if l:optsCount == 0
94+
let l:base = s:SearchRoot(l:base, ['.git', '.hg', '.svn', 'CMakeList.txt', 'configure'])
95+
elseif l:optsCount == 1
96+
if a:opts[0] == '-cmd=<root>'
97+
let l:base = s:SearchRoot(l:base, ['.git', '.hg', '.svn', 'CMakeList.txt', 'configure'])
98+
elseif a:opts[0] == '-cmd=<file>'
99+
let l:base = expand('%:p:h')
100+
elseif a:opts[0] == '-cmd=<cwd>'
101+
let l:base = getcwd()
102+
elseif a:opts[0] =~ '-cmd=.*'
103+
let l:base = strpart(a:opts[0], 5)
104+
endif
105+
else
106+
echom "Arghh !!! This is not implemented."
107+
endif
108+
return l:base
109+
endfunction
110+
62111

63112
function! s:GetDockerAddHosts()
64113
" Split arguments
@@ -82,14 +131,13 @@ function! s:DockerCmdLine(container, pwd, uid)
82131
let l:cmdX = 'docker run --rm -u ' . a:uid . ' --volume "' . a:pwd . ':' . a:pwd . '"'
83132
" TODO: Add g:hosts and volumes.
84133
let l:cmd = join([l:cmdX, s:GetDockerAddHosts(), s:GetDockerVolumes()], ' ')
85-
let l:cmd = l:cmd . ' -w ' . a:pwd . ' ' . a:container . ' '
134+
let l:cmd = l:cmd . ' -w "' . a:pwd . '" ' . a:container . ' '
86135
return l:cmd
87136
endfunction
88137

89-
function! s:RunInDocker(container, command)
138+
function! s:RunInDocker(opts, container, command)
90139
let l:uid = s:GetUserID()
91-
let l:wd = s:GetWorkingDir()
92-
let l:dockerPart = s:DockerCmdLine(a:container, l:wd, l:uid)
140+
let l:dockerPart = s:DockerCmdLine(a:container, s:GetWorkingDir(a:opts), l:uid)
93141
let l:cmd = l:dockerPart . a:command
94142
echom l:cmd
95143
if exists('*asyncrun#run')
@@ -99,11 +147,39 @@ function! s:RunInDocker(container, command)
99147
endif
100148
endfunction
101149

150+
function! s:PoppingArgumentsOpts(list)
151+
let l:opts = []
152+
let l:idx = 0
153+
for el in a:list
154+
if el =~ '^-cmd=.*'
155+
call add(l:opts, el)
156+
elseif el =~ '^-perego=.*'
157+
call add(l:opts, el)
158+
else
159+
break
160+
endif
161+
let l:idx = l:idx + 1
162+
endfor
163+
return [ l:opts, a:list[l:idx:] ]
164+
endfunction
165+
166+
function! s:PoppingArgumentsContainer(opts, list)
167+
return [ a:opts, a:list[0], a:list[1:] ]
168+
endfunction
169+
170+
function! s:PoppingArgumentsCommand(opts, container, list)
171+
return [ a:opts, a:container, join(a:list, ' ') ]
172+
endfunction
102173

103174
"{{{ Misc
104175

105-
function! build_in_docker#RunInDocker(container, ...) "{{{
106-
call s:RunInDocker(a:container, join(a:000, ' '))
176+
function! build_in_docker#RunInDocker(...) "{{{
177+
" Pop options, container, commands.
178+
let l:args = s:PoppingArgumentsOpts(a:000)
179+
let l:args = s:PoppingArgumentsContainer(l:args[0], l:args[1])
180+
let l:args = s:PoppingArgumentsCommand(l:args[0], l:args[1], l:args[2])
181+
echom 'Run in docker opts: [' . join(l:args[0], ', ') . '] container: ' . l:args[1] . ' command: ' . l:args[2]
182+
call s:RunInDocker(l:args[0], l:args[1], l:args[2])
107183
endfunction "}}}
108184

109185
"}}}

plugin/build_in_docker.vim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ let loaded_build_in_docker = 1"}}}
1818
"{{{ Exported command.
1919

2020
command! -nargs=* RunInDocker call build_in_docker#RunInDocker(<f-args>)
21-
command! -nargs=0 CMakeInDocker call build_in_docker#CMakeInDocker()
2221

2322
"}}}

0 commit comments

Comments
 (0)