@@ -117,83 +117,57 @@ local function get_meson_opts(path)
117
117
return options
118
118
end
119
119
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` .
122
122
---
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.
124
125
--- @param path string Path to the build.gradle.kts file.
125
126
--- @return table options A table like :
126
127
--- { { 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 )
142
143
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 )
156
145
end
157
- return false
158
- end
159
146
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
+ )
185
155
end
186
156
end
187
157
188
- gradleOutput = executeCommand (gradleCommand );
189
-
190
- return parseTasks (gradleOutput )
158
+ return options
191
159
end
192
160
193
161
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"} ...}
194
169
local function get_gradle_opts (path )
195
170
local options = {}
196
-
197
171
local file = io.open (path , " r" )
198
172
199
173
if not file then
@@ -203,23 +177,6 @@ local function get_gradle_opts(path)
203
177
end
204
178
205
179
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
223
180
local in_task = false
224
181
local task_name = " "
225
182
@@ -230,13 +187,10 @@ local function get_gradle_opts(path)
230
187
if task_match then
231
188
in_task = true
232
189
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
+ )
240
194
elseif in_task then
241
195
local task_end = line :match (" }" )
242
196
if task_end then
@@ -249,13 +203,10 @@ local function get_gradle_opts(path)
249
203
if task_match then
250
204
in_task = true
251
205
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
+ )
259
210
elseif in_task then
260
211
local task_end = line :match (" }" )
261
212
if task_end then
@@ -370,6 +321,9 @@ function M.get_bau_opts()
370
321
))
371
322
372
323
-- gradle
324
+ vim .list_extend (options , get_gradle_cmd_opts (
325
+ working_dir .. utils .os_path (" /build.gradle.kts" )
326
+ ))
373
327
vim .list_extend (options , get_gradle_opts (
374
328
working_dir .. utils .os_path (" /build.gradle.kts" )
375
329
))
0 commit comments