@@ -157,43 +157,56 @@ 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
165+ end
166+ end
167+ return false
168+ end
169+
160170
161171local function get_gradle_opts (path )
172+ local options = {}
173+
162174 -- OS-specific commands to get all Application tasks from 'gradle tasks --all'
163175 -- Needs gradle to be install globally and available in the PATH
164176 -- For windows, powershell needs to be installed
165- local GRADLE_COMMAND = " gradle tasks "
166- local RUN_POWERSHELL_COMMAND = " powershell -c "
167- local POWERSHELL_COMMAND =
168- [[ | 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 "" } } ]]
169- local AWK_COMMAND =
170- " | awk '/Application tasks/,/^$/{if (!/^$/) print}' | awk 'NR > 2' | awk '!/--/ && NF {gsub(/ .*/, \"\" , $0); print}' | sed '/^$/d' "
171- local UNIX_COMMAND = GRADLE_COMMAND .. AWK_COMMAND
172- local WINDOWS_COMMAND = RUN_POWERSHELL_COMMAND .. " ' " .. GRADLE_COMMAND .. POWERSHELL_COMMAND .. " ' "
173- local options = {}
174- local gradleOutput = " "
175- local tasks = {}
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 = {}
176188
177- if isWindows () then
178- gradleOutput = executeCommand (WINDOWS_COMMAND )
179- else -- Assume Unix
180- gradleOutput = executeCommand (UNIX_COMMAND )
181- end
189+ if isWindows () then
190+ gradleOutput = executeCommand (WINDOWS_COMMAND )
191+ else -- Assume Unix
192+ gradleOutput = executeCommand (UNIX_COMMAND )
193+ end
182194
183- tasks = parseTasks (gradleOutput )
195+ tasks = parseTasks (gradleOutput )
184196
185- -- If the gradle command returns something, use it as the file content
186- if tasks and # tasks > 0 then
187- -- For debugging purposes
188- -- writeTasksToFile("tasks.txt", tasks)
189- for _ , task_name in ipairs (tasks ) do
190- if task_name == " " then
191- break
197+ -- If the gradle command returns something, use it as the file content
198+ if tasks and # tasks > 0 then
199+ -- For debugging purposes
200+ -- writeTasksToFile("tasks.txt", tasks)
201+ for _ , task_name in ipairs (tasks ) do
202+ if task_name == " " then
203+ break
204+ end
205+ table.insert (
206+ options ,
207+ { text = " Gradle " .. task_name , value = task_name , bau = " gradle" }
208+ )
192209 end
193- table.insert (
194- options ,
195- { text = " Gradle " .. task_name , value = task_name , bau = " gradle" }
196- )
197210 end
198211 end
199212 local file = io.open (path , " r" )
0 commit comments