This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.lua
159 lines (136 loc) · 5.1 KB
/
utils.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
local os = require('ffi').os:lower()
local Path = require('plenary.path')
local config = require('cmake.config')
local scandir = require('plenary.scandir')
local utils = {}
function utils.notify(msg, log_level)
vim.notify(msg, log_level, { title = 'CMake' })
end
function utils.get_parameters()
local parameters_file = Path:new(config.parameters_file)
if not parameters_file:is_file() then
return { currentTarget = '', buildType = 'Debug', arguments = {} }
end
return vim.fn.json_decode(parameters_file:read())
end
function utils.set_parameters(parameters)
local parameters_file = Path:new(config.parameters_file)
parameters_file:write(vim.fn.json_encode(parameters), 'w')
end
function utils.get_build_dir(parameters)
if not parameters then
parameters = utils.get_parameters()
end
local build_dir = tostring(config.build_dir)
build_dir = build_dir:gsub('{cwd}', vim.fn.getcwd())
build_dir = build_dir:gsub('{os}', os)
build_dir = build_dir:gsub('{build_type}', parameters['buildType']:lower())
return Path:new(build_dir)
end
function utils.get_reply_dir(build_dir)
return build_dir / '.cmake/api/v1/reply'
end
function utils.get_codemodel_targets(reply_dir)
local codemodel = Path:new(vim.fn.globpath(reply_dir.filename, 'codemodel*'))
local codemodel_json = vim.fn.json_decode(codemodel:read())
return codemodel_json['configurations'][1]['targets']
end
function utils.get_target_info(reply_dir, codemodel_target)
return vim.fn.json_decode((reply_dir / codemodel_target['jsonFile']):read())
end
-- Tell CMake to generate codemodel
function utils.make_query_files(build_dir)
local query_dir = build_dir / '.cmake/api/v1/query'
if not query_dir:mkdir({ parents = true }) then
utils.notify('Unable to create folder ' .. query_dir.filename, vim.log.levels.ERROR)
return false
end
local codemodel_file = query_dir / 'codemodel-v2'
if not codemodel_file:is_file() then
if not codemodel_file:touch() then
utils.notify('Unable to create file ' .. codemodel_file.filename, vim.log.levels.ERROR)
return false
end
end
return true
end
function utils.get_current_executable_info(parameters, build_dir)
if not build_dir:is_dir() then
utils.notify('You need to configure first', vim.log.levels.ERROR)
return nil
end
local target_name = parameters['currentTarget']
if not target_name then
utils.notify('You need to select target first', vim.log.levels.ERROR)
return nil
end
local reply_dir = utils.get_reply_dir(build_dir)
for _, target in ipairs(utils.get_codemodel_targets(reply_dir)) do
if target_name == target['name'] then
local target_info = utils.get_target_info(reply_dir, target)
if target_info['type'] ~= 'EXECUTABLE' then
utils.notify('Specified target is not executable: ' .. target_name, vim.log.levels.ERROR)
return nil
end
return target_info
end
end
utils.notify('Unable to find the following target: ' .. target_name, vim.log.levels.ERROR)
return nil
end
function utils.get_current_target(parameters)
local build_dir = utils.get_build_dir(parameters)
local target_info = utils.get_current_executable_info(parameters, build_dir)
if not target_info then
return nil, nil
end
local target = build_dir / target_info['artifacts'][1]['path']
if not target:is_file() then
utils.notify('Selected target is not built: ' .. target.filename, vim.log.levels.ERROR)
return nil, nil
end
local target_dir = parameters['runDir']
if target_dir == nil then
target_dir = target:parent()
end
local arguments = parameters['arguments'][target_info['name']]
return target_dir, target, arguments
end
function utils.asyncrun_callback(function_string)
vim.api.nvim_command('autocmd User AsyncRunStop ++once if g:asyncrun_status ==? "success" | call luaeval("' .. function_string .. '") | endif')
end
function utils.copy_compile_commands()
local compile_commands = utils.get_build_dir() / 'compile_commands.json'
local destination = Path:new(vim.fn.getcwd(), 'compile_commands.json')
destination:rm()
compile_commands:copy({ destination = destination.filename })
end
function utils.copy_folder(folder, destination)
destination:mkdir()
for _, entry in ipairs(scandir.scan_dir(folder.filename, { depth = 1, add_dirs = true })) do
local target_entry = destination / entry:sub(#folder.filename + 2)
local source_entry = Path:new(entry)
if source_entry:is_file() then
if not source_entry:copy({ destination = target_entry.filename }) then
error('Unable to copy ' .. target_entry)
end
else
utils.copy_folder(folder, target_entry)
end
end
end
function utils.autoclose_quickfix(options)
local mode = options['mode']
if not mode or mode ~= 'async' then
vim.api.nvim_command('cclose')
end
end
function utils.check_debugging_build_type(parameters)
local buildType = parameters['buildType']
if buildType ~= 'Debug' and buildType ~= 'RelWithDebInfo' then
utils.notify('For debugging you need to use Debug or RelWithDebInfo, but currently your build type is ' .. buildType, vim.log.levels.ERROR)
return false
end
return true
end
return utils