aboutsummaryrefslogtreecommitdiff
path: root/lua/cmake-explorer/utils.lua
blob: e944642d1b27773529f1c5a87d0f952e577f5e31 (plain)
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
local config = require("cmake-explorer.config")
local capabilities = require("cmake-explorer.capabilities")
local Path = require("plenary.path")

local utils = {
	plugin_prefix = "CM",
}

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

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 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

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