Skip to content
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

Allow arguments for :Go{Install,Update}Binaries #1467

Merged
merged 3 commits into from
Sep 25, 2017
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ IMPROVEMENTS:
let g:go_list_type_commands = {"GoBuild": "quickfix", "GoTest": "locationlist"}
* Show unexpected errors better by expanding newlines and tabs
[[GH-1456]](https://github.com/fatih/vim-go/pull/1456).
* `:GoInstallBinaries` and `:GoUpdateBinaries` can now install/update only the
selected binaries (e.g. `:GoUpdateBinaries guru golint`)
[[GH-1467]](https://github.com/fatih/vim-go/pull/1467).

BUG FIXES:

Expand Down
19 changes: 11 additions & 8 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,18 +472,21 @@ CTRL-t
Show dependencies for the current package.

*:GoInstallBinaries*
:GoInstallBinaries
:GoInstallBinaries [binaries]

Download and Install all necessary Go tool binaries such as `godef`,
`goimports`, `gocode`, etc. under `g:go_bin_path`. Set
|'g:go_get_update'| to disable updating dependencies.
Download and install all necessary Go tool binaries such as `godef`,
`goimports`, `gocode`, etc. under `g:go_bin_path`. If one or more
[binaries] are given then it will only install those. The default is to
update everything.
Set |'g:go_get_update'| to disable updating dependencies.

*:GoUpdateBinaries*
:GoUpdateBinaries
:GoUpdateBinaries [binaries]

Download and Update previously installed Go tool binaries such as `godef`,
`goimports`, `gocode`, etc.. under `g:go_bin_path`. This can be used to
update the necessary Go binaries.
Download and update previously installed Go tool binaries such as `godef`,
`goimports`, `gocode`, etc. under `g:go_bin_path`. If one or more
[binaries] are given then it will only update those. The
default is to update everything.

*:GoImplements*
:GoImplements
Expand Down
85 changes: 55 additions & 30 deletions plugin/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,39 @@ let g:go_loaded_install = 1

" these packages are used by vim-go and can be automatically installed if
" needed by the user with GoInstallBinaries
let s:packages = [
\ "github.com/nsf/gocode",
\ "github.com/alecthomas/gometalinter",
\ "golang.org/x/tools/cmd/goimports",
\ "golang.org/x/tools/cmd/guru",
\ "golang.org/x/tools/cmd/gorename",
\ "github.com/golang/lint/golint",
\ "github.com/rogpeppe/godef",
\ "github.com/kisielk/errcheck",
\ "github.com/jstemmer/gotags",
\ "github.com/klauspost/asmfmt/cmd/asmfmt",
\ "github.com/fatih/motion",
\ "github.com/fatih/gomodifytags",
\ "github.com/zmb3/gogetdoc",
\ "github.com/josharian/impl",
\ "github.com/dominikh/go-tools/cmd/keyify",
\ "github.com/davidrjenni/reftools/cmd/fillstruct",
\ ]
let s:packages = {
\ 'asmfmt': ['github.com/klauspost/asmfmt/cmd/asmfmt'],
\ 'errcheck': ['github.com/kisielk/errcheck'],
\ 'fillstruct': ['github.com/davidrjenni/reftools/cmd/fillstruct'],
\ 'gocode': ['github.com/nsf/gocode', {'windows': '-ldflags -H=windowsgui'}],
\ 'godef': ['github.com/rogpeppe/godef'],
\ 'gogetdoc': ['github.com/zmb3/gogetdoc'],
\ 'goimports': ['golang.org/x/tools/cmd/goimports'],
\ 'golint': ['github.com/golang/lint/golint'],
\ 'gometalinter': ['github.com/alecthomas/gometalinter'],
\ 'gomodifytags': ['github.com/fatih/gomodifytags'],
\ 'gorename': ['golang.org/x/tools/cmd/gorename'],
\ 'gotags': ['github.com/jstemmer/gotags'],
\ 'guru': ['golang.org/x/tools/cmd/guru'],
\ 'impl': ['github.com/josharian/impl'],
\ 'keyify': ['github.com/dominikh/go-tools/cmd/keyify'],
\ 'motion': ['github.com/fatih/motion'],
\ }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want a dictionary here? In every case the key is simply the basename of the first element in the list.

Perhaps this would suffice:

let s:packages = {
    \  'github.com/klauspost/asmfmt/cmd/asmfmt': {}`,
...
    \ 'github.com/nsf/gocode': {'windows: '-ldflags -H=windowsgui'},
...
\  }

Food for thought...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started some work on converting this, but it complicates some of the logic by quite a bit (e.g. for filtering the selected packages). I think that keeping it like this is better (a little bit of redundancy in data is better than complicated logic).

I saved the (non-working) patch here: https://gist.github.com/Carpetsmoker/f2003d47f985734c766dff6b68390717


" These commands are available on any filetypes
command! GoInstallBinaries call s:GoInstallBinaries(-1)
command! GoUpdateBinaries call s:GoInstallBinaries(1)
command! -nargs=* -complete=customlist,s:complete GoInstallBinaries call s:GoInstallBinaries(-1, <f-args>)
command! -nargs=* -complete=customlist,s:complete GoUpdateBinaries call s:GoInstallBinaries(1, <f-args>)
command! -nargs=? -complete=dir GoPath call go#path#GoPath(<f-args>)

fun! s:complete(lead, cmdline, cursor)
return filter(keys(s:packages), 'strpart(v:val, 0, len(a:lead)) == a:lead')
endfun

" GoInstallBinaries downloads and install all necessary binaries stated in the
" packages variable. It uses by default $GOBIN or $GOPATH/bin as the binary
" target install directory. GoInstallBinaries doesn't install binaries if they
" exist, to update current binaries pass 1 to the argument.
function! s:GoInstallBinaries(updateBinaries)
function! s:GoInstallBinaries(updateBinaries, ...)
let err = s:CheckBinaries()
if err != 0
return
Expand Down Expand Up @@ -80,26 +84,47 @@ function! s:GoInstallBinaries(updateBinaries)
let cmd .= "-f "
endif

for pkg in s:packages
let basename = fnamemodify(pkg, ":t")
let binname = "go_" . basename . "_bin"
" Filter packages from arguments (if any).
let l:packages = {}
if a:0 > 0
for l:bin in a:000
let l:pkg = get(s:packages, l:bin, [])
if len(l:pkg) == 0
call go#util#EchoError('unknown binary: ' . l:bin)
return
endif
let l:packages[l:bin] = l:pkg
endfor
else
let l:packages = s:packages
endif

let l:platform = ''
if go#util#IsWin()
let l:platform = 'windows'
endif

for [binary, pkg] in items(l:packages)
let l:importPath = pkg[0]
let l:goGetFlags = len(pkg) > 1 ? get(pkg[1], l:platform, '') : ''

let bin = basename
let binname = "go_" . binary . "_bin"

let bin = binary
if exists("g:{binname}")
let bin = g:{binname}
endif

if !executable(bin) || a:updateBinaries == 1
if a:updateBinaries == 1
echo "vim-go: Updating ". basename .". Reinstalling ". pkg . " to folder " . go_bin_path
echo "vim-go: Updating " . binary . ". Reinstalling ". importPath . " to folder " . go_bin_path
else
echo "vim-go: ". basename ." not found. Installing ". pkg . " to folder " . go_bin_path
echo "vim-go: ". binary ." not found. Installing ". importPath . " to folder " . go_bin_path
endif


let out = go#util#System(cmd . shellescape(pkg))
let out = go#util#System(cmd . l:goGetFlags . shellescape(importPath))
if go#util#ShellError() != 0
echo "Error installing ". pkg . ": " . out
echom "Error installing " . pkgimportPath . ": " . out
endif
endif
endfor
Expand Down