-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathutils.lua
More file actions
272 lines (241 loc) · 7.82 KB
/
utils.lua
File metadata and controls
272 lines (241 loc) · 7.82 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
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
local Path = require("plenary.path")
local osys = require("cmake-tools.osys")
local Result = require("cmake-tools.result")
local Types = require("cmake-tools.types")
local notification = require("cmake-tools.notification")
local scratch = require("cmake-tools.scratch")
---@alias executor_conf {name:string, opts:table}
---@alias runner_conf {name:string, opts:table}
local utils = {}
function utils.get_cmake_configuration(cwd)
local cmakelists = Path:new(cwd, "CMakeLists.txt")
if not cmakelists:is_file() then
return Result:new(
Types.CANNOT_FIND_CMAKE_CONFIGURATION_FILE,
nil,
"Cannot find CMakeLists.txt at cwd (" .. cwd .. ")."
)
end
return Result:new(Types.SUCCESS, cmakelists, "cmake-tools has found CMakeLists.txt.")
end
-- Get string representation for object o
function utils.dump(o)
if type(o) == "table" then
local s = "{ "
for k, v in pairs(o) do
if type(k) ~= "number" then
k = '"' .. k .. '"'
end
s = s .. "[" .. k .. "] = " .. utils.dump(v) .. ","
end
return s .. "} "
else
return tostring(o)
end
end
function utils.get_path(str, sep)
sep = sep or (osys.iswin32 and "\\" or "/")
return str:match("(.*" .. sep .. ")")
end
function utils.mkdir(dir)
local _dir = Path:new(dir)
_dir:mkdir({ parents = true, exists_ok = true })
end
function utils.rmfile(file)
if file:exists() then
file:rm()
end
end
function utils.file_exists(path)
local file = Path:new(path)
if not file:exists() then
return false
end
return true
end
function utils.deepcopy(orig, copies)
copies = copies or {}
local orig_type = type(orig)
local copy
if orig_type == "table" then
if copies[orig] then
copy = copies[orig]
else
copy = {}
copies[orig] = copy
for orig_key, orig_value in next, orig, nil do
copy[utils.deepcopy(orig_key, copies)] = utils.deepcopy(orig_value, copies)
end
setmetatable(copy, utils.deepcopy(getmetatable(orig), copies))
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function utils.softlink(src, target)
if utils.file_exists(src) and not utils.file_exists(target) then
-- if we don't always use terminal
local cmd = "silent exec "
.. "'!cmake -E create_symlink "
.. utils.transform_path(src)
.. " "
.. utils.transform_path(target)
.. "'"
vim.cmd(cmd)
end
end
function utils.transform_path(path, keep)
if keep then
return path
end
if path[1] ~= '"' and string.find(path, " ") then
return '"' .. path .. '"'
else
return path
end
end
--- Get the appropriate executor by name
---@param name string
---@return executor
function utils.get_executor(name)
return require("cmake-tools.executors")[name]
end
--- Get the appropriate runner by name
---@param name string
---@return runner
function utils.get_runner(name)
return require("cmake-tools.runners")[name]
end
---@param executor_data executor_conf
function utils.show_executor(executor_data)
utils.get_executor(executor_data.name).show(executor_data.opts)
end
---@param runner_data runner_conf
function utils.show_runner(runner_data)
utils.get_runner(runner_data.name).show(runner_data.opts)
end
---@param executor_data executor_conf
function utils.close_executor(executor_data)
utils.get_executor(executor_data.name).close(executor_data.opts)
end
---@param runner_data runner_conf
function utils.close_runner(runner_data)
utils.get_runner(runner_data.name).close(runner_data.opts)
end
---@param executor_data executor_conf
function utils.stop_executor(executor_data)
utils.get_executor(executor_data.name).stop(executor_data.opts)
end
---@param runner_data runner_conf
function utils.stop_runner(runner_data)
utils.get_runner(runner_data.name).stop(runner_data.opts)
end
--- Check if exists active job.
---@param runner_data executor_conf the runner
---@param executor_data executor_conf the executor
-- @return true if exists else false
function utils.has_active_job(runner_data, executor_data)
return utils.get_executor(executor_data.name).has_active_job(executor_data.opts)
or utils.get_runner(runner_data.name).has_active_job(runner_data.opts)
end
local notify_update_line = function(ntfy)
return function(out, err)
if not ntfy.enabled then
return
end
local line = err and err or out
if line ~= nil then
if line and vim.fn.match(line, "^%[%s*(%d+)%s*%%%]") then -- only show lines containing build progress e.g [ 12%]
ntfy:notify(line, err and "warn" or "info")
ntfy:startSpinner()
end
end
end
end
---Run a command using specified executor, this is used by generate, build, clean, install, etc.
---@param cmd string the executable to execute
---@param env_script string environment setup script
---@param env table environment variables
---@param args table arguments to the executable
---@param cwd string the directory to run in
---@param runner runner_conf the executor or runner
---@param callback nil|function extra arguments, f.e on_success is a callback to be called when the process finishes
---@return nil
function utils.run(cmd, env_script, env, args, cwd, runner, callback)
-- save all
vim.cmd("silent exec " .. '"wall"')
local ntfy = notification:new("runner")
ntfy:notify(cmd, "info")
local _mes =
{ "[RUN]:", cmd, table.concat(args, " "), "<ENV>", table.concat(env, " "), "{CWD}", cwd }
scratch.append(table.concat(_mes, " "))
utils.get_runner(runner.name).run(cmd, env_script, env, args, cwd, runner.opts, function(code)
local msg = "Exited with code " .. code
local icon = ""
local level = nil -- use the previously defined level
if code ~= 0 then
level = "error"
icon = ""
end
ntfy:notify(msg, level, { icon = icon, timeout = 3000 })
if type(callback) == "function" then
if code == 0 then
callback(Result:new(Types.SUCCESS, nil, nil))
else
callback(Result:new(Types.CMAKE_RUN_FAILED, nil, "Process exited with code " .. code))
end
end
end, notify_update_line(ntfy))
end
---Run a command using specified executor, this is used by generate, build, clean, install, etc.
---@param cmd string the executable to execute
---@param env_script string environment setup script
---@param env table environment variables
---@param args table arguments to the executable
---@param cwd string the directory to run in
---@param executor executor_conf the executor or runner
---@param callback nil|fun(cmake.Result) extra arguments, f.e on_success is a callback to be called when the process exits with a 0 exit code
---@return nil
function utils.execute(cmd, env_script, env, args, cwd, executor, callback)
-- save all
vim.cmd("silent exec " .. '"wall"')
local ntfy = notification:new("executor")
ntfy:notify(cmd, "info")
local _mes =
{ "[EXECUTE]:", cmd, table.concat(args, " "), "<ENV>", table.concat(env, " "), "{CWD}", cwd }
scratch.append(table.concat(_mes, " "))
utils
.get_executor(executor.name)
.run(cmd, env_script, env, args, cwd, executor.opts, function(code)
ntfy:stopSpinner()
local msg = "Exited with code " .. code
local level = nil -- use the previously defined level
local icon = ""
if code ~= 0 then
level = "error"
icon = ""
end
ntfy:notify(msg, level, { icon = icon, timeout = 3000 })
if type(callback) == "function" then
if code == 0 then
callback(Result:new(Types.SUCCESS, nil, nil))
else
callback(
Result:new(
Types.CMAKE_RUN_FAILED,
nil,
string.format(
"Process %s %s (cwd=%s) exited with code %d",
cmd,
table.concat(args, " "),
cwd,
code
)
)
)
end
end
end, notify_update_line(ntfy))
end
return utils