Skip to content

Changes to auto-download of tools #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions autoload/gopher/system.vim
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ fun! s:init() abort
call gopher#info('installing gopls; this may take a minute')
call s:tool('gopls')
endif
" goimports is similarly useful, but not directly referenced by gopher.vim
if !executable('goimports')
call gopher#info('installing gopls; this may take a minute')
call gopher#info('installing goimports; this may take a minute')
call s:tool('goimports')
endif
endfun

" Setup modules and install all tools.
fun! gopher#system#setup() abort
call s:setup_debug('running with g:gopher_setup flags: %s', get(g:, 'gopher_setup', []))

if !s:download(1)
return
endif
Expand Down Expand Up @@ -256,25 +259,40 @@ fun! s:tool(name) abort
return ''
endif

let l:tool = s:tools[a:name]
let l:bin = s:gobin . '/' . a:name

" We already ran go install and there is a binary.
if l:tool[1] && filereadable(l:bin)
if index(get(g:, 'gopher_setup', []), 'no-auto-install') > -1
return a:name
endif

let l:no_vendor_gobin = index(get(g:, 'gopher_setup', []), 'no-vendor-gobin') > -1
let l:tool = s:tools[a:name]
let l:bin = s:gobin . '/' . a:name

if l:tool[1]
if l:no_vendor_gobin && exepath(a:name)
call s:setup_debug('%s: already in PATH; not doing anything', a:name)
return a:name
endif
if !l:no_vendor_gobin && filereadable(l:bin)
call s:setup_debug('%s: %s already exists; not doing anything', a:name, l:bin)
return a:name
endif
endif

if !s:download(0)
return
endif

try
let l:old_gobin = exists('$GOBIN') ? $GOBIN : -1
let l:old_gomod = exists('$GO111MODULE') ? $GO111MODULE : -1
if !l:no_vendor_gobin
let l:old_gobin = exists('$GOBIN') ? $GOBIN : -1
let $GOBIN = s:gobin
endif

let $GOBIN = s:gobin
let l:old_gomod = exists('$GO111MODULE') ? $GO111MODULE : -1
let $GO111MODULE = 'on' " In case user set to 'off'

call s:setup_debug('%s: running go install %s', a:name, l:tool[0])

let l:out = system(printf('cd %s && go install %s',
\ shellescape(s:gotools), shellescape(l:tool[0])))
if v:shell_error
Expand All @@ -284,7 +302,9 @@ fun! s:tool(name) abort
" Record go install ran.
let s:tools[a:name][1] = 1
finally
call gopher#system#restore_env('GOBIN', l:old_gobin)
if !l:no_vendor_gobin
call gopher#system#restore_env('GOBIN', l:old_gobin)
endif
call gopher#system#restore_env('GO111MODULE', l:old_gomod)
endtry

Expand Down Expand Up @@ -450,5 +470,11 @@ fun! gopher#system#cache(name, ...) abort
return [l:c[1], v:true]
endfun

fun! s:setup_debug(msg, ...) abort
if gopher#has_debug('setup')
call call('gopher#info', ['setup: ' . a:msg] + a:000)
endif
endfun


call s:init() " At end so entire file is parsed.
35 changes: 32 additions & 3 deletions doc/gopher.txt
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,35 @@ COMMANDS *gopher-commands*
Make sure all dependent tools are downloaded and compiled. Normally
this is done automatically after you start Vim so it's not required to
manually run it.
It's may still be useful to run as post-install/update hook, so you
don't have to wait for stuff to install or update on first usage.

Add the `setup` flag to |g:gopher_debug| to show more information
about what it's doing.

You need to set the |filetype| to `go` to run this as a post-install
script, since it's only loaded for Go files; for example: >
vim +'set ft=go' +GoSetup +'q!'
<
*g:gopher_setup* = `[]`
A list of flags to control |:GoSetup| behaviour; allowed
values:

no-auto-install Don't automatically download or
install tools upon first use; it will
be an error if a tool doesn't exist.
This is useful if you run |:GoSetup|
manually after installing and are
_sure_ all the required tools exist
and want a minor performance boost.

no-vendor-gobin By default the tools are installed to
a "vendored" `$GOBIN` in the
gopher.vim directory to ensure 1) the
correct versions are used and 2) to
not interfere with anything else that
might be installed on the system. If
this is disabled it will run a
"normal" `go install` without setting
`$GOBIN`.

:GoDiag[!] {report, clear} *:GoDiag*
Print diagnostic information. Also see |g:gopher_debug|.
Expand Down Expand Up @@ -467,7 +494,9 @@ just a command are documented in the command.

commands Records all shell commands; use |:GoDiag| or
`gopher#system#history()` to view. The last 5 commands are
always saved with limited output.
always saved with limited output irregardless of this flag.

setup Be verbose about what |:GoSetup| does.

*g:gopher_override_vimgo* = (undefined)
Override vim-go; this removes all vim-go commands, autocommands,
Expand Down