Skip to content

Commit d89fe66

Browse files
binarytemplemhanberg
authored andcommitted
fix: cache latest release and fallback to it if call to GitHub fails
1 parent d8f9535 commit d89fe66

3 files changed

Lines changed: 76 additions & 8 deletions

File tree

lua/elixir/init.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ function M.setup(opts)
4040
opts.credo.cmd = M.credo.default_bin
4141
end
4242

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

4747
if not opts.nextls.cmd then
4848
opts.nextls.cmd = M.nextls.default_bin
4949
end
5050

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

@@ -57,11 +57,12 @@ function M.setup(opts)
5757
if enabled(opts.elixirls.enable) then
5858
elixirls.setup(opts.elixirls)
5959
end
60-
if enabled(opts.credo.enable) then
60+
61+
if opts.credo.version and enabled(opts.credo.enable) then
6162
credo.setup(opts.credo)
6263
end
6364

64-
if opts.nextls.enable == true then
65+
if opts.nextls.version and opts.nextls.enable == true then
6566
nextls.setup(opts.nextls)
6667
end
6768
end

lua/elixir/utils.lua

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
local Path = require("plenary.path")
12
local M = {}
23

4+
---@param path string
35
function M.safe_path(path)
46
return string.gsub(path, "/", "_")
57
end
68

9+
---@param repo string
10+
---@param ref string
11+
---@return string
712
function M.repo_path(repo, ref)
813
local x = M.safe_path(string.format("%s-%s", repo, ref or "HEAD"))
914

1015
return x
1116
end
1217

18+
---@param fname string?
19+
---@return string
1320
function M.root_dir(fname)
1421
if not fname or fname == "" then
1522
fname = vim.fn.getcwd()
@@ -21,15 +28,44 @@ function M.root_dir(fname)
2128
return vim.fs.dirname(maybe_umbrella_path or child_or_root_path)
2229
end
2330

24-
function M.latest_release(owner, repo)
31+
---@param owner string
32+
---@param repo string
33+
---@param opts? table
34+
---@return string?
35+
function M.latest_release(owner, repo, opts)
36+
opts = opts or {}
37+
local github_host = opts.github_host or "api.github.com"
38+
local cache_dir = opts.cache_dir or "~/.cache/nvim/elixir-tools.nvim/"
2539
local curl = string.format(
26-
[[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]],
40+
[[curl --silent -L -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" https://%s/repos/%s/%s/releases/latest]],
41+
github_host,
2742
owner,
2843
repo
2944
)
30-
local resp = vim.json.decode(vim.fn.system(curl))
45+
local invocation = vim.fn.system(curl)
3146

32-
return resp and resp.tag_name and resp.tag_name:gsub("^v", "") or nil
47+
local latest_version_file = Path:new(vim.fn.expand(cache_dir .. owner .. "-" .. repo .. ".txt")):absolute()
48+
49+
if vim.v.shell_error == 0 then
50+
local resp = vim.json.decode(invocation)
51+
local version = resp and resp.tag_name and resp.tag_name:gsub("^v", "")
52+
53+
assert(type(version) == "string")
54+
55+
vim.fn.writefile({ version }, latest_version_file)
56+
57+
return version
58+
elseif vim.fn.filereadable(latest_version_file) == 1 then
59+
return vim.fn.readfile(latest_version_file)[1]
60+
else
61+
vim.notify(
62+
"Failed to fetch the current "
63+
.. repo
64+
.. " version from GitHub or the cache. You most likely do not have an internet connection and have no cached version of the language server."
65+
)
66+
67+
return nil
68+
end
3369
end
3470

3571
return M

tests/utils_spec.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,35 @@ describe("utils", function()
3636
assert.are.equal(nil, result)
3737
end)
3838
end)
39+
40+
describe("latest_release", function()
41+
it("returns the latest release for the repo", function()
42+
local result = utils.latest_release("elixir-tools", "next-ls")
43+
44+
assert(type(result) == "string")
45+
assert.is.Truthy(string.match(result, "%d+%.%d+%.%d+"))
46+
end)
47+
48+
it("returns nil if the command has a non zero exit code and no file in cache", function()
49+
vim.fn.delete("./tmp/elixir-tools-next-ls.txt")
50+
local result = utils.latest_release(
51+
"elixir-tools",
52+
"next-ls",
53+
{ github_host = "localhost:9999", cache_dir = "./tmp/" }
54+
)
55+
56+
assert.is.Nil(result)
57+
end)
58+
59+
it("returns nil if the command has a non zero exit code and no file in cache", function()
60+
vim.fn.writefile({ "0.2.2" }, "./tmp/elixir-tools-next-ls.txt")
61+
local result = utils.latest_release(
62+
"elixir-tools",
63+
"next-ls",
64+
{ github_host = "localhost:9999", cache_dir = "./tmp/" }
65+
)
66+
67+
assert.are.same(result, "0.2.2")
68+
end)
69+
end)
3970
end)

0 commit comments

Comments
 (0)