Skip to content

Commit 139f529

Browse files
authored
refactor(#40): The PR that add gradle cmd support.
1 parent 886561d commit 139f529

File tree

1 file changed

+47
-93
lines changed

1 file changed

+47
-93
lines changed

lua/compiler/utils-bau.lua

+47-93
Original file line numberDiff line numberDiff line change
@@ -117,83 +117,57 @@ local function get_meson_opts(path)
117117
return options
118118
end
119119

120-
---Given a build.gradle.kts file, parse all the tasks,
121-
---and return them as a table.
120+
---If a gradle.build.kts or gradle.build file exists,
121+
---parse the result of the command `gradle tasks`.
122122
---
123-
--- If the file is not found. It will fallback to build.gradle.
123+
---For windows, gradle needs gradle to be install globally
124+
---and available in the PATH.
124125
---@param path string Path to the build.gradle.kts file.
125126
---@return table options A table like:
126127
--- { { text: "Gradle all", value="all", bau = "gradle"}, { text: "Gradle hello", value="hello", bau = "gradle"} ...}
127-
128-
local function executeCommand(command)
129-
local output = vim.fn.system(command)
130-
return output
131-
end
132-
133-
-- Function to parse tasks from the output
134-
local function parseTasks(output)
135-
-- Check if output is a single line and contains only characters
136-
if output:find("[^\n\r]+") and not output:find("[^%a\n\r]") then
137-
local tasks = {}
138-
for task in output:gmatch("%S+") do
139-
table.insert(tasks, task)
140-
end
141-
return tasks
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)
142143
else
143-
return {}
144-
end
145-
end
146-
147-
local function isWindows()
148-
return os.getenv("OS") == "Windows_NT"
149-
end
150-
151-
local function isTaskAlreadyAdded(options, task_name)
152-
for _, option in ipairs(options) do
153-
if option.value == task_name then
154-
return true
155-
end
144+
cmd_output = vim.fn.system(UNIX_CMD)
156145
end
157-
return false
158-
end
159146

160-
local function getGradleTasksCommandLine()
161-
local GRADLE_GLOBAL_COMMAND = "gradle tasks"
162-
local GRADLEW_UNIX_COMMAND = "./gradlew tasks"
163-
local GRADLEW_WINDOWS_COMMAND = "gradlew.bat tasks"
164-
local RUN_POWERSHELL_COMMAND = "powershell -c"
165-
166-
local POWERSHELL_COMMAND =
167-
[[ | 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 "" } }]]
168-
local AWK_COMMAND =
169-
" 2>&1 | awk '/Application tasks/,/^$/{if (!/^$/) print}' | awk 'NR > 2' | awk '!/--/ && NF {gsub(/ .*/, \"\", $0); print}' | sed '/^$/d'"
170-
171-
local gradleOutput = ""
172-
local gradleCommand = ""
173-
174-
if isWindows() then
175-
if vim.fn.filereadable("./gradlew.bat") then
176-
gradleCommand = RUN_POWERSHELL_COMMAND .. " '" .. GRADLEW_WINDOWS_COMMAND .. POWERSHELL_COMMAND .. "'"
177-
else -- Fallback to global gradle
178-
gradleCommand = RUN_POWERSHELL_COMMAND .. " '" .. GRADLE_GLOBAL_COMMAND .. POWERSHELL_COMMAND .. "'"
179-
end
180-
else -- Assume Unix
181-
if vim.fn.filereadable("./gradlew") then
182-
gradleCommand = GRADLEW_UNIX_COMMAND .. AWK_COMMAND
183-
else -- Fallback to global gradle
184-
gradleCommand = GRADLE_GLOBAL_COMMAND .. AWK_COMMAND
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+
)
185155
end
186156
end
187157

188-
gradleOutput = executeCommand(gradleCommand);
189-
190-
return parseTasks(gradleOutput)
158+
return options
191159
end
192160

193161

162+
---Given a build.gradle.kts file, parse all the tasks,
163+
---and return them as a table.
164+
---
165+
--- If the file is not found. It will fallback to build.gradle.
166+
---@param path string Path to the build.gradle.kts file.
167+
---@return table options A table like:
168+
--- { { text: "Gradle all", value="all", bau = "gradle"}, { text: "Gradle hello", value="hello", bau = "gradle"} ...}
194169
local function get_gradle_opts(path)
195170
local options = {}
196-
197171
local file = io.open(path, "r")
198172

199173
if not file then
@@ -203,23 +177,6 @@ local function get_gradle_opts(path)
203177
end
204178

205179
if file then
206-
-- First parse the gradle tasks using the command line
207-
local tasks = getGradleTasksCommandLine()
208-
209-
-- If the gradle command returns something, add the tasks to the options
210-
if tasks and #tasks > 0 then
211-
for _, task_name in ipairs(tasks) do
212-
if task_name == "" then
213-
break
214-
end
215-
table.insert(
216-
options,
217-
{ text = "Gradle " .. task_name, value = task_name, bau = "gradle" }
218-
)
219-
end
220-
end
221-
222-
-- Then parse the the tasks from file to get additional user-defined tasks if any
223180
local in_task = false
224181
local task_name = ""
225182

@@ -230,13 +187,10 @@ local function get_gradle_opts(path)
230187
if task_match then
231188
in_task = true
232189
task_name = task_match
233-
234-
if not isTaskAlreadyAdded(options, task_name) then
235-
table.insert(
236-
options,
237-
{ text = "Gradle " .. task_name, value = task_name, bau = "gradle" }
238-
)
239-
end
190+
table.insert(
191+
options,
192+
{ text = "Gradle " .. task_name, value = task_name, bau = "gradle" }
193+
)
240194
elseif in_task then
241195
local task_end = line:match("}")
242196
if task_end then
@@ -249,13 +203,10 @@ local function get_gradle_opts(path)
249203
if task_match then
250204
in_task = true
251205
task_name = task_match
252-
253-
if not isTaskAlreadyAdded(options, task_name) then
254-
table.insert(
255-
options,
256-
{ text = "Gradle " .. task_name, value = task_name, bau = "gradle" }
257-
)
258-
end
206+
table.insert(
207+
options,
208+
{ text = "Gradle " .. task_name, value = task_name, bau = "gradle" }
209+
)
259210
elseif in_task then
260211
local task_end = line:match("}")
261212
if task_end then
@@ -370,6 +321,9 @@ function M.get_bau_opts()
370321
))
371322

372323
-- gradle
324+
vim.list_extend(options, get_gradle_cmd_opts(
325+
working_dir .. utils.os_path("/build.gradle.kts")
326+
))
373327
vim.list_extend(options, get_gradle_opts(
374328
working_dir .. utils.os_path("/build.gradle.kts")
375329
))

0 commit comments

Comments
 (0)