Skip to content

Commit 6973167

Browse files
DenzellceWolf
authored andcommitted
refactor(Config): instance based values
1 parent 304ffaa commit 6973167

2 files changed

Lines changed: 90 additions & 43 deletions

File tree

lua/cmake-tools/config.lua

Lines changed: 89 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,57 @@ local variants = require("cmake-tools.variants")
77
local Presets = require("cmake-tools.presets")
88
local kits = require("cmake-tools.kits")
99

10-
local Config = {
11-
build_directory = nil,
12-
query_directory = nil,
13-
reply_directory = nil,
14-
build_type = nil,
15-
variant = nil,
16-
build_target = nil,
17-
launch_target = nil,
18-
kit = nil,
19-
configure_preset = nil,
20-
build_preset = nil,
21-
test_preset = nil,
22-
selected_test = nil,
23-
base_settings = {
24-
env = {},
25-
build_dir = "",
26-
working_dir = "${dir.binary}",
27-
use_preset = true,
28-
generate_options = {},
29-
build_options = {},
30-
show_disabled_build_presets = true,
31-
ctest_show_labels = false,
32-
}, -- general config
33-
target_settings = {}, -- target specific config
34-
executor = nil,
35-
runner = nil,
36-
env_script = " ",
37-
cwd = vim.loop.cwd(),
38-
}
39-
10+
---@class Config.BaseSettings
11+
---@field env table
12+
---@field build_dir string
13+
---@field working_dir string
14+
---@field use_preset boolean
15+
---@field generate_options string[]
16+
---@field build_options string[]
17+
---@field show_disabled_build_presets boolean
18+
---@field ctest_show_labels boolean
19+
20+
---@class Config
21+
---@field build_directory Path?
22+
---@field query_directory Path?
23+
---@field reply_directory Path?
24+
---@field build_type string?
25+
---@field variant table?
26+
---@field build_target string|string[]|nil
27+
---@field launch_target string?
28+
---@field kit string?
29+
---@field configure_preset string?
30+
---@field build_preset string?
31+
---@field test_preset string?
32+
---@field selected_test string?
33+
---@field base_settings Config.BaseSettings
34+
---@field target_settings table
35+
---@field executor table?
36+
---@field runner table?
37+
---@field env_script string
38+
---@field cwd string
39+
local Config = {}
40+
Config.__index = Config
41+
42+
---@param const Const
43+
---@return Config
4044
function Config:new(const)
41-
local obj = {}
42-
setmetatable(obj, { __index = self }) -- when obj cannot find key in its table, it will try to find it from its __index value
45+
local obj = {
46+
base_settings = {
47+
env = {},
48+
build_dir = "",
49+
working_dir = "${dir.binary}",
50+
use_preset = true,
51+
generate_options = {},
52+
build_options = {},
53+
show_disabled_build_presets = true,
54+
ctest_show_labels = false,
55+
}, -- general config
56+
target_settings = {}, -- target specific config
57+
env_script = " ",
58+
cwd = vim.loop.cwd(),
59+
}
60+
setmetatable(obj, Config)
4361

4462
obj:update_build_dir(const.cmake_build_directory, const.cmake_build_directory)
4563

@@ -56,21 +74,22 @@ function Config:new(const)
5674
return obj
5775
end
5876

77+
---@return string
5978
function Config:build_directory_path()
6079
return self.build_directory.filename
6180
end
6281

82+
---@return boolean
6383
function Config:has_build_directory()
6484
return self.build_directory and self.build_directory:exists()
6585
end
6686

67-
---comment
6887
---The reason for storing no expand build directory is to make cwd selecting easier
88+
---@return string
6989
function Config:no_expand_build_directory_path()
7090
return self.base_settings.build_dir
7191
end
7292

73-
---comment
7493
---@param build_dir string|function string or a function returning string containing path to the build dir
7594
---@param no_expand_build_dir string|function
7695
function Config:update_build_dir(build_dir, no_expand_build_dir)
@@ -107,8 +126,9 @@ function Config:update_build_dir(build_dir, no_expand_build_dir)
107126
self.base_settings.build_dir = Path:new(no_expand_build_dir):absolute()
108127
end
109128

