From b34e79d4afb371b718a5c2b8dcbc638aa75411c1 Mon Sep 17 00:00:00 2001 From: Zeioth Date: Sat, 23 Mar 2024 03:55:44 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat(`meson`):=20Build=20automation=20?= =?UTF-8?q?utility=20added.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/compiler/bau/meson.lua | 44 ++++++++++++++++++++++++++ lua/compiler/utils-bau.lua | 64 +++++++++++++++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 lua/compiler/bau/meson.lua diff --git a/lua/compiler/bau/meson.lua b/lua/compiler/bau/meson.lua new file mode 100644 index 0000000..1d2811b --- /dev/null +++ b/lua/compiler/bau/meson.lua @@ -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 + diff --git a/lua/compiler/utils-bau.lua b/lua/compiler/utils-bau.lua index 6b39088..3ead14a 100644 --- a/lua/compiler/utils-bau.lua +++ b/lua/compiler/utils-bau.lua @@ -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. --- @@ -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") @@ -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" })