-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathutils.lua
More file actions
73 lines (58 loc) · 1.99 KB
/
utils.lua
File metadata and controls
73 lines (58 loc) · 1.99 KB
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
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()
end
local matches = vim.fs.find({ "mix.exs" }, { upward = true, limit = 2, path = fname })
local child_or_root_path, maybe_umbrella_path = unpack(matches)
return vim.fs.dirname(maybe_umbrella_path or child_or_root_path)
end
---@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://%s/repos/%s/%s/releases/latest]],
github_host,
owner,
repo
)
local invocation = vim.fn.system(curl)
vim.fn.mkdir(vim.fn.expand(cache_dir), "p")
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