110-
---Prepare build directory. Which allows macro expansion.
129+
---Prepare build directory with macro expansion
111130
---@param kit_list table all the kits
131+
---@return string
112132
function Config:prepare_build_directory(kit_list)
113133
-- macro expansion:
114134
-- ${kit}
@@ -140,22 +160,27 @@ function Config:prepare_build_directory(kit_list)
140160
return build_dir
141161
end
142162

163+
---@return string[]
143164
function Config:generate_options()
144165
return self.base_settings.generate_options and self.base_settings.generate_options or {}
145166
end
146167

168+
---@return string[]
147169
function Config:build_options()
148170
return self.base_settings.build_options and self.base_settings.build_options or {}
149171
end
150172

173+
---@return boolean
151174
function Config:show_disabled_build_presets()
152175
return self.base_settings.show_disabled_build_presets
153176
end
154177

178+
---@return boolean
155179
function Config:ctest_show_labels()
156180
return self.base_settings.ctest_show_labels
157181
end
158182

183+
---@return cmake.Result
159184
function Config:generate_build_directory()
160185
local build_directory = Path:new(self.build_directory)
161186

@@ -165,6 +190,7 @@ function Config:generate_build_directory()
165190
return self:generate_query_files()
166191
end
167192

193+
---@return cmake.Result
168194
function Config:generate_query_files()
169195
local query_directory = Path:new(self.query_directory)
170196
if not query_directory:mkdir({ parents = true }) then
@@ -196,6 +222,7 @@ function Config:generate_query_files()
196222
return Result:new(Types.SUCCESS, true, "yeah, that could be")
197223
end
198224

225+
---@return cmake.Result
199226
function Config:get_cmake_files()
200227
-- if reply_directory exists
201228
local reply_directory = Path:new(self.reply_directory)
@@ -212,6 +239,7 @@ function Config:get_cmake_files()
212239
return Result:new(Types.SUCCESS, codemodel_json["inputs"], "find it")
213240
end
214241

242+
---@return cmake.Result
215243
function Config:get_codemodel_targets()
216244
-- if reply_directory exists
217245
local reply_directory = Path:new(self.reply_directory)
@@ -233,12 +261,15 @@ function Config:get_codemodel_targets()
233261
return Result:new(Types.SUCCESS, codemodel_json["configurations"][1]["targets"], "find it") -- Return the first else
234262
end
235263

264+
---@param codemodel_target table
265+
---@return table
236266
function Config:get_code_model_target_info(codemodel_target)
237267
local reply_directory = Path:new(self.reply_directory)
238268
return vim.json.decode((reply_directory / codemodel_target["jsonFile"]):read())
239269
end
240270

241-
-- Check if launch target is built
271+
---Check if launch target is built
272+
---@return cmake.Result
242273
function Config:check_launch_target()
243274
-- 1. not configured
244275
local build_directory = Path:new(self.build_directory)
@@ -276,6 +307,8 @@ function Config:check_launch_target()
276307
)
277308
end
278309

310+
---@param target_info table
311+
---@return cmake.Result
279312
function Config:get_launch_target_from_info(target_info)
280313
local target_path = target_info["artifacts"][1]["path"]
281314
if require("cmake-tools.osys").iswin32 then
@@ -300,8 +333,8 @@ function Config:get_launch_target_from_info(target_info)
300333
return Result:new(Types.SUCCESS, target_path.filename, "yeah, that's good")
301334
end
302335

303-
-- Retrieve launch target path: self.launch_target
304-
-- it will first check if this launch target is built
336+
---Retrieve launch target path
337+
---@return cmake.Result
305338
function Config:get_launch_target()
306339
local check_result = self:check_launch_target()
307340
if check_result.code ~= Types.SUCCESS then
@@ -312,7 +345,8 @@ function Config:get_launch_target()
312345
return self:get_launch_target_from_info(target_info)
313346
end
314347

