aboutsummaryrefslogtreecommitdiff
path: root/lua/cmake/project.lua
blob: a5d6cdd072fe29d8bc8ade3536431617a7f077d5 (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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
local config = require("cmake.config")
local VariantConfig = require("cmake.variants")
local FileApi = require("cmake.fileapi")
local utils = require("cmake.utils")
local constants = require("cmake.constants")
local uv = vim.uv

local Project = {}

local initialised = false

local configs = {}
local current_config = nil
local fileapis = {}

local reset_internals = function()
	configs = {}
	current_config = nil
	fileapis = {}
	initialised = true
end

local append_after_success_actions = function()
	local read_reply = function(v, not_presented)
		if (not_presented and not fileapis[v.directory]) or not not_presented then
			utils.symlink(v.directory .. "/compile_commands.json", uv.cwd())
			fileapis[v.directory] = { targets = {} }
			FileApi.read_reply(v.directory, function(target)
				table.insert(fileapis[v.directory].targets, target)
			end)
		end
	end
	for _, v in ipairs(configs) do
		v.generate_command.after_success = function()
			read_reply(v, false)
		end
		for _, bv in ipairs(v.build_options) do
			bv.command.after_success = function()
				read_reply(v, true)
			end
		end
	end
end

local init_fileapis = function()
	fileapis = {}
	for _, v in ipairs(configs) do
		if not fileapis[v.directory] then
			fileapis[v.directory] = { targets = {} }
			FileApi.exists(v.directory, function(fileapi_exists)
				if fileapi_exists then
					FileApi.read_reply(v.directory, function(target)
						table.insert(fileapis[v.directory].targets, target)
					end)
				end
			end)
		end
	end
end

-- TODO: validate yaml and fallback to config's variants if not valid
-- TODO: make variants order more stable. at least when reading from file
function Project.from_variants(variants)
	local variants_copy = vim.deepcopy(variants)
	local list_variants = {}
	for k, v in pairs(variants_copy) do
		table.insert(list_variants, v)
		list_variants[#list_variants]._name = k
	end
	table.sort(list_variants, function(a, b)
		return a._name < b._name
	end)
	for var, is_default in VariantConfig.cartesian_product(list_variants) do
		var.current_build = 1
		table.insert(configs, var)
		current_config = not current_config and is_default and #configs or current_config
	end
	if not current_config and #configs ~= 0 then
		current_config = 1
	end
	append_after_success_actions()
	init_fileapis()
end

--NOTE: Neovim craches on this code
function Project.clear_cache(callback)
	uv.fs_unlink(vim.fs.joinpath(Project.current_directory(), "CMakeCache.txt"), function(f_err)
		assert(f_err, f_err)
		uv.fs_unlink(vim.fs.joinpath(Project.current_directory(), "CMakeFiles"), function(d_err)
			assert(d_err, d_err)
			callback()
		end)
	end)
end

function Project.generate_options(opts)
	opts = opts or {}
	return configs
end

--TODO: remove opts where it is useless
function Project.current_generate_option(opts)
	opts = opts or {}
	assert(current_config, "No current project config")
	return configs[current_config]
end

function Project.current_generate_option_idx()
	return current_config
end

function Project.set_current_generate_option(idx)
	current_config = idx
end

--TODO: check on out of range
function Project.current_build_option_idx()
	return configs[current_config].current_build
end

function Project.current_build_option()
	if not Project.current_build_option_idx() then
		return nil
	end
	return configs[current_config].build_options[Project.current_build_option_idx()]
end

function Project.set_current_build_option(idx)
	configs[current_config].current_build = idx
end

function Project.current_directory()
	return current_config and configs[current_config].directory or nil
end

local current_fileapi = function()
	if not Project.current_directory() or not fileapis[Project.current_directory()] then
		return nil
	end
	return fileapis[Project.current_directory()]
end

function Project.set_current_executable_target(idx)
	current_fileapi().current_executable_target = idx
end

function Project.current_executable_target_idx()
	local _curr_fileapi = current_fileapi()
	if not _curr_fileapi then
		return nil
	end
	return _curr_fileapi.current_executable_target
end

function Project.current_executable_target()
	local _curr_fileapi = current_fileapi()
	if not _curr_fileapi then
		return nil
	end
	local _curr_exe_target_idx = Project.current_executable_target_idx()
	if not _curr_exe_target_idx then
		return nil
	end
	return _curr_fileapi.targets[_curr_exe_target_idx]
end

function Project.current_targets(opts)
	opts = opts or {}
	local _curr_fileapi = current_fileapi()
	if not _curr_fileapi then
		return nil
	end
	if opts.type then
		return vim.tbl_filter(function(t)
			return t.type == opts.type
		end, _curr_fileapi.targets)
	end
	return _curr_fileapi.targets
end

function Project.create_fileapi_query(opts, callback)
	opts = opts or {}
	local path

	if type(opts.idx) == "number" then
		path = configs[opts.idx].directory
	elseif type(opts.path) == "string" then
		path = opts.path
		--TODO: compare getmetatable(opts.config) with VariantConfig (and PresetsConfig in future)
	elseif type(opts.config) == "table" then
		path = opts.config.directory
	else
		path = configs[current_config].directory
	end
	FileApi.query_exists(path, function(query_exists)
		if not query_exists then
			FileApi.create(path, function()
				callback()
			end)
		else
			callback()
		end
	end)
end

local do_setup = function(opts)
	reset_internals()
	local variants_path = vim.fs.joinpath(uv.cwd(), constants.variants_yaml_filename)
	utils.file_exists(variants_path, function(variants_exists)
		if variants_exists then
			utils.read_file(variants_path, function(variants_data)
				local yaml = require("cmake.lyaml").eval(variants_data)
				Project.from_variants(yaml)
			end)
		else
			Project.from_variants(config.cmake.variants)
		end
	end)
end

function Project.setup(opts)
	opts = opts or {}
	if opts.first_time_only and initialised then
		return
	end
	if not initialised then
		require("cmake.capabilities").setup(function()
			do_setup(opts)
		end)
	else
		do_setup(opts)
	end
end

return Project