|
| 1 | +" autoload/laravel/homestead.vim - Laravel Homestead support for Vim |
| 2 | +" Maintainer: Noah Frederick |
| 3 | + |
| 4 | +"" |
| 5 | +" The directory where Homestead is installed. |
| 6 | +let s:dir = get(g:, 'laravel_homestead_dir', '~/Homestead') |
| 7 | +let s:yaml = s:dir . '/Homestead.yaml' |
| 8 | +let s:json = s:dir . '/Homestead.json' |
| 9 | + |
| 10 | +"" |
| 11 | +" Get Dict from JSON {expr}. |
| 12 | +function! s:json_decode(expr) abort |
| 13 | + try |
| 14 | + if exists('*json_decode') |
| 15 | + let expr = type(a:expr) == type([]) ? join(a:expr, "\n") : a:expr |
| 16 | + return json_decode(expr) |
| 17 | + else |
| 18 | + return projectionist#json_parse(a:expr) |
| 19 | + endif |
| 20 | + catch /^Vim\%((\a\+)\)\=:E474/ |
| 21 | + call laravel#error('Homestead.json cannot be parsed') |
| 22 | + catch /^invalid JSON/ |
| 23 | + call laravel#error('Homestead.json cannot be parsed') |
| 24 | + catch /^Vim\%((\a\+)\)\=:E117/ |
| 25 | + call laravel#error('projectionist is not available') |
| 26 | + endtry |
| 27 | + return {} |
| 28 | +endfunction |
| 29 | + |
| 30 | +"" |
| 31 | +" Get path to current project on the Homestead VM. |
| 32 | +function! laravel#homestead#root(app_root) abort |
| 33 | + if !filereadable(s:json) |
| 34 | + call laravel#error('Homestead.json cannot be read: ' |
| 35 | + \ . s:json . ' (set g:laravel_homestead_dir)') |
| 36 | + return '' |
| 37 | + endif |
| 38 | + |
| 39 | + let config = s:json_decode(readfile(s:json)) |
| 40 | + |
| 41 | + for folder in get(config, 'folders', []) |
| 42 | + let source = expand(folder.map) |
| 43 | +
|
| 44 | + if a:app_root . '/' =~# '^' . source . '/' |
| 45 | + return substitute(a:app_root, '^' . source, folder.to, '') |
| 46 | + endif |
| 47 | + endfor |
| 48 | + |
| 49 | + return '' |
| 50 | +endfunction |
| 51 | + |
| 52 | +"" |
| 53 | +" Change working directory to {dir}, respecting current window's local dir |
| 54 | +" state. Returns old working directory to be restored later by a second |
| 55 | +" invocation of the function. |
| 56 | +function! s:cd(dir) abort |
| 57 | + let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd' |
| 58 | + let cwd = getcwd() |
| 59 | + execute cd fnameescape(a:dir) |
| 60 | + return cwd |
| 61 | +endfunction |
| 62 | + |
| 63 | +"" |
| 64 | +" Build SSH shell command from command-line arguments. |
| 65 | +function! s:ssh(args) abort |
| 66 | + if empty(a:args) |
| 67 | + return 'vagrant ssh' |
| 68 | + endif |
| 69 | + |
| 70 | + let root = laravel#app().homestead_path() |
| 71 | + |
| 72 | + if empty(root) |
| 73 | + call laravel#error('Homestead site not configured for ' |
| 74 | + \ . laravel#app().path()) |
| 75 | + return '' |
| 76 | + endif |
| 77 | + |
| 78 | + let args = insert(a:args, 'cd ' . fnamemodify(root, ':S') . ' &&') |
| 79 | + return 'vagrant ssh -- ' . shellescape(join(args)) |
| 80 | +endfunction |
| 81 | + |
| 82 | +"" |
| 83 | +" Build Vagrant shell command from command-line arguments. |
| 84 | +function! s:vagrant(args) abort |
| 85 | + let args = empty(a:args) ? ['status'] : a:args |
| 86 | + return 'vagrant ' . join(args) |
| 87 | +endfunction |
| 88 | + |
| 89 | +"" |
| 90 | +" The :Homestead command. |
| 91 | +function! laravel#homestead#exec(...) abort |
| 92 | + let args = copy(a:000) |
| 93 | + let vagrant = remove(args, 0) |
| 94 | + |
| 95 | + if !isdirectory(s:dir) |
| 96 | + return laravel#error('Homestead directory does not exist: ' |
| 97 | + \ . s:dir . ' (set g:laravel_homestead_dir)') |
| 98 | + endif |
| 99 | + |
| 100 | + let cmdline = vagrant ==# '!' ? s:vagrant(args) : s:ssh(args) |
| 101 | + |
| 102 | + if empty(cmdline) |
| 103 | + " There is no path configured for the VM. |
| 104 | + return '' |
| 105 | + endif |
| 106 | + |
| 107 | + if exists(':Start') |
| 108 | + execute 'Start -title=homestead -wait=always -dir='.fnameescape(s:dir) cmdline |
| 109 | + elseif exists(':terminal') |
| 110 | + tab split |
| 111 | + execute 'lcd' fnameescape(s:dir) |
| 112 | + execute 'terminal' cmdline |
| 113 | + else |
| 114 | + let cwd = s:cd(s:dir) |
| 115 | + execute '!' . cmdline |
| 116 | + call s:cd(cwd) |
| 117 | + endif |
| 118 | + |
| 119 | + return '' |
| 120 | +endfunction |
| 121 | + |
| 122 | +"" |
| 123 | +" @private |
| 124 | +" Hack for testing script-local functions. |
| 125 | +function! laravel#homestead#sid() |
| 126 | + nnoremap <SID> <SID> |
| 127 | + return maparg('<SID>', 'n') |
| 128 | +endfunction |
| 129 | + |
| 130 | +" vim: fdm=marker:sw=2:sts=2:et |
0 commit comments