1+ local Path = require (" plenary.path" )
12local M = {}
23
4+ --- @param path string
35function M .safe_path (path )
46 return string.gsub (path , " /" , " _" )
57end
68
9+ --- @param repo string
10+ --- @param ref string
11+ --- @return string
712function M .repo_path (repo , ref )
813 local x = M .safe_path (string.format (" %s-%s" , repo , ref or " HEAD" ))
914
1015 return x
1116end
1217
18+ --- @param fname string ?
19+ --- @return string
1320function 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 )
2229end
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
3369end
3470
3571return M
0 commit comments