Skip to content

Commit df2f55d

Browse files
committed
fix: ctest command change to async
1 parent feb4d1c commit df2f55d

3 files changed

Lines changed: 186 additions & 106 deletions

File tree

lua/cmake-tools/config.lua

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ local Result = require("cmake-tools.result")
44
-- local utils = require("cmake-tools.utils") -- Fails lua check. Uncomment this for testing
55
local Types = require("cmake-tools.types")
66
local variants = require("cmake-tools.variants")
7+
local Presets = require("cmake-tools.presets")
8+
local kits = require("cmake-tools.kits")
79

810
local Config = {
911
build_directory = nil,
@@ -97,8 +99,8 @@ function Config:update_build_dir(build_dir, no_expand_build_dir)
9799
end
98100

99101
---Prepare build directory. Which allows macro expansion.
100-
---@param kits table all the kits
101-
function Config:prepare_build_directory(kits)
102+
---@param kit_list table all the kits
103+
function Config:prepare_build_directory(kit_list)
102104
-- macro expansion:
103105
-- ${kit}
104106
-- ${kitGenerator}
@@ -108,8 +110,8 @@ function Config:prepare_build_directory(kits)
108110
local kit = self.kit
109111
local variant = self.variant
110112
local kit_info = nil
111-
if kits then
112-
for _, item in ipairs(kits) do
113+
if kit_list then
114+
for _, item in ipairs(kit_list) do
113115
if item.name == kit then
114116
kit_info = item
115117
end
@@ -482,4 +484,103 @@ function Config:build_targets_with_sources()
482484
return _virtual_targets
483485
end
484486

487+
function Config:update_build_type()
488+
local presets = Presets:parse(self.cwd)
489+
if not presets then
490+
return
491+
end
492+
if not self.configure_preset then
493+
return
494+
end
495+
local configure_preset =
496+
presets:get_configure_preset(self.configure_preset, { include_hidden = true })
497+
if not configure_preset then
498+
return
499+
end
500+
501+
self.build_type = configure_preset:get_build_type()
502+
503+
if not self.build_preset then
504+
return
505+
end
506+
local build_preset = presets:get_build_preset(self.build_preset)
507+
if not build_preset then
508+
return
509+
end
510+
local configuration_types = configure_preset:get_build_configuration_types()
511+
512+
if not configuration_types then
513+
return
514+
end
515+
local build_type_from_build_preset = build_preset:get_build_type()
516+
517+
if not build_type_from_build_preset then
518+
return
519+
end
520+
local exists = false
521+
for _, Item in ipairs(configuration_types) do
522+
if Item == build_type_from_build_preset then
523+
exists = true
524+
break
525+
end
526+
end
527+
if exists then
528+
self.build_type = build_type_from_build_preset
529+
end
530+
end
531+
532+
function Config:update_build_target()
533+
local presets = Presets:parse(self.cwd)
534+
if not presets then
535+
return
536+
end
537+
if not self.configure_preset then
538+
return
539+
end
540+
local configure_preset =
541+
presets:get_configure_preset(self.configure_preset, { include_hidden = true })
542+
if not configure_preset then
543+
return
544+
end
545+
546+
if not self.build_preset then
547+
return
548+
end
549+
local build_preset = presets:get_build_preset(self.build_preset)
550+
if not build_preset then
551+
return
552+
end
553+
local build_target = build_preset:get_build_target()
554+
if build_target ~= "" then
555+
self.build_target = build_target
556+
end
557+
end
558+
559+
function Config:update_build_directory()
560+
local kits_config = kits.parse(self.cmake_kits_path, self.cwd)
561+
if kits_config then
562+
local build_dir = self:prepare_build_directory(kits_config)
563+
self:update_build_dir(build_dir, self:no_expand_build_directory_path())
564+
return
565+
end
566+
567+
local presets = Presets:parse(self.cwd)
568+
if presets then
569+
if not self.configure_preset then
570+
return
571+
end
572+
local configure_preset =
573+
presets:get_configure_preset(self.configure_preset, { include_hidden = true })
574+
if not configure_preset then
575+
return
576+
end
577+
578+
local build_directory, no_expand_build_directory =
579+
configure_preset.binaryDirExpanded, configure_preset.binaryDir
580+
if build_directory ~= "" then
581+
self:update_build_dir(build_directory, no_expand_build_directory)
582+
end
583+
end
584+
end
585+
485586
return Config

lua/cmake-tools/init.lua

Lines changed: 50 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ function cmake.generate(opt, callback)
166166
return
167167
end
168168

169-
cmake.update_build_type()
169+
config:update_build_type()
170170

171171
local build_directory, no_expand_build_directory = preset.binaryDirExpanded, preset.binaryDir
172172
if build_directory ~= "" then
@@ -368,8 +368,8 @@ function cmake.build(opt, callback)
368368
if config.build_preset then
369369
local build_preset = presets:get_build_preset(config.build_preset)
370370
if build_preset then
371-
cmake.update_build_target(build_preset)
372-
cmake.update_build_type()
371+
config:update_build_target()
372+
config:update_build_type()
373373
end
374374
end
375375
end
@@ -880,7 +880,7 @@ function cmake.select_build_preset(callback)
880880

881881
local build_preset = presets:get_build_preset(choice)
882882
if build_preset then
883-
cmake.update_build_target(build_preset)
883+
config:update_build_target()
884884
end
885885
end
886886
local associated_configure_preset = presets:get_configure_preset(
@@ -1046,8 +1046,20 @@ end
10461046
function cmake.get_target_vars(target)
10471047
local vars = cmake.get_base_vars()
10481048

1049-
local model = config:get_code_model_info()[target]
1049+
config:update_build_directory()
1050+
1051+
local codemodel = config:get_code_model_info()
1052+
if not codemodel then
1053+
return
1054+
end
1055+
local model = codemodel[target]
1056+
if not model then
1057+
return
1058+
end
10501059
local result = config:get_launch_target_from_info(model)
1060+
if result.code ~= Types.SUCCESS then
1061+
return
1062+
end
10511063
vars.dir.binary = utils.get_path(result.data)
10521064
return vars
10531065
end
@@ -1117,8 +1129,13 @@ function cmake.target_settings(opt)
11171129
inherit_base_environment = true,
11181130
env = {},
11191131
})
1132+
local target_vars = cmake.get_target_vars(target)
1133+
if not target_vars then
1134+
log.info("Target has not been configured!")
1135+
return
1136+
end
11201137

