@@ -117,6 +117,48 @@ local function get_meson_opts(path)
117
117
return options
118
118
end
119
119
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
+
120
162
--- Given a build.gradle.kts file, parse all the tasks,
121
163
--- and return them as a table.
122
164
---
126
168
--- { { text: "Gradle all", value="all", bau = "gradle"}, { text: "Gradle hello", value="hello", bau = "gradle"} ...}
127
169
local function get_gradle_opts (path )
128
170
local options = {}
129
-
130
171
local file = io.open (path , " r" )
131
172
132
173
if not file then
@@ -146,7 +187,6 @@ local function get_gradle_opts(path)
146
187
if task_match then
147
188
in_task = true
148
189
task_name = task_match
149
-
150
190
table.insert (
151
191
options ,
152
192
{ text = " Gradle " .. task_name , value = task_name , bau = " gradle" }
@@ -163,7 +203,6 @@ local function get_gradle_opts(path)
163
203
if task_match then
164
204
in_task = true
165
205
task_name = task_match
166
-
167
206
table.insert (
168
207
options ,
169
208
{ text = " Gradle " .. task_name , value = task_name , bau = " gradle" }
@@ -282,6 +321,9 @@ function M.get_bau_opts()
282
321
))
283
322
284
323
-- gradle
324
+ vim .list_extend (options , get_gradle_cmd_opts (
325
+ working_dir .. utils .os_path (" /build.gradle.kts" )
326
+ ))
285
327
vim .list_extend (options , get_gradle_opts (
286
328
working_dir .. utils .os_path (" /build.gradle.kts" )
287
329
))
0 commit comments