forked from williamboman/mason-lspconfig.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
144 lines (131 loc) · 4.62 KB
/
init.lua
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
local installer = require "mason-core.installer"
local _ = require "mason-core.functional"
local process = require "mason-core.process"
local path = require "mason-core.path"
local Result = require "mason-core.result"
local spawn = require "mason-core.spawn"
local Optional = require "mason-core.optional"
local platform = require "mason-core.platform"
local M = {}
local create_bin_path = _.compose(path.concat, function(executable)
return _.append(executable, { "bin" })
end, _.if_else(_.always(platform.is.win), _.format "%s.bat", _.identity))
---@param package string
local function with_receipt(package)
return function()
local ctx = installer.context()
ctx.receipt:with_primary_source(ctx.receipt.luarocks(package))
end
end
---@param package string: The luarock package to install.
---@param opts { dev: boolean, bin : string[] | nil } | nil
function M.package(package, opts)
return function()
return M.install(package, opts).with_receipt()
end
end
---@async
---@param pkg string: The luarock package to install.
---@param opts { dev: boolean, bin : string[] | nil } | nil
function M.install(pkg, opts)
opts = opts or {}
local ctx = installer.context()
ctx:promote_cwd()
ctx.spawn.luarocks {
"install",
"--tree",
ctx.cwd:get(),
opts.dev and "--dev" or vim.NIL,
pkg,
ctx.requested_version:or_else(vim.NIL),
}
if opts.bin then
_.each(function(executable)
ctx:link_bin(executable, create_bin_path(executable))
end, opts.bin)
end
return {
with_receipt = with_receipt(pkg),
}
end
---@alias InstalledLuarock {package: string, version: string, arch: string, nrepo: string, namespace: string}
---@type fun(output: string): InstalledLuarock[]
M.parse_installed_rocks = _.compose(
_.map(_.compose(
-- https://github.com/luarocks/luarocks/blob/fbd3566a312e647cde57b5d774533731e1aa844d/src/luarocks/search.lua#L317
_.zip_table { "package", "version", "arch", "nrepo", "namespace" },
_.split "\t"
)),
_.split "\n"
)
---@async
---@param receipt InstallReceipt
---@param install_dir string
function M.get_installed_primary_package_version(receipt, install_dir)
if receipt.primary_source.type ~= "luarocks" then
return Result.failure "Receipt does not have a primary source of type luarocks"
end
local primary_package = receipt.primary_source.package
return spawn
.luarocks({
"list",
"--tree",
install_dir,
"--porcelain",
})
:map_catching(function(result)
local luarocks = M.parse_installed_rocks(result.stdout)
return Optional.of_nilable(_.find_first(_.prop_eq("package", primary_package), luarocks))
:map(_.prop "version")
:or_else_throw()
end)
end
---@alias OutdatedLuarock {name: string, installed: string, available: string, repo: string}
---@type fun(output: string): OutdatedLuarock[]
M.parse_outdated_rocks = _.compose(
_.map(_.compose(
-- https://github.com/luarocks/luarocks/blob/fbd3566a312e647cde57b5d774533731e1aa844d/src/luarocks/cmd/list.lua#L59
_.zip_table { "name", "installed", "available", "repo" },
_.split "\t"
)),
_.split "\n"
)
---@async
---@param receipt InstallReceipt
---@param install_dir string
function M.check_outdated_primary_package(receipt, install_dir)
if receipt.primary_source.type ~= "luarocks" then
return Result.failure "Receipt does not have a primary source of type luarocks"
end
local primary_package = receipt.primary_source.package
return spawn
.luarocks({
"list",
"--outdated",
"--tree",
install_dir,
"--porcelain",
})
:map_catching(function(result)
local outdated_rocks = M.parse_outdated_rocks(result.stdout)
return Optional.of_nilable(_.find_first(_.prop_eq("name", primary_package), outdated_rocks))
:map(
---@param outdated_rock OutdatedLuarock
function(outdated_rock)
return {
name = outdated_rock.name,
current_version = assert(outdated_rock.installed),
latest_version = assert(outdated_rock.available),
}
end
)
:or_else_throw()
end)
end
---@param install_dir string
function M.env(install_dir)
return {
PATH = process.extend_path { path.concat { install_dir, "bin" } },
}
end
return M