1121-
local content = "local vars = " .. vim.inspect(cmake.get_target_vars(target))
1138+
local content = "local vars = " .. vim.inspect(target_vars)
11221139
content = content .. "\nreturn " .. vim.inspect(config.target_settings[target])
11231140

11241141
window.set_content(content)
@@ -1159,32 +1176,33 @@ function cmake.run_test(opt, callback)
11591176
end
11601177

11611178
local env = environment.get_build_environment(config)
1162-
local all_tests = ctest.list_all_tests(config:build_directory_path())
1163-
if #all_tests == 0 then
1164-
return
1165-
end
1166-
table.insert(all_tests, 1, "all")
1167-
vim.ui.select(
1168-
all_tests,
1169-
{ prompt = "select test to run" },
1170-
vim.schedule_wrap(function(_, idx)
1171-
if not idx then
1172-
return
1173-
end
1174-
if idx == 1 then
1175-
ctest.run(const.ctest_command, "'.*'", config:build_directory_path(), env, config, opt)
1176-
else
1177-
ctest.run(
1178-
const.ctest_command,
1179-
all_tests[idx],
1180-
config:build_directory_path(),
1181-
env,
1182-
config,
1183-
opt
1184-
)
1185-
end
1186-
end)
1187-
)
1179+
ctest.list_all_tests(config:build_directory_path(), function(all_tests)
1180+
if #all_tests == 0 then
1181+
return
1182+
end
1183+
table.insert(all_tests, 1, "all")
1184+
vim.ui.select(
1185+
all_tests,
1186+
{ prompt = "select test to run" },
1187+
vim.schedule_wrap(function(_, idx)
1188+
if not idx then
1189+
return
1190+
end
1191+
if idx == 1 then
1192+
ctest.run(const.ctest_command, "'.*'", config:build_directory_path(), env, config, opt)
1193+
else
1194+
ctest.run(
1195+
const.ctest_command,
1196+
all_tests[idx],
1197+
config:build_directory_path(),
1198+
env,
1199+
config,
1200+
opt
1201+
)
1202+
end
1203+
end)
1204+
)
1205+
end)
11881206
end
11891207

