go.nvim/lua/go/install.lua

190 lines
4.6 KiB
Lua
Raw Normal View History

2021-03-10 12:15:06 +00:00
local uv = vim.loop
2022-06-01 14:47:00 +00:00
local DIR_SEP = package.config:sub(1, 1)
2022-08-04 13:30:52 +00:00
local utils = require('go.utils')
2022-07-27 04:50:39 +00:00
local log = utils.log
2021-03-10 12:15:06 +00:00
local url = {
2022-08-04 13:30:52 +00:00
gofumpt = 'mvdan.cc/gofumpt',
golines = 'github.com/segmentio/golines',
['golangci-lint'] = 'github.com/golangci/golangci-lint/cmd/golangci-lint',
goimports = 'golang.org/x/tools/cmd/goimports',
gorename = 'golang.org/x/tools/cmd/gorename',
gomodifytags = 'github.com/fatih/gomodifytags',
gopls = 'golang.org/x/tools/gopls',
gotests = 'github.com/cweill/gotests/...',
iferr = 'github.com/koron/iferr',
callgraph = 'golang.org/x/tools/cmd/callgraph',
guru = 'golang.org/x/tools/cmd/guru',
impl = 'github.com/josharian/impl',
fillstruct = 'github.com/davidrjenni/reftools/cmd/fillstruct',
2022-08-04 13:30:52 +00:00
fillswitch = 'github.com/davidrjenni/reftools/cmd/fillswitch',
dlv = 'github.com/go-delve/delve/cmd/dlv',
ginkgo = 'github.com/onsi/ginkgo/v2/ginkgo',
richgo = 'github.com/kyoh86/richgo',
gotestsum = 'gotest.tools/gotestsum',
mockgen = 'go.uber.org/mock/mockgen',
2022-08-04 13:30:52 +00:00
['json-to-struct'] = 'github.com/tmc/json-to-struct',
gomvp = 'github.com/abenz1267/gomvp',
govulncheck = 'golang.org/x/vuln/cmd/govulncheck',
2023-01-12 04:34:00 +00:00
['go-enum'] = 'github.com/abice/go-enum',
gonew = 'golang.org/x/tools/cmd/gonew',
2021-03-10 12:15:06 +00:00
}
2022-01-15 11:14:56 +00:00
local tools = {}
2021-12-06 10:32:23 +00:00
for tool, _ in pairs(url) do
2022-01-15 11:14:56 +00:00
table.insert(tools, tool)
2021-12-06 10:32:23 +00:00
end
2022-06-18 05:43:07 +00:00
local function is_installed(bin)
if utils.installed_tools[bin] then
2023-02-15 20:36:49 +00:00
return true
end
2022-07-27 04:50:39 +00:00
local sep = utils.sep2()
2022-07-27 16:21:10 +00:00
local ext = utils.ext()
if utils.goenv_mode() then
local cwd = vim.fn.getcwd()
2023-10-09 01:12:45 +00:00
local cmd = 'cd ' .. cwd .. ' && goenv which ' .. bin .. ' 2>&1'
local status = os.execute(cmd)
if status == 0 then
utils.installed_tools[bin] = true
return true
end
return false
end
if vim.fn.executable(bin) == 1 then
utils.installed_tools[bin] = true
return true
end
local env_path = os.getenv('PATH')
local base_paths = vim.split(env_path, sep, { trimempty = true })
2022-06-01 11:29:13 +00:00
for _, value in pairs(base_paths) do
2022-07-27 16:21:10 +00:00
if uv.fs_stat(value .. DIR_SEP .. bin .. ext) then
utils.installed_tools[bin] = true
2021-07-14 15:35:38 +00:00
return true
end
2021-07-14 15:35:38 +00:00
end
return false
2021-03-10 12:15:06 +00:00
end
2023-01-19 12:31:00 +00:00
local function go_install_sync(pkg)
local u = url[pkg]
if u == nil then
vim.notify(
'command ' .. pkg .. ' not supported, please update install.lua, or manually install it',
vim.log.levels.WARN
2023-01-19 12:31:00 +00:00
)
return
end
u = u .. '@latest'
local setup = { 'go', 'install', u }
if utils.goenv_mode() then
setup = { 'goenv', 'exec', 'go', 'install', u }
end
2023-01-19 12:31:00 +00:00
local output = vim.fn.system(table.concat(setup, ' '))
if vim.v.shell_error ~= 0 then
vim.notify('install ' .. pkg .. ' failed: ' .. output, vim.log.levels.ERROR)
2023-01-19 12:31:00 +00:00
else
vim.notify(
'install ' .. pkg .. ' installed to ' .. (vim.env['GOBIN'] or vim.fn.system('go env GOBIN')),
vim.log.levels.INFO
)
2023-01-19 12:31:00 +00:00
end
end
-- async install pkg
local function go_install(pkg)
local u = url[pkg]
2021-03-10 12:15:06 +00:00
if u == nil then
2022-01-15 11:14:56 +00:00
vim.notify(
2022-08-04 13:30:52 +00:00
'command ' .. pkg .. ' not supported, please update install.lua, or manually install it',
vim.log.levels.WARN
2022-01-15 11:14:56 +00:00
)
2021-03-10 12:15:06 +00:00
return
end
2021-04-23 06:31:00 +00:00
2022-08-04 13:30:52 +00:00
u = u .. '@latest'
local setup = { 'go', 'install', u }
if utils.goenv_mode() then
setup = { 'goenv', 'exec', 'go', 'install', u }
end
2021-03-10 12:15:06 +00:00
2021-07-14 15:35:38 +00:00
vim.fn.jobstart(setup, {
2022-06-01 11:29:13 +00:00
on_stdout = function(_, data, _)
2021-12-06 10:32:23 +00:00
log(setup)
2022-08-04 13:30:52 +00:00
if type(data) == 'table' and #data > 0 then
data = table.concat(data, ' ')
2021-12-06 10:32:23 +00:00
end
2022-08-04 13:30:52 +00:00
local msg = 'install ' .. u .. ' finished'
2021-12-06 10:32:23 +00:00
if #data > 1 then
msg = msg .. data
end
vim.notify(msg, vim.log.levels.INFO)
2021-12-06 10:32:23 +00:00
end,
2021-07-14 15:35:38 +00:00
})
2021-03-10 12:15:06 +00:00
end
local function install(bin, verbose)
2023-01-19 01:26:14 +00:00
if verbose == nil and _GO_NVIM_CFG then
2022-02-02 04:49:54 +00:00
verbose = _GO_NVIM_CFG.verbose
end
if not is_installed(bin) then
go_install(bin)
2023-10-09 01:12:45 +00:00
vim.notify('installing ' .. bin, vim.log.levels.INFO)
end
return is_installed(bin)
end
local function update(bin)
2021-07-14 15:35:38 +00:00
go_install(bin)
end
2023-01-19 12:46:20 +00:00
local function update_sync(bin)
go_install_sync(bin)
end
2021-03-10 12:15:06 +00:00
local function install_all()
2022-06-01 11:29:13 +00:00
for key, _ in pairs(url) do
2021-03-10 12:15:06 +00:00
install(key)
end
end
2023-01-19 12:31:00 +00:00
local function install_all_sync()
for key, _ in pairs(url) do
if not is_installed(key) then
vim.notify('installing ' .. key, vim.log.levels.INFO)
2023-01-19 12:31:00 +00:00
go_install_sync(key)
end
end
end
2021-12-06 10:32:23 +00:00
local function update_all()
2022-06-01 11:29:13 +00:00
for key, _ in pairs(url) do
2021-12-06 10:32:23 +00:00
update(key)
end
end
2023-01-19 12:46:20 +00:00
local function update_all_sync()
for key, _ in pairs(url) do
update_sync(key)
end
end
2021-12-06 10:32:23 +00:00
return {
install = install,
update = update,
install_all = install_all,
2023-01-19 12:32:15 +00:00
install_all_sync = install_all_sync,
2021-12-06 10:32:23 +00:00
update_all = update_all,
2023-01-19 12:46:20 +00:00
update_all_sync = update_all_sync,
2022-01-15 11:14:56 +00:00
tools = tools,
2021-12-06 10:32:23 +00:00
}