Skip to content

Commit 9c0d8db

Browse files
committed
WIP homestead command
1 parent b3544cc commit 9c0d8db

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

autoload/laravel.vim

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,14 @@ function! s:app_views_path(...) dict abort
134134
return join([self._root, 'resources/views'] + a:000, '/')
135135
endfunction
136136

137+
""
138+
" Get path to source root in Homestead VM.
139+
function! s:app_homestead_root() dict abort
140+
return laravel#homestead#root(self._root)
141+
endfunction
142+
137143
call s:add_methods('app', ['glob', 'has_dir', 'has_file', 'has_path'])
138-
call s:add_methods('app', ['path', 'src_path', 'config_path', 'migrations_path', 'find_migration', 'expand_migration', 'views_path'])
144+
call s:add_methods('app', ['path', 'src_path', 'config_path', 'migrations_path', 'find_migration', 'expand_migration', 'views_path', 'homestead_root'])
139145

140146
""
141147
" Detect app's namespace
@@ -468,6 +474,21 @@ function! laravel#buffer_commands() abort
468474
" Invoke Artisan with [arguments] (with intelligent completion).
469475
command! -buffer -bang -bar -nargs=* -complete=customlist,laravel#artisan#complete
470476
\ Artisan execute laravel#artisan#exec(<q-bang>, <f-args>)
477+
478+
""
479+
" @command Homestead {cmd}
480+
" Invoke shell {cmd} on the Homestead VM over SSH.
481+
"
482+
" @command Homestead
483+
" Start an interactive SSH session on the Homestead VM.
484+
"
485+
" @command Homestead! [arguments]
486+
" Invoke Vagrant with [arguments] in the context of the Homestead directory
487+
" on the host machine. For example, to start the VM: >
488+
" :Homestead! up
489+
" <
490+
command! -buffer -bang -bar -nargs=* -complete=shellcmd
491+
\ Homestead execute laravel#homestead#exec(<q-bang>, <f-args>)
471492
endfunction
472493

473494
""

autoload/laravel/homestead.vim

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
" autoload/laravel/homestead.vim - Laravel Homestead support for Vim
2+
" Maintainer: Noah Frederick
3+
4+
""
5+
" @setting g:laravel_homestead_dir
6+
" The directory where Homestead is installed.
7+
let s:dir = get(g:, 'laravel_homestead_dir', '~/Homestead')
8+
let s:json = s:dir . '/Homestead.json'
9+
10+
""
11+
" Change working directory to {dir}, respecting current window's local dir
12+
" state. Returns old working directory to be restored later by a second
13+
" invocation of the function.
14+
function! s:cd(dir) abort
15+
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd'
16+
let cwd = getcwd()
17+
execute cd fnameescape(a:dir)
18+
return cwd
19+
endfunction
20+
21+
""
22+
" Escape arguments to be passed to ssh.
23+
function! s:prepare_args(args) abort
24+
return shellescape(join(a:args))
25+
endfunction
26+
27+
""
28+
" Build SSH shell command from command-line arguments.
29+
function! s:ssh(args) abort
30+
let root = laravel#app().homestead_root()
31+
32+
if empty(root)
33+
echoerr 'Homestead site not configured for ' . laravel#app().path()
34+
endif
35+
36+
if empty(a:args)
37+
return 'vagrant ssh'
38+
endif
39+
40+
let args = insert(a:args, 'cd ' . root . ' &&')
41+
return 'vagrant ssh -- ' . s:prepare_args(args)
42+
endfunction
43+
44+
""
45+
" Build Vagrant shell command from command-line arguments.
46+
function! s:vagrant(args) abort
47+
if empty(a:args)
48+
return 'vagrant status'
49+
endif
50+
51+
return 'vagrant ' . join(a:args)
52+
endfunction
53+
54+
""
55+
" The :Homestead command.
56+
function! laravel#homestead#exec(...) abort
57+
let args = copy(a:000)
58+
let vagrant = remove(args, 0)
59+
60+
if !isdirectory(s:dir)
61+
echoerr 'Homestead directory does not exist: ' . s:dir
62+
\ . ' (set g:laravel_homestead_dir)'
63+
endif
64+
65+
let cmdline = vagrant ==# '!' ? s:vagrant(args) : s:ssh(args)
66+
67+
if exists(':terminal')
68+
tabedit %
69+
execute 'lcd' fnameescape(s:dir)
70+
execute 'terminal' cmdline
71+
else
72+
let cwd = s:cd(s:dir)
73+
execute '!' . cmdline
74+
call s:cd(cwd)
75+
endif
76+
77+
return ''
78+
endfunction
79+
80+
function! laravel#homestead#root(app_root) abort
81+
" TODO: Read homestead.json configuration file instead:
82+
return get(get(g:, 'laravel_homestead_sites', {}), a:app_root, '')
83+
endfunction
84+
85+
""
86+
" @private
87+
" Hack for testing script-local functions.
88+
function! laravel#homestead#sid()
89+
nnoremap <SID> <SID>
90+
return maparg('<SID>', 'n')
91+
endfunction
92+
93+
" vim: fdm=marker:sw=2:sts=2:et

doc/laravel.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Some features include:
1515

1616
* The |:Artisan| command wraps artisan with intelligent completion.
1717
* Includes projections for projectionist.vim.
18+
* Use |:Homestead| to send commands over SSH to your development VM.
1819
* Use |:Console| to fire up a REPL (artisan tinker).
1920

2021
This plug-in is only available if 'compatible' is not set.
@@ -25,6 +26,21 @@ COMMANDS *laravel-commands*
2526
:Artisan[!] [arguments] *:Artisan*
2627
Invoke Artisan with [arguments] (with intelligent completion).
2728

29+
:Homestead {cmd} *:Homestead*
30+
Invoke shell {cmd} on the Homestead VM over SSH.
31+
32+
33+
:Homestead
34+
Start an interactive SSH session on the Homestead VM.
35+
36+
37+
:Homestead! [arguments]
38+
Invoke Vagrant with [arguments] in the context of the Homestead directory on
39+
the host machine. For example, to start the VM:
40+
>
41+
:Homestead! up
42+
<
43+
2844
==============================================================================
2945
ABOUT *laravel-about*
3046

plugin/laravel.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"
1010
" * The |:Artisan| command wraps artisan with intelligent completion.
1111
" * Includes projections for projectionist.vim.
12+
" * Use |:Homestead| to send commands over SSH to your development VM.
1213
" * Use |:Console| to fire up a REPL (artisan tinker).
1314
"
1415
" This plug-in is only available if 'compatible' is not set.

0 commit comments

Comments
 (0)