aboutsummaryrefslogtreecommitdiff
path: root/lua/cmake-explorer/project.lua
blob: bdd54e379832d055df7914e6eb0ebbf6ac152e84 (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
local config = require("cmake-explorer.config")
local capabilities = require("cmake-explorer.capabilities")
local FileApi = require("cmake-explorer.file_api")
local Path = require("plenary.path")
local Scandir = require("plenary.scandir")
local utils = require("cmake-explorer.utils")
local notif = require("cmake-explorer.notification")

local Project = {}

Project.__index = Project

function Project:new(o)
	o = o or {}

	local path
	if type(o) == "string" then
		path = o
	elseif type(o) == "table" and o.path then
		path = o.path
	else
		return
	end

	if not Path:new(path, "CMakeLists.txt"):exists() then
		return
	end

	local obj = {
		path = Path:new(path):absolute(),
		fileapis = {},
		last_generate = {},
	}
	setmetatable(obj, Project)
	return obj
end

function Project:scan_build_dirs()
	local candidates = Scandir.scan_dir(self.path, { hidden = false, only_dirs = true, depth = 0, silent = true })
	for _, v in ipairs(candidates) do
		local fa = FileApi:new(v)
		if fa and fa:exists() and fa:read_reply() then
			self.fileapis[v] = fa
		end
	end
end

function Project:symlink_compile_commands(path)
	local src = Path:new(path, "compile_commands.json")
	if src:exists() then
		vim.cmd(
			'silent exec "!'
			.. config.cmake_cmd
			.. " -E create_symlink "
			.. src:absolute()
			.. " "
			.. Path:new(self.path, "compile_commands.json"):absolute()
			.. '"'
		)
	end
end

function Project:configure(params)
	params = params or {}
	local args = utils.generate_args(params, self.path)
	local build_dir = utils.build_path(params, self.path)
	if not args then
		return
	end
	if not self.fileapis[build_dir] then
		local fa = FileApi:new(build_dir)
		if not fa then
			notif.notify("Cannot fileapi object", vim.log.levels.ERROR)
			return
		end
		if not fa:create() then
			return
		end
		self.fileapis[build_dir] = fa
	end

	return {
		cmd = config.cmake_cmd,
		args = args,
		cwd = Path:new(self.path):absolute(),
		after_success = function()
			self.last_generate = build_dir
			self.fileapis[build_dir]:read_reply()
			self:symlink_compile_commands(build_dir)
		end,
	}
end

function Project:configure_last()
	return self:configure(self.last_generate)
end

function Project:list_build_dirs()
	local ret = {}
	for k, _ in pairs(self.fileapis) do
		local build = {}
		build.path = k
		table.insert(ret, build)
	end
	return ret
end

return Project