-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.lua
298 lines (244 loc) · 6.55 KB
/
platform.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
local M = {}
local vimfn = (vim or {}).fn or {}
local is_windows = (function()
local is_ok, has_win = pcall(vimfn.has, "win32")
if is_ok then
return has_win > 0
end
return package.config:sub(1, 1) == "\\"
end)()
local function _exec(cmd)
local status_ok, pipe = pcall(io.popen, cmd)
if not status_ok or not pipe then
return nil
end
pipe:flush()
local result = pipe:read("*a")
pipe:close()
return result
end
local function _to_lines(s)
local lines = {}
while s and #s > 0 do
local eol, _ = s:find("\n", 1, true)
local line = eol and s:sub(1, eol) or s
s = eol and s:sub(eol + 1) or ""
line = line:gsub("[%s\r\n]+$", "")
table.insert(lines, line)
end
return lines
end
local function _str_strip(s)
return s:gsub("^%s+", ""):gsub("%s+$", "")
end
local function _read_file(filename)
local file = io.open(filename, "r")
if not file then
return nil
end
local data = file:read("*a")
file:close()
return data
end
local function _is_file(filename)
local f = io.open(filename, "r")
return f ~= nil and io.close(f)
end
---@return integer? procs number of processors detected
---@nodiscard
function M.cpu_procs()
local s = os.getenv("NUMBER_OF_PROCESSORS")
if s == nil or s:match("^%s*$") then
local is_ok, handle = pcall(io.popen, "nproc -all 2>&1")
if is_ok and handle ~= nil then
s = handle:read("*a")
handle:close()
end
end
if s == nil or s:match("^%s*$") then
return nil
end
s = s:match("^%s*(.*%S)%s*$")
return tonumber(s)
end
---@return boolean is_win true if current os is windows
---@nodiscard
function M.is_windows()
return is_windows
end
---@return string? ver_str neovim version string
---@nodiscard
function M.nvim_version()
if vimfn.execute then
local ver_str = vimfn.execute("version")
return ver_str:match("^.+ +v([%d.]+)[-_%s]*([%w._-]*)\r?\n")
end
return nil
end
---@package
---@alias regval { type: string, value: number|string }
---@param key string path to registry key
---@return string[] keys, table<string, regval> values, regval? default
---@nodiscard
function M.read_winreg(key)
assert(type(key) == "string", "key must be a string")
if not is_windows then
return {}, {}, nil
end
local subkeys = {}
local values = {}
local default = nil
key = key
:gsub("[\\/]+", "\\")
:gsub("[\\/]+$", "")
:gsub("[^a-zA-Z0-9_\\-\\/\\. ]", "")
local out = _exec('reg.exe query "' .. key .. '"')
local lines = _to_lines(out)
-- skip prelude
while
lines
and #lines > 0
and lines[1]
and (lines[1]:match("^[%s\r\n]*$") or lines[1]:match("^[^%s\r\n]"))
do
table.remove(lines, 1)
end
for _, line in ipairs(lines) do
if line:match("^%s+") then
-- line specifies a value
local name, type_, value = line:match("^%s+(.+)%s+(REG_[%w_]+)%s+(.*)$")
if name and type_ then
name = _str_strip(name)
value = _str_strip(value)
local entry = { type = type_, value = value }
if name == "(Default)" then
default = entry
else
values[name] = entry
end
end
elseif line:match("^[^%s]") then
-- line specifies a sub-key
local keyname = _str_strip(line)
if keyname and #keyname > 0 then
table.insert(subkeys, keyname)
end
end
end
return subkeys, values, default
end
---@param key string registry key
---@param name? string value name
---@return regval? value value read from registry
function M.read_winreg_value(key, name)
if not is_windows then
return nil
end
key = key
:gsub("[\\/]+", "\\")
:gsub("[\\/]+$", "")
:gsub("[^a-zA-Z0-9_\\-\\/\\. ]", "")
local _, values, default = M.read_winreg(key)
if not name or #name <= 0 then
return default
end
return values and values[name] or nil
end
local function _get_win_machine_id()
local key = "HKLM\\SOFTWARE\\Microsoft\\Cryptography"
local name = "MachineGuid"
local value = M.read_winreg_value(key, name)
if not value or not value.value then
return nil
end
return tostring(value.value)
end
local function _get_macos_machine_id()
local output = _exec("ioreg -rd1 -c IOPlatformExpertDevice")
if not output then
return nil
end
local match = output:match(
"IOPlatformUUID[^\n]+=[\r\t\v\n ]*[\"']?(.+)[\"']?[\r\t\v\n ]*$"
)
if not match then
return nil
end
return _str_strip(match)
end
local function _get_linux_machine_id()
local mach_id = _read_file("/var/lib/dbus/machine-id")
mach_id = mach_id and _str_strip(mach_id) or nil
if not mach_id or #mach_id <= 0 then
mach_id = _read_file("/etc-machine-id")
mach_id = mach_id and _str_strip(mach_id) or nil
end
if not mach_id or #mach_id <= 0 then
return nil
end
return mach_id
end
local function _get_bsd_machine_id()
local mach_id = _read_file("/etc/hostid")
mach_id = mach_id and _str_strip(mach_id) or nil
if not mach_id then
local output = _exec("kenv -q smbios.system.uuid")
mach_id = output and _str_strip(output) or nil
end
return mach_id
end
---@return string? mach_id unique machine-specific id
function M.machine_id()
local mach_id = nil
if is_windows then
mach_id = mach_id or _get_win_machine_id()
else
mach_id = mach_id
or _get_macos_machine_id()
or _get_linux_machine_id()
or _get_bsd_machine_id()
end
return mach_id
end
---@param syspath? string path string or nil to use $PATH env var
---@return string[] path array of dirs found in path
function M.path(syspath)
syspath = syspath or os.getenv("PATH")
local dirs = {}
while syspath and #syspath > 0 do
local pat = "^%s*(.-)%s*[;:][;:%s]*" -- unix path
if syspath:match("^%s*[a-zA-Z]:[\\/]") then
pat = "^%s*([a-zA-Z]:[\\/].-)%s*[;:][;:%s]*" -- win path
end
local _, stop, dir = syspath:find(pat)
if stop then
syspath = syspath:sub(stop + 1)
else
dir = _str_strip(syspath):gsub("[;:%s]+$", "")
syspath = nil
end
if dir and #dir > 0 then
table.insert(dirs, dir)
end
end
return dirs
end
---@param exe string executable name
---@param syspath? string|string[] $PATH to search. default is use env var
---@return string? filename path to executable if found
function M.in_path(exe, syspath)
local sep = is_windows and "\\" or "/"
if type(syspath) ~= "table" then
syspath = M.path(syspath)
end
for _, dir in ipairs(syspath) do
local filename = dir .. sep .. exe
if _is_file(filename) then
return filename
elseif is_windows and _is_file(filename .. ".exe") then
return filename .. ".exe"
end
end
return nil
end
return M