Skip to content

Commit ff31d13

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/main'
2 parents 57ca6fb + 1463f49 commit ff31d13

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

lua/compiler/utils-bau.lua

+45-3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,48 @@ local function get_meson_opts(path)
117117
return options
118118
end
119119

120+
---If a gradle.build.kts or gradle.build file exists,
121+
---parse the result of the command `gradle tasks`.
122+
---
123+
---For windows, gradle needs gradle to be install globally
124+
---and available in the PATH.
125+
---@param path string Path to the build.gradle.kts file.
126+
---@return table options A table like:
127+
--- { { text: "Gradle all", value="all", bau = "gradle"}, { text: "Gradle hello", value="hello", bau = "gradle"} ...}
128+
local function get_gradle_cmd_opts(path)
129+
-- guard clause
130+
local gradle_kts_file_exists = vim.fn.filereadable(path) == 1
131+
local gradle_file_exists = vim.fn.filereadable(vim.fn.fnamemodify(path, ':t:r')) == 1
132+
if not gradle_kts_file_exists and not gradle_file_exists then return {} end
133+
134+
-- parse
135+
local GRADLE_CMD = "gradle tasks"
136+
local UNIX_CMD = GRADLE_CMD .. " | awk '/Application tasks/,/^$/{if (!/^$/) print}' | awk 'NR > 2' | awk '!/--/ && NF {gsub(/ .*/, \"\", $0); print}' | sed '/^$/d'"
137+
local WINDOWS_CMD = "powershell -c \"" .. GRADLE_CMD .. [[ | Out-String | Select-String -Pattern '(?sm)Application tasks(.*?)(?:\r?\n){2}' | ForEach-Object { $_.Matches.Groups[1].Value -split '\r?\n' | ForEach-Object -Begin { $skip = $true } { if (-not $skip) { ($_ -split '\s+', 2)[0] } $skip = $false } | Where-Object { $_ -notmatch '--' -and $_.Trim() -ne '' } }"]]
138+
local options = {}
139+
local cmd_output = ""
140+
local is_windows = os.getenv("OS") == "Windows_NT"
141+
if is_windows then
142+
cmd_output = vim.fn.system(WINDOWS_CMD)
143+
else
144+
cmd_output = vim.fn.system(UNIX_CMD)
145+
end
146+
147+
-- Check if the output is a single line and contains only characters.
148+
if cmd_output:find("[^\n\r]+") and not cmd_output:find("[^%a\n\r]") then
149+
for task in cmd_output:gmatch("%S+") do
150+
if task == "" then break end
151+
table.insert(
152+
options,
153+
{ text = "Gradle " .. task, value = task, bau = "gradle" }
154+
)
155+
end
156+
end
157+
158+
return options
159+
end
160+
161+
120162
---Given a build.gradle.kts file, parse all the tasks,
121163
---and return them as a table.
122164
---
@@ -126,7 +168,6 @@ end
126168
--- { { text: "Gradle all", value="all", bau = "gradle"}, { text: "Gradle hello", value="hello", bau = "gradle"} ...}
127169
local function get_gradle_opts(path)
128170
local options = {}
129-
130171
local file = io.open(path, "r")
131172

132173
if not file then
@@ -146,7 +187,6 @@ local function get_gradle_opts(path)
146187
if task_match then
147188
in_task = true
148189
task_name = task_match
149-
150190
table.insert(
151191
options,
152192
{ text = "Gradle " .. task_name, value = task_name, bau = "gradle" }
@@ -163,7 +203,6 @@ local function get_gradle_opts(path)
163203
if task_match then
164204
in_task = true
165205
task_name = task_match
166-
167206
table.insert(
168207
options,
169208
{ text = "Gradle " .. task_name, value = task_name, bau = "gradle" }
@@ -282,6 +321,9 @@ function M.get_bau_opts()
282321
))
283322

284323
-- gradle
324+
vim.list_extend(options, get_gradle_cmd_opts(
325+
working_dir .. utils.os_path("/build.gradle.kts")
326+
))
285327
vim.list_extend(options, get_gradle_opts(
286328
working_dir .. utils.os_path("/build.gradle.kts")
287329
))

0 commit comments

Comments
 (0)