Skip to content
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
9 changes: 5 additions & 4 deletions lua/elixir/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ function M.setup(opts)
opts.credo.cmd = M.credo.default_bin
end

if not opts.credo.version then
if enabled(opts.credo.enable) and not opts.credo.version then
opts.credo.version = utils.latest_release("elixir-tools", "credo-language-server")
end

if not opts.nextls.cmd then
opts.nextls.cmd = M.nextls.default_bin
end

if not opts.nextls.version then
if opts.nextls.enable and not opts.nextls.version then
opts.nextls.version = utils.latest_release("elixir-tools", "next-ls")
end

Expand All @@ -57,11 +57,12 @@ function M.setup(opts)
if enabled(opts.elixirls.enable) then
elixirls.setup(opts.elixirls)
end
if enabled(opts.credo.enable) then

if opts.credo.version and enabled(opts.credo.enable) then
credo.setup(opts.credo)
end

if opts.nextls.enable == true then
if opts.nextls.version and opts.nextls.enable == true then
nextls.setup(opts.nextls)
end
end
Expand Down
44 changes: 40 additions & 4 deletions lua/elixir/utils.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
local Path = require("plenary.path")
local M = {}

---@param path string
function M.safe_path(path)
return string.gsub(path, "/", "_")
end

---@param repo string
---@param ref string
---@return string
function M.repo_path(repo, ref)
local x = M.safe_path(string.format("%s-%s", repo, ref or "HEAD"))

return x
end

---@param fname string?
---@return string
function M.root_dir(fname)
if not fname or fname == "" then
fname = vim.fn.getcwd()
Expand All @@ -21,15 +28,44 @@ function M.root_dir(fname)
return vim.fs.dirname(maybe_umbrella_path or child_or_root_path)
end

function M.latest_release(owner, repo)
---@param owner string
---@param repo string
---@param opts? table
---@return string?
function M.latest_release(owner, repo, opts)
opts = opts or {}
local github_host = opts.github_host or "api.github.com"
local cache_dir = opts.cache_dir or "~/.cache/nvim/elixir-tools.nvim/"
local curl = string.format(
[[curl --silent -L -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/%s/%s/releases/latest]],
[[curl --silent -L -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" https://%s/repos/%s/%s/releases/latest]],
github_host,
owner,
repo
)
local resp = vim.json.decode(vim.fn.system(curl))
local invocation = vim.fn.system(curl)

return resp and resp.tag_name and resp.tag_name:gsub("^v", "") or nil
local latest_version_file = Path:new(vim.fn.expand(cache_dir .. owner .. "-" .. repo .. ".txt")):absolute()

if vim.v.shell_error == 0 then
local resp = vim.json.decode(invocation)
local version = resp and resp.tag_name and resp.tag_name:gsub("^v", "")

assert(type(version) == "string")

vim.fn.writefile({ version }, latest_version_file)

return version
elseif vim.fn.filereadable(latest_version_file) == 1 then
return vim.fn.readfile(latest_version_file)[1]
else
vim.notify(
"Failed to fetch the current "
.. repo
.. " version from GitHub or the cache. You most likely do not have an internet connection and have no cached version of the language server."
)

return nil
end
end

return M
31 changes: 31 additions & 0 deletions tests/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,35 @@ describe("utils", function()
assert.are.equal(nil, result)
end)
end)

describe("latest_release", function()
it("returns the latest release for the repo", function()
local result = utils.latest_release("elixir-tools", "next-ls", { cache_dir = "./tmp/" })

assert(type(result) == "string")
assert.is.Truthy(string.match(result, "%d+%.%d+%.%d+"))
end)

it("returns nil if the command has a non zero exit code and no file in cache", function()
vim.fn.delete("./tmp/elixir-tools-next-ls.txt")
local result = utils.latest_release(
"elixir-tools",
"next-ls",
{ github_host = "localhost:9999", cache_dir = "./tmp/" }
)

assert.is.Nil(result)
end)

it("returns nil if the command has a non zero exit code and no file in cache", function()
vim.fn.writefile({ "0.2.2" }, "./tmp/elixir-tools-next-ls.txt")
local result = utils.latest_release(
"elixir-tools",
"next-ls",
{ github_host = "localhost:9999", cache_dir = "./tmp/" }
)

assert.are.same(result, "0.2.2")
end)
end)
end)