Skip to content

Commit

Permalink
feat: ask user which package to download when detection fails
Browse files Browse the repository at this point in the history
  • Loading branch information
pbogut committed Apr 6, 2024
1 parent 9cb52e0 commit 7d5c769
Showing 1 changed file with 62 additions and 27 deletions.
89 changes: 62 additions & 27 deletions lua/magento2_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ local function start_lsp(opts)
end
end

---@return string|nil
local function get_machine()
local machine = vim.loop.os_uname().machine
if machine == 'x86_64' then
return 'x64'
elseif machine == 'aarch64' then
elseif machine == 'aarch64' or machine == 'arm64' then
return 'arm64'
end
end

---@return string|nil
local function get_system()
local os = vim.loop.os_uname().sysname
if os == 'Linux' then
Expand All @@ -39,10 +41,16 @@ local function get_system()
end
end

---@return string|nil
local function get_package()
return get_system() .. '-' .. get_machine()
local system = get_system()
local machine = get_machine()
if system and machine then
return system .. '-' .. machine
end
end

---@return string
local function get_version()
local file = io.open(script_path('../Cargo.toml'), 'r')
if file ~= nil then
Expand All @@ -54,27 +62,51 @@ local function get_version()
end
end

return "0.0.0"
return '0.0.0'
end

---@return string|nil
local function get_bin_name()
local package = get_package()
if not package then
return nil
end
if get_system() == 'windows' then
return 'magento2-ls-' .. get_package() .. '.exe'
return 'magento2-ls-' .. package .. '.exe'
else
return 'magento2-ls-' .. get_package()
return 'magento2-ls-' .. package
end
end

local function get_bin_url()
---@param bin_name string
---@return string
local function get_bin_url(bin_name)
local base_url = 'https://github.com/pbogut/magento2-ls/releases/download/' .. get_version()
return base_url .. '/' .. bin_name
end

---@param bin_url string
local function download_server(bin_url)
local bin = destination .. '/magento2-ls'
if get_system() == 'windows' then
return base_url .. '/' .. get_bin_name()
else
return base_url .. '/' .. get_bin_name()
bin = bin .. '.exe'
end
vim.fn.mkdir(destination, 'p')
local cmd = 'curl -L -o ' .. bin .. ' ' .. bin_url
vim.fn.jobstart(cmd, {
on_exit = function(_, code)
if code == 0 then
vim.notify('Server download successful', vim.log.levels.INFO, { title = 'magento2-ls' })
if get_system() ~= 'windows' then
vim.fn.system('chmod +x ' .. bin)
end
else
vim.notify('Server download failed', vim.log.levels.ERROR, { title = 'magento2-ls' })
end
end,
})
end


M.setup = function(opts)
opts = opts or {}
opts = vim.tbl_deep_extend('keep', opts, {
Expand Down Expand Up @@ -103,24 +135,27 @@ M.setup = function(opts)
end

M.get_server = function()
local bin = destination .. '/magento2-ls'
if get_system() == 'windows' then
bin = bin .. '.exe'
end
vim.fn.mkdir(destination, 'p')
local cmd = 'curl -L -o ' .. bin .. ' ' .. get_bin_url()
vim.fn.jobstart(cmd, {
on_exit = function(_, code)
if code == 0 then
vim.notify('Server download successful', vim.log.levels.INFO, { title = 'magento2-ls' })
if get_system() ~= 'windows' then
vim.fn.system('chmod +x ' .. bin)
end
else
vim.notify('Server download failed', vim.log.levels.ERROR, { title = 'magento2-ls' })
local bin_name = get_bin_name()
local bin_url = ''
if bin_name then
bin_url = get_bin_url(bin_name)
download_server(bin_url)
else
vim.ui.select({
'magento2-ls-darwin-arm64',
'magento2-ls-darwin-x64',
'magento2-ls-linux-x64',
'magento2-ls-windows-arm64.exe',
'magento2-ls-windows-x64.exe',
}, {
prompt = 'Select package for your system',
}, function(selected)
if selected then
bin_url = get_bin_url(selected)
download_server(bin_url)
end
end,
})
end)
end
end

M.build = function()
Expand Down

0 comments on commit 7d5c769

Please sign in to comment.