diff options
Diffstat (limited to 'lua/cmake-explorer/utils.lua')
-rw-r--r-- | lua/cmake-explorer/utils.lua | 103 |
1 files changed, 92 insertions, 11 deletions
diff --git a/lua/cmake-explorer/utils.lua b/lua/cmake-explorer/utils.lua index 8fff940..e944642 100644 --- a/lua/cmake-explorer/utils.lua +++ b/lua/cmake-explorer/utils.lua @@ -1,25 +1,106 @@ +local config = require("cmake-explorer.config") +local capabilities = require("cmake-explorer.capabilities") +local Path = require("plenary.path") + local utils = { plugin_prefix = "CM", } -function utils.with_prefix(command) - return utils.plugin_prefix .. command +utils.build_dir_name = function(params) + if capabilities.is_multiconfig_generator(params.generator) then + return config.build_dir_template[1] + else + local paths = {} + for k, v in ipairs(config.build_dir_template) do + local path = v:gsub("${buildType}", params.build_type) + if k ~= 1 and config.build_dir_template.case then + if config.build_dir_template.case == "lower" then + path = string.lower(path) + elseif config.build_dir_template.case == "upper" then + path = string.upper(path) + end + end + table.insert(paths, path) + end + return table.concat(paths, config.build_dir_template.sep) + end end -function utils.has_value(tab, val) - for index, value in ipairs(tab) do - if type(val) == "function" then - if val(value) then - return true - end +utils.build_path = function(params, source_dir) + if type(params) == "string" then + return params + end + local build_path = Path:new(config.build_dir) + if build_path:is_absolute() then + return (build_path / utils.build_dir_name(params)):absolute() + else + return Path:new(source_dir, build_path, utils.build_dir_name(params)):absolute() + end +end + +utils.generate_args = function(params, source_dir) + local ret = {} + + if type(params) == "string" then + table.insert(ret, "-B" .. Path:new(params):make_relative(source_dir)) + else + if params.preset then + table.insert(ret, "--preset " .. params.preset) else - if value == val then - return true + if params.generator and vim.tbl_contains(capabilities.generators(), params.generator) then + table.insert(ret, "-G" .. params.generator) + end + + params.build_type = params.build_type or config.build_types[1] + if params.build_type then + table.insert(ret, "-DCMAKE_BUILD_TYPE=" .. params.build_type) + end + + table.insert(ret, "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON") + + if type(params.args) == "table" then + for k, v in pairs(params.args) do + table.insert(ret, "-D" .. k .. "=" .. v) + end + elseif type(params.args) == "string" then + table.insert(ret, params.args) end + table.insert(ret, "-B" .. Path:new(utils.build_path(params, source_dir)):make_relative(source_dir)) + end + end + return ret +end + +utils.is_eq = function(val, cmp, if_eq, if_not_eq) + if val == cmp then + if if_eq then + return if_eq + else + return val + end + else + if if_not_eq then + return if_not_eq + else + return nil end end +end - return false +utils.is_neq = function(val, cmp, if_eq, if_not_eq) + if val ~= cmp then + if if_eq then + return if_eq + else + return val + end + else + if if_not_eq then + return if_not_eq + else + return nil + end + end end return utils |