Skip to content

Commit b34e79d

Browse files
committed
✨feat(meson): Build automation utility added.
1 parent c65dc4c commit b34e79d

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

Diff for: lua/compiler/bau/meson.lua

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--- meson.build bau actions
2+
3+
local M = {}
4+
5+
-- Backend - overseer tasks performed on option selected
6+
function M.action(option)
7+
local overseer = require("overseer")
8+
local final_message = "--task finished--"
9+
10+
-- Global: MESON_BUILD_ROOT
11+
local success, build_dir = pcall(vim.api.nvim_get_var, 'MESON_BUILD_ROOT')
12+
if not success or build_dir == "" then build_dir = './build' end
13+
14+
-- Global: MESON_BUILD_TYPE
15+
local success, build_type = pcall(vim.api.nvim_get_var, 'MESON_BUILD_TYPE')
16+
if not success or build_type == "" then build_type = '"debug"' end
17+
18+
-- Global: MESON_CLEAN_FIRST
19+
local clean_first_arg = ""
20+
local _, clean_first = pcall(vim.api.nvim_get_var, 'MESON_CLEAN_FIRST')
21+
if clean_first == "true" then clean_first_arg = "--wipe" end
22+
23+
-- Run command
24+
local cmd_setup = "meson setup " .. clean_first_arg .. build_dir .. " --buildtype=" .. build_type
25+
local cmd_build = "meson compile -C " .. build_dir .. " " .. option
26+
local cmd_target = "ninja -C " .. build_dir .. " " .. option
27+
print(cmd_build)
28+
local task = overseer.new_task({
29+
name = "- Mason interpreter",
30+
strategy = { "orchestrator",
31+
tasks = {{ "shell", name = "- Run Meson → " .. option,
32+
cmd = "mkdir -p " .. build_dir ..
33+
" && " .. cmd_setup .. -- Setup
34+
" && " .. cmd_build .. -- Build target from the 'build' directory.
35+
--" && " .. cmd_target .. -- Run target
36+
" && echo '" .. cmd_setup .. " && " .. cmd_build .. "'" .. -- echo
37+
" && echo '" .. final_message .. "'"
38+
},},},})
39+
task:start()
40+
vim.cmd("OverseerOpen")
41+
end
42+
43+
return M
44+

Diff for: lua/compiler/utils-bau.lua

+63-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,62 @@ local function get_cmake_opts(path)
108108
return options
109109
end
110110

111+
112+
--- Given a Mesonfile, parse all the options,
113+
--- and return them as a table.
114+
--- @param path string Path to the meson.build
115+
--- @return table options A table like:
116+
--- { { text = "Meson hello", value = "hello", description = "Print Hello, World!", bau = "meson" }, ...}
117+
local function get_meson_opts(path)
118+
local options = {}
119+
120+
local file = io.open(path, "r")
121+
122+
if file then
123+
local in_command = false
124+
125+
for line in file:lines() do
126+
-- Parse 'executable' commands
127+
local target = line:match("^%s*([%w_]-)%s*=%s*executable%s*%('%s*([%w_]+)'")
128+
or line:match("^%s*executable%s*%('%s*([%w_]+)'")
129+
130+
if target then
131+
in_command = true
132+
local target_name = line:match("%('%s*([^']+)'%s*")
133+
if target_name then
134+
table.insert(
135+
options,
136+
{ text = "Meson " .. target_name, value = target_name, bau = "meson" }
137+
)
138+
end
139+
elseif in_command then
140+
in_command = false
141+
end
142+
143+
-- Parse 'custom_target' commands
144+
local custom_target = line:match("^%s*([%w_]-)%s*=%s*custom_target%s*%('%s*([%w_]+)'")
145+
or line:match("^%s*custom_target%s*%('%s*([%w_]+)'")
146+
147+
if custom_target then
148+
in_command = true
149+
local target_name = line:match("%('([%w_']+)'")
150+
if target_name then
151+
table.insert(
152+
options,
153+
{ text = "Meson " .. target_name, value = target_name, bau = "meson" }
154+
)
155+
end
156+
elseif in_command then
157+
in_command = false
158+
end
159+
end
160+
161+
file:close()
162+
end
163+
164+
return options
165+
end
166+
111167
---Given a build.gradle.kts file, parse all the tasks,
112168
---and return them as a table.
113169
---
@@ -267,6 +323,11 @@ function M.get_bau_opts()
267323
working_dir .. utils.os_path("/CMakeLists.txt")
268324
))
269325

326+
-- meson
327+
vim.list_extend(options, get_meson_opts(
328+
working_dir .. utils.os_path("/meson.build")
329+
))
330+
270331
-- gradle
271332
vim.list_extend(options, get_gradle_opts(
272333
working_dir .. utils.os_path("/build.gradle.kts")
@@ -290,7 +351,8 @@ function M.require_bau(bau)
290351
local module_file_path = utils.os_path(local_path_dir .. "bau/" .. bau .. ".lua")
291352
local success, bau = pcall(dofile, module_file_path)
292353

293-
if success then return bau
354+
if success then
355+
return bau
294356
else
295357
-- local error = "Build automation utilities \"" .. bau .. "\" not supported by the compiler."
296358
-- vim.notify(error, vim.log.levels.INFO, { title = "Build automation utilities unsupported" })

0 commit comments

Comments
 (0)