Skip to content

Commit

Permalink
✨feat(meson): Build automation utility added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeioth committed Mar 23, 2024
1 parent c65dc4c commit b34e79d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
44 changes: 44 additions & 0 deletions lua/compiler/bau/meson.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--- meson.build bau actions

local M = {}

-- Backend - overseer tasks performed on option selected
function M.action(option)
local overseer = require("overseer")
local final_message = "--task finished--"

-- Global: MESON_BUILD_ROOT
local success, build_dir = pcall(vim.api.nvim_get_var, 'MESON_BUILD_ROOT')
if not success or build_dir == "" then build_dir = './build' end

-- Global: MESON_BUILD_TYPE
local success, build_type = pcall(vim.api.nvim_get_var, 'MESON_BUILD_TYPE')
if not success or build_type == "" then build_type = '"debug"' end

-- Global: MESON_CLEAN_FIRST
local clean_first_arg = ""
local _, clean_first = pcall(vim.api.nvim_get_var, 'MESON_CLEAN_FIRST')
if clean_first == "true" then clean_first_arg = "--wipe" end

-- Run command
local cmd_setup = "meson setup " .. clean_first_arg .. build_dir .. " --buildtype=" .. build_type
local cmd_build = "meson compile -C " .. build_dir .. " " .. option
local cmd_target = "ninja -C " .. build_dir .. " " .. option
print(cmd_build)
local task = overseer.new_task({
name = "- Mason interpreter",
strategy = { "orchestrator",
tasks = {{ "shell", name = "- Run Meson → " .. option,
cmd = "mkdir -p " .. build_dir ..
" && " .. cmd_setup .. -- Setup
" && " .. cmd_build .. -- Build target from the 'build' directory.
--" && " .. cmd_target .. -- Run target
" && echo '" .. cmd_setup .. " && " .. cmd_build .. "'" .. -- echo
" && echo '" .. final_message .. "'"
},},},})
task:start()
vim.cmd("OverseerOpen")
end

return M

64 changes: 63 additions & 1 deletion lua/compiler/utils-bau.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,62 @@ local function get_cmake_opts(path)
return options
end


--- Given a Mesonfile, parse all the options,
--- and return them as a table.
--- @param path string Path to the meson.build
--- @return table options A table like:
--- { { text = "Meson hello", value = "hello", description = "Print Hello, World!", bau = "meson" }, ...}
local function get_meson_opts(path)
local options = {}

local file = io.open(path, "r")

if file then
local in_command = false

for line in file:lines() do
-- Parse 'executable' commands
local target = line:match("^%s*([%w_]-)%s*=%s*executable%s*%('%s*([%w_]+)'")
or line:match("^%s*executable%s*%('%s*([%w_]+)'")

if target then
in_command = true
local target_name = line:match("%('%s*([^']+)'%s*")
if target_name then
table.insert(
options,
{ text = "Meson " .. target_name, value = target_name, bau = "meson" }
)
end
elseif in_command then
in_command = false
end

-- Parse 'custom_target' commands
local custom_target = line:match("^%s*([%w_]-)%s*=%s*custom_target%s*%('%s*([%w_]+)'")
or line:match("^%s*custom_target%s*%('%s*([%w_]+)'")

if custom_target then
in_command = true
local target_name = line:match("%('([%w_']+)'")
if target_name then
table.insert(
options,
{ text = "Meson " .. target_name, value = target_name, bau = "meson" }
)
end
elseif in_command then
in_command = false
end
end

file:close()
end

return options
end

---Given a build.gradle.kts file, parse all the tasks,
---and return them as a table.
---
Expand Down Expand Up @@ -267,6 +323,11 @@ function M.get_bau_opts()
working_dir .. utils.os_path("/CMakeLists.txt")
))

-- meson
vim.list_extend(options, get_meson_opts(
working_dir .. utils.os_path("/meson.build")
))

-- gradle
vim.list_extend(options, get_gradle_opts(
working_dir .. utils.os_path("/build.gradle.kts")
Expand All @@ -290,7 +351,8 @@ function M.require_bau(bau)
local module_file_path = utils.os_path(local_path_dir .. "bau/" .. bau .. ".lua")
local success, bau = pcall(dofile, module_file_path)

if success then return bau
if success then
return bau
else
-- local error = "Build automation utilities \"" .. bau .. "\" not supported by the compiler."
-- vim.notify(error, vim.log.levels.INFO, { title = "Build automation utilities unsupported" })
Expand Down

0 comments on commit b34e79d

Please sign in to comment.