aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/installer.lua
blob: a6789d7d2b3a4e59e184f721717eaba6477ed17e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
local M = {}

local _INSTALLERS = {
    'bashls',
    'cssls',
    'dockerls',
    'eslintls',
    'graphql',
    'html',
    'jsonls',
    'solargraph',
    'sumneko_lua',
    'tsserver',
    'vimls',
    'yamlls',
}

local function escape_quotes(str)
    return string.format("%q", str)
end

local function get_server_installer(server)
    return pcall(require, 'nvim-lsp-installer.installers.' .. server)
end

function M.get_available_servers() return _INSTALLERS end

function M.get_installed_servers()
    local installed_servers = {}
    for _, server in pairs(M.get_available_servers()) do
        local ok, module = get_server_installer(server)
        if not ok then
            vim.api.nvim_err_writeln("Unable to find installer for " .. server)
            goto continue
        end
        if module:is_installed() then
            table.insert(installed_servers, module)
        end
        ::continue::
    end
    return installed_servers
end

function M.get_uninstalled_servers()
    local installed_servers = M.get_installed_servers()
    return vim.tbl_filter(
        function (server)
            return not vim.tbl_contains(installed_servers, server)
        end,
        M.get_available_servers()
    )
end

function M.install(server)
    local ok, installer = get_server_installer(server)
    if not ok then
        return vim.api.nvim_err_writeln("Unable to find installer for " .. server)
    end
    local success, error = pcall(installer.install, installer)
    if not success then
        pcall(installer.uninstall, installer)
        return vim.api.nvim_err_writeln("Failed to install " .. server .. ". Error=" .. vim.inspect(error))
    end
end

function M.uninstall(server)
    local ok, installer = get_server_installer(server)
    if not ok then
        return vim.api.nvim_err_writeln("Unable to find installer for " .. server)
    end
    local success, error = pcall(installer.uninstall, installer)
    if not success then
        vim.api.nvim_err_writeln('Unable to uninstall ' .. server .. '. Error=' .. vim.inspect(error))
        return success
    end
    print("Successfully uninstalled " .. server)
end

function M.get_server_root_path(server)
    return vim.fn.stdpath('data') .. "/lsp_servers/" .. server
end

M.Installer = {}
M.Installer.__index = M.Installer

---@class Installer
function M.Installer:new(opts)
    return setmetatable({
        name = opts.name,
        _install_cmd = opts.install_cmd,
        _root_dir = opts.root_dir,
        _default_options = opts.default_options,
        _pre_install = opts.pre_install,
    }, M.Installer)
end

function M.Installer:setup(opts)
    -- We require the lspconfig server here in order to do it as late as possible.
    -- The reason for this is because once a lspconfig server has been imported, it's
    -- automatically registered with lspconfig and causes it to show up in :LspInfo and whatnot.
    require'lspconfig'[self.name].setup(
        vim.tbl_deep_extend('force', self._default_options, opts)
    )
end

function M.Installer:is_installed()
    return os.execute('test -d ' .. escape_quotes(self._root_dir)) == 0
end

function M.Installer:create_root_dir()
    if os.execute('mkdir -p ' .. escape_quotes(self._root_dir)) ~= 0 then
        error('Could not create LSP server directory ' .. self._root_dir)
    end
end

function M.Installer:install()
    if self._pre_install then
        self._pre_install()
    end

    -- We run uninstall after pre_install because we don't want to
    -- unnecessarily uninstall a server should it no longer pass the
    -- pre_install check.
    self:uninstall()

    self:create_root_dir()

    local shell = vim.o.shell
    vim.o.shell = '/bin/bash'
    vim.cmd [[new]]
    vim.fn.termopen(
        'set -e;\n' .. self._install_cmd,
        {
            cwd = self._root_dir,
            on_exit = function (_, exit_code)
                if exit_code ~= 0 then
                    vim.api.nvim_err_writeln("Installer failed for " .. self.name .. ". Exit code: " .. exit_code)
                    self:uninstall()
                else
                    print("Successfully installed " .. self.name)
                end

            end
        }
    )
    vim.o.shell = shell
    vim.cmd([[startinsert]]) -- so that the buffer tails the term log nicely
end

function M.Installer:uninstall()
    -- giggity
    if os.execute('rm -rf ' .. escape_quotes(self._root_dir)) ~= 0 then
        error('Could not remove LSP server directory ' .. self._root_dir)
    end

end

return M