10
10
11
11
" {{{ Init
12
12
13
- if v: version < ' 800' " {{{
13
+ if v: version < ' 800'
14
14
function ! s: DidNotLoad ()
15
15
echohl WarningMsg |echomsg " vim-build-in-docker unavailable: requires Vim 8.0+" |echohl None
16
16
endfunction
17
17
command ! -nargs =* RunInDocker call s: DidNotLoad ()
18
18
finish
19
- endif " }}}
19
+ endif
20
20
21
21
" TODO: Check docker is available.
22
22
@@ -46,19 +46,68 @@ let s:plugin_path = escape(expand('<sfile>:p:h'), '\')
46
46
function ! s: GetUserID ()
47
47
" return 1000
48
48
let l: output = system (' id -u' )
49
- " return substitute(l:output, '^\s*\(.\{-}\)\s*$', '\1', '')
49
+ " Filter only numbers (id -u will returns also \n).
50
50
return matchstr (l: output ,' [0-9]*' )
51
51
endfunction
52
52
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
56
61
endfunction
57
62
63
+
58
64
" }}}
59
65
60
66
" {{{ 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
+
62
111
63
112
function ! s: GetDockerAddHosts ()
64
113
" Split arguments
@@ -82,14 +131,13 @@ function! s:DockerCmdLine(container, pwd, uid)
82
131
let l: cmdX = ' docker run --rm -u ' . a: uid . ' --volume "' . a: pwd . ' :' . a: pwd . ' "'
83
132
" TODO: Add g:hosts and volumes.
84
133
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 . ' '
86
135
return l: cmd
87
136
endfunction
88
137
89
- function ! s: RunInDocker (container, command )
138
+ function ! s: RunInDocker (opts, container, command )
90
139
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 )
93
141
let l: cmd = l: dockerPart . a: command
94
142
echom l: cmd
95
143
if exists (' *asyncrun#run' )
@@ -99,11 +147,39 @@ function! s:RunInDocker(container, command)
99
147
endif
100
148
endfunction
101
149
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
102
173
103
174
" {{{ Misc
104
175
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 ])
107
183
endfunction " }}}
108
184
109
185
" }}}
0 commit comments