@@ -157,47 +157,57 @@ local function isTaskAlreadyAdded(options, task_name)
157157 return false
158158end
159159
160- -- Check if at least one of the files exists
161- local function check_files (files_to_check )
162- for _ , file in ipairs (files_to_check ) do
163- if vim .fn .filereadable (" ./" .. file ) then
164- return true
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
165185 end
166186 end
167- return false
187+
188+ gradleOutput = executeCommand (gradleCommand );
189+
190+ return parseTasks (gradleOutput )
168191end
169192
170193
171194local function get_gradle_opts (path )
172195 local options = {}
173196
174- -- OS-specific commands to get all Application tasks from 'gradle tasks --all'
175- -- Needs gradle to be install globally and available in the PATH
176- -- For windows, powershell needs to be installed
177- if check_files ({ " gradlew" , " build.gradle.kts" , " build.gradle" }) then
178- local GRADLE_COMMAND = " gradle tasks"
179- local RUN_POWERSHELL_COMMAND = " powershell -c"
180- local POWERSHELL_COMMAND =
181- [[ | 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 "" } }]]
182- local AWK_COMMAND =
183- " | awk '/Application tasks/,/^$/{if (!/^$/) print}' | awk 'NR > 2' | awk '!/--/ && NF {gsub(/ .*/, \"\" , $0); print}' | sed '/^$/d'"
184- local UNIX_COMMAND = GRADLE_COMMAND .. AWK_COMMAND
185- local WINDOWS_COMMAND = RUN_POWERSHELL_COMMAND .. " '" .. GRADLE_COMMAND .. POWERSHELL_COMMAND .. " '"
186- local gradleOutput = " "
187- local tasks = {}
197+ local file = io.open (path , " r" )
188198
189- if isWindows () then
190- gradleOutput = executeCommand ( WINDOWS_COMMAND )
191- else -- Assume Unix
192- gradleOutput = executeCommand ( UNIX_COMMAND )
193- end
199+ if not file then
200+ -- If the file with ".kts" extension doesn't exist, try without the extension
201+ local alternative_path = string.gsub ( path , " %.kts$ " , " " )
202+ file = io.open ( alternative_path , " r " )
203+ end
194204
195- tasks = parseTasks (gradleOutput )
205+ if file then
206+ -- First parse the gradle tasks using the command line
207+ local tasks = getGradleTasksCommandLine ()
196208
197- -- If the gradle command returns something, use it as the file content
209+ -- If the gradle command returns something, add the tasks to the options
198210 if tasks and # tasks > 0 then
199- -- For debugging purposes
200- -- writeTasksToFile("tasks.txt", tasks)
201211 for _ , task_name in ipairs (tasks ) do
202212 if task_name == " " then
203213 break
@@ -208,16 +218,8 @@ local function get_gradle_opts(path)
208218 )
209219 end
210220 end
211- end
212- local file = io.open (path , " r" )
213221
214- if not file then
215- -- If the file with ".kts" extension doesn't exist, try without the extension
216- local alternative_path = string.gsub (path , " %.kts$" , " " )
217- file = io.open (alternative_path , " r" )
218- end
219-
220- if file then
222+ -- Then parse the the tasks from file to get additional user-defined tasks if any
221223 local in_task = false
222224 local task_name = " "
223225
0 commit comments