aboutsummaryrefslogtreecommitdiff
path: root/lua/cmake/actions.lua
blob: 3c09d93490d99cc41781dbac64e28bd272749cc3 (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
local pr = require("cmake.project")
local config = require("cmake.config")
local t = require("cmake.terminal")
local utils = require("cmake.utils")
local constants = require("cmake.constants")
local Path = require("plenary.path")

local M = {}

local default_generate_exe_opts = {
	notify = {
		ok_message = "CMake build finished",
		err_message = function(code)
			return "CMake generate failed with code " .. tostring(code)
		end,
	},
}

local default_build_exe_opts = {
	notify = {
		ok_message = "CMake build finished",
		err_message = function(code)
			return "CMake build failed with code " .. tostring(code)
		end,
	},
}

M.generate = function(opts)
	pr.create_fileapi_query({ idx = pr.current_generate_option_idx() }, function()
		vim.schedule(function()
			t.cmake_execute(pr.current_generate_option().generate_command, default_generate_exe_opts)
		end)
	end)
end

M.generate_select = function(opts)
	local items = pr.generate_options(opts)
	vim.ui.select(items, {
		prompt = "Select configuration to generate:",
		format_item = function(item)
			return table.concat(item.name, config.variants_display.short.sep)
		end,
	}, function(choice, idx)
		if not idx then
			return
		end
		pr.set_current_generate_option(idx)
		pr.create_fileapi_query({ idx = idx }, function()
			vim.schedule(function()
				t.cmake_execute(choice.generate_command, default_generate_exe_opts)
			end)
		end)
	end)
end

M.build = function(opts)
	if not pr.current_build_option_idx() then
		M.build_select(opts)
	else
		pr.create_fileapi_query({ idx = pr.current_build_option_idx() }, function()
			vim.schedule(function()
				t.cmake_execute(pr.current_build_option().command, default_build_exe_opts)
			end)
		end)
	end
end

M.build_select = function(opts)
	local items = pr.current_generate_option(opts).build_options
	vim.ui.select(items, {
		prompt = "Select build option to generate:",
		format_item = function(item)
			return table.concat(item.name, config.variants_display.short.sep)
		end,
	}, function(choice, idx)
		if not idx then
			return
		end
		pr.set_current_build_option(idx)
		pr.create_fileapi_query({ idx = idx }, function()
			vim.schedule(function()
				t.cmake_execute(choice.command, default_build_exe_opts)
			end)
		end)
	end)
end

M.run_tagret = function(opts)
	opts = opts or {}
	local _curr_exe_cmd = pr.current_executable_target()
	if not _curr_exe_cmd then
		M.run_tagret_select(opts)
	else
		local command = {
			cmd = Path:new(pr.current_directory(), _curr_exe_cmd.path):make_relative(vim.loop.cwd()),
		}
		t.target_execute(command)
	end
end

M.run_tagret_select = function(opts)
	opts = opts or {}
	opts.type = "EXECUTABLE"
	local items = pr.current_targets(opts)
	vim.ui.select(items, {
		prompt = "Select tagret to run:",
		format_item = function(item)
			return item.name
		end,
	}, function(choice, idx)
		if not idx then
			return
		end
		pr.set_current_executable_target(idx)
		local command = {
			cmd = Path:new(pr.current_directory(), choice.path):make_relative(vim.loop.cwd()),
		}
		t.target_execute(command)
	end)
end

M.toggle = function()
	t.cmake_toggle()
end

M.edit_variants = function()
	utils.file_exists(constants.variants_yaml_filename, function(variants_exists)
		if variants_exists then
			vim.schedule(function()
				vim.cmd(string.format("e %s", constants.variants_yaml_filename))
			end)
		else
			local default_yaml = require("cmake.lyaml").dump(config.cmake.variants)
			utils.write_file(constants.variants_yaml_filename, default_yaml, function()
				vim.schedule(function()
					vim.cmd(string.format("e %s", constants.variants_yaml_filename))
				end)
			end)
		end
	end)
end

return M