Skip to content

Commit 72a80af

Browse files
committed
feat(bau): Added support for build automation utils.
1 parent 260e87a commit 72a80af

File tree

3 files changed

+179
-6
lines changed

3 files changed

+179
-6
lines changed

lua/compiler/bau/make.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--- Run the selected make option using overseer
2+
3+
local M = {}
4+
5+
-- Backend
6+
function M.action(option)
7+
local overseer = require("overseer")
8+
local final_message = "--task finished--"
9+
local task = overseer.new_task({
10+
name = "- Make interpreter",
11+
strategy = { "orchestrator",
12+
tasks = {{ "shell", name = "- Run makefile → make " .. option ,
13+
cmd = "make ".. option .. -- run
14+
" && echo make " .. option .. -- echo
15+
" && echo '" .. final_message .. "'"
16+
},},},})
17+
task:start()
18+
vim.cmd("OverseerOpen")
19+
end
20+
21+
return M

lua/compiler/telescope.lua

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,83 @@ function M.show()
1818
local pickers = require "telescope.pickers"
1919
local finders = require "telescope.finders"
2020
local utils = require("compiler.utils")
21+
local utils_bau = require("compiler.utils-bau")
2122

2223
local buffer = vim.api.nvim_get_current_buf()
23-
local filetype = vim.api.nvim_buf_get_option(buffer, "filetype")
24+
local filetype = vim.api.nvim_get_option_value("filetype", { buf = buffer })
25+
26+
27+
-- POPULATE
28+
-- ========================================================================
2429

2530
-- Programatically require the backend for the current language.
26-
-- On unsupported languages, allow "Run Makefile".
2731
local language = utils.require_language(filetype)
28-
if not language then language = require("compiler.languages.make") end
2932

30-
--- On option selected → Run action depending of the language
33+
-- On unsupported languages, default to make.
34+
if not language then language = utils.require_language("make") or {} end
35+
36+
-- Also show options discovered on Makefile, Cmake... and other bau.
37+
if not language.bau_added then
38+
language.bau_added = true
39+
local bau_opts = utils_bau.get_bau_opts()
40+
41+
-- Insert a separator on telescope for every bau.
42+
local last_bau_value = nil
43+
for _, item in ipairs(bau_opts) do
44+
if last_bau_value ~= item.bau then
45+
table.insert(language.options, { text = "", value = "separator" })
46+
last_bau_value = item.bau
47+
end
48+
table.insert(language.options, item)
49+
end
50+
end
51+
52+
-- Add numbers in front of the options to display.
53+
local index_counter = 0
54+
for _, option in ipairs(language.options) do
55+
if option.value ~= "separator" then
56+
index_counter = index_counter + 1
57+
option.text = index_counter .. " - " .. option.text
58+
end
59+
end
60+
61+
62+
-- RUN ACTION ON SELECTED
63+
-- ========================================================================
64+
65+
--- On option selected → Run action depending of the language.
3166
local function on_option_selected(prompt_bufnr)
3267
actions.close(prompt_bufnr) -- Close Telescope on selection
3368
local selection = state.get_selected_entry()
3469
if selection.value == "" then return end -- Ignore separators
3570
_G.compiler_redo = selection.value -- Save redo
3671
_G.compiler_redo_filetype = filetype -- Save redo
37-
if selection then language.action(selection.value) end
72+
73+
74+
if selection then
75+
-- Do the selected option belong to a build automation utility?
76+
local bau = nil
77+
for _, value in ipairs(language.options) do
78+
if value.text == selection.display then
79+
bau = value.bau
80+
end
81+
end
82+
83+
-- If any → call the bau backend.
84+
-- If nil → call the language backend.
85+
if bau then
86+
bau = utils_bau.require_bau(bau)
87+
if bau then bau.action(selection.value) end
88+
else
89+
language.action(selection.value)
90+
end
91+
92+
end
3893
end
3994

40-
--- Show telescope
95+
96+
-- SHOW TELESCOPE
97+
-- ========================================================================
4198
local function open_telescope()
4299
pickers
43100
.new({}, {

lua/compiler/utils-bau.lua

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
--- ### Utils-bau for build automation systems
2+
3+
--- Frontend compatilibity:
4+
--- The parsers in this file return data in a format Telescope can undestand.
5+
--- If you want to support other frontend, write an adapter function
6+
--- to convert the data format returned by the parsers as needed.
7+
8+
local M = {}
9+
local utils = require("compiler.utils")
10+
11+
12+
-- PARSERS
13+
-- Private functions to parse bau files.
14+
-- ============================================================================
15+
16+
---Given a file, open the makefile, extract all the options,
17+
-- and return them as a table.
18+
---@param path string Path to the Makefile.
19+
---@return table options A table like:
20+
--- { { text: "my option", value="all" bau = "make"}, { text: "another option", value="hello", "make"} ...}
21+
local function get_makefile_opts(path)
22+
local options = {}
23+
24+
-- Open the Makefile for reading
25+
local file = io.open(path, "r")
26+
27+
if file then
28+
local in_target = false
29+
30+
-- Iterate through each line in the Makefile
31+
for line in file:lines() do
32+
-- Check for lines starting with a target rule (e.g., "target: dependencies")
33+
local target = line:match "^(.-):"
34+
if target then
35+
in_target = true
36+
-- Exclude the ":" and add the option to the list with text and value fields
37+
table.insert(
38+
options,
39+
{ text = "Make " .. target, value = target, bau = "make" }
40+
)
41+
elseif in_target then
42+
-- If we're inside a target block, stop adding options
43+
in_target = false
44+
end
45+
end
46+
47+
-- Close the Makefile
48+
file:close()
49+
end
50+
51+
return options
52+
end
53+
54+
55+
-- FRONTEND
56+
-- Public functions to call from the frontend.
57+
-- ============================================================================
58+
59+
---Function that call all bau function and combine the result in a table.
60+
---@return table options A table that contain
61+
--- the options of all bau available in the current working directory.
62+
--- Empty table {} if none is found.
63+
function M.get_bau_opts()
64+
local current_dir = vim.fn.expand("%:p:h")
65+
local options = {}
66+
67+
vim.list_extend(
68+
options,
69+
get_makefile_opts(
70+
current_dir .. utils.os_path("/Makefile")
71+
)
72+
)
73+
74+
return options
75+
end
76+
77+
---Programatically require a bau backend,
78+
-- responsible for running the action selected by the user in the frontend.
79+
---@return table|nil bau The bau backend,
80+
--- or nil, if ./bau/<bau>.lua doesn't exist.
81+
function M.require_bau(bau)
82+
local local_path = debug.getinfo(1, "S").source:sub(2)
83+
local local_path_dir = local_path:match("(.*[/\\])")
84+
local module_file_path = utils.os_path(local_path_dir .. "bau/" .. bau .. ".lua")
85+
local success, bau = pcall(dofile, module_file_path)
86+
87+
if success then return bau
88+
else
89+
-- local error = "Build automation system \"" .. bau .. "\" not supported by the compiler."
90+
-- vim.notify(error, vim.log.levels.INFO, { title = "Build automation system unsupported" })
91+
return nil
92+
end
93+
end
94+
95+
return M

0 commit comments

Comments
 (0)