315-
-- Check if build target exists
348+
---Check if build target exists
349+
---@return cmake.Result
316350
function Config:check_build_target()
317351
-- 1. not configured
318352
local build_directory = Path:new(self.build_directory)
@@ -346,8 +380,8 @@ function Config:check_build_target()
346380
)
347381
end
348382

349-
-- Retrieve launch target path: self.launch_target
350-
-- it will first check if this launch target is built
383+
---Retrieve build target path
384+
---@return cmake.Result
351385
function Config:get_build_target()
352386
local check_result = self:check_build_target()
353387
if check_result.code ~= Types.SUCCESS then
@@ -374,8 +408,8 @@ function Config:get_build_target()
374408
return Result:new(Types.SUCCESS, target_path.filename, "yeah, that's good")
375409
end
376410

377-
-- Check if this launch target is debuggable
378-
-- use variants.debuggable
411+
---Check if this launch target is debuggable using variants.debuggable
412+
---@return cmake.Result
379413
function Config:validate_for_debugging()
380414
local build_type = self.build_type
381415

@@ -385,6 +419,9 @@ function Config:validate_for_debugging()
385419
return Result:new(Types.SUCCESS, true, "Yeah, it may be")
386420
end
387421

422+
---@param config Config
423+
---@param opt { has_all: boolean, only_executable: boolean, query_sources: boolean? }
424+
---@return cmake.Result
388425
local function get_targets(config, opt)
389426
local targets, display_targets, paths, abs_paths = {}, {}, {}, {}
390427
local sources = {}
@@ -455,6 +492,7 @@ local function get_targets(config, opt)
455492
end
456493
end
457494

495+
---@return table
458496
function Config:get_code_model_info()
459497
local codemodel_targets = self:get_codemodel_targets()
460498
if codemodel_targets.code ~= Types.SUCCESS then
@@ -472,31 +510,37 @@ function Config:get_code_model_info()
472510
return result
473511
end
474512

513+
---@return cmake.Result
475514
function Config:launch_targets()
476515
return get_targets(self, { has_all = false, only_executable = true })
477516
end
478517

518+
---@return cmake.Result
479519
function Config:build_targets()
480520
return get_targets(self, { has_all = true, only_executable = false })
481521
end
482522

523+
---@return cmake.Result
483524
function Config:launch_targets_with_sources()
484525
return get_targets(self, { has_all = false, only_executable = true, query_sources = true })
485526
end
486527

487528
local _virtual_targets = nil
529+
488530
function Config:update_targets()
489531
_virtual_targets =
490532
get_targets(self, { has_all = false, only_executable = false, query_sources = true })
491533
end
492534

535+
---@return cmake.Result?
493536
function Config:build_targets_with_sources()
494537
if not _virtual_targets then
495538
self:update_targets()
496539
end
497540
return _virtual_targets
498541
end
499542

543+
---Update build type from configure and build presets
500544
function Config:update_build_type()
501545
local presets_exists = self.base_settings.use_preset and Presets.exists(self.cwd)
502546
if not presets_exists then
@@ -546,6 +590,7 @@ function Config:update_build_type()
546590
end
547591
end
548592

593+
---Update build target from build preset
549594
function Config:update_build_target()
550595
local presets_exists = self.base_settings.use_preset and Presets.exists(self.cwd)
551596
if not presets_exists then
@@ -578,6 +623,7 @@ function Config:update_build_target()
578623
end
579624
end
580625

626+
---Update build directory from kits or configure preset
581627
function Config:update_build_directory()
582628
local kits_config = kits.parse(self.cmake_kits_path, self.cwd)
583629
if kits_config then

lua/cmake-tools/const.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local osys = require("cmake-tools.osys")
2+
---@class Const
23
local const = {
34
cmake_command = "cmake", -- this is used to specify cmake command path
45
ctest_command = "ctest", -- this is used to specify ctest command path

0 commit comments

Comments
 (0)