11901208
function cmake.run_current_file(opt)
@@ -1868,56 +1886,4 @@ function cmake.register_telescope_function()
18681886
end
18691887
end
18701888

1871-
function cmake.update_build_target(build_preset)
1872-
local build_target = build_preset:get_build_target()
1873-
if build_target ~= "" then
1874-
config.build_target = build_target
1875-
end
1876-
end
1877-
1878-
function cmake.update_build_type()
1879-
local presets = Presets:parse(config.cwd)
1880-
if not presets then
1881-
return
1882-
end
1883-
if not config.configure_preset then
1884-
return
1885-
end
1886-
local configure_preset =
1887-
presets:get_configure_preset(config.configure_preset, { include_hidden = true })
1888-
if not configure_preset then
1889-
return
1890-
end
1891-
1892-
config.build_type = configure_preset:get_build_type()
1893-
1894-
if not config.build_preset then
1895-
return
1896-
end
1897-
local build_preset = presets:get_build_preset(config.build_preset)
1898-
if not build_preset then
1899-
return
1900-
end
1901-
local configuration_types = configure_preset:get_build_configuration_types()
1902-
1903-
if not configuration_types then
1904-
return
1905-
end
1906-
local build_type_from_build_preset = build_preset:get_build_type()
1907-
1908-
if not build_type_from_build_preset then
1909-
return
1910-
end
1911-
local exists = false
1912-
for _, Item in ipairs(configuration_types) do
1913-
if Item == build_type_from_build_preset then
1914-
exists = true
1915-
break
1916-
end
1917-
end
1918-
if exists then
1919-
config.build_type = build_type_from_build_preset
1920-
end
1921-
end
1922-
19231889
return cmake

lua/cmake-tools/test/ctest.lua

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
local Job = require("plenary.job")
22
local utils = require("cmake-tools.utils")
33
local const = require("cmake-tools.const")
4-
local terminal = require("cmake-tools.terminal")
54

6-
local ctest = {}
5+
local ctest = {
6+
job = nil,
7+
}
78

8-
function ctest.list_all_tests(build_dir)
9+
function ctest.list_all_tests(build_dir, callback)
910
local result = {}
1011

11-
Job:new({
12+
ctest.job = Job:new({
1213
command = "ctest",
1314
args = { "--test-dir", build_dir, "--show-only=json-v1" },
1415
on_exit = function(j, _, _)
15-
local json_data = ""
16+
vim.schedule(function()
17+
local json_data = ""
1618

17-
for _, v in pairs(j:result()) do
18-
json_data = json_data .. v
19-
end
19+
for _, v in pairs(j:result()) do
20+
json_data = json_data .. v
21+
end
2022

21-
result = json_data
22-
end,
23-
}):sync()
24-
25-
result = vim.fn.json_decode(result)
23+
result = vim.fn.json_decode(json_data)
2624

27-
local tests = {}
28-
for _, item in ipairs(result.tests) do
29-
table.insert(tests, item["name"])
30-
end
25+
local tests = {}
26+
for _, item in ipairs(result.tests) do
27+
table.insert(tests, item["name"])
28+
end
29+
callback(tests)
30+
end)
31+
end,
32+
})
3133

32-
return tests
34+
ctest.job:start()
3335
end
3436

3537
function ctest.run(ctest_command, test_name, build_dir, env, config, opt)
@@ -40,4 +42,15 @@ function ctest.run(ctest_command, test_name, build_dir, env, config, opt)
4042
utils.run(cmd, config.env_script, env, args, config.cwd, config.runner, nil)
4143
end
4244

45+
function ctest.stop()
46+
if not ctest.job or ctest.job.is_shut_down then
47+
return
48+
end
49+
ctest.job:shutdown(1, 9)
50+
51+
for _, pid in ipairs(vim.api.nvim_get_proc_children(ctest.job.pid)) do
52+
vim.loop.kill(pid, 9)
53+
end
54+
end
55+
4356
return ctest

0 commit comments

Comments
 (0)