@@ -237,18 +237,49 @@ class BuildPlugin implements Plugin<Project> {
237
237
if (rootProject. hasProperty(' requiresDocker' ) == false ) {
238
238
rootProject. rootProject. ext. requiresDocker = []
239
239
rootProject. gradle. taskGraph. whenReady { TaskExecutionGraph taskGraph ->
240
- if (new File (' /usr/bin/docker' ). exists() || new File (' /usr/local/bin/docker' ). exists()) {
241
- return
240
+ final String dockerBinary
241
+ if (new File (' /usr/bin/docker' ). exists()) {
242
+ dockerBinary = ' /usr/bin/docker'
243
+ } else if (new File (' /usr/local/bin/docker' ). exists()) {
244
+ dockerBinary = ' /usr/local/bin/docker'
245
+ }
246
+ final int exitCode
247
+ final String dockerErrorOutput
248
+ if (dockerBinary == null ) {
249
+ exitCode = -1
250
+ dockerErrorOutput = null
251
+ } else {
252
+ final Process process = new ProcessBuilder (dockerBinary, " images" ). start()
253
+ exitCode = process. waitFor()
254
+ if (exitCode == 0 ) {
255
+ return
256
+ }
257
+ final Scanner scanner = new Scanner (process. errorStream). useDelimiter(" \\ A" )
258
+ dockerErrorOutput = scanner. hasNext() ? scanner. next() : " "
259
+ process. closeStreams()
242
260
}
243
261
final List<String > tasks =
244
262
((List<Task > )rootProject. requiresDocker). findAll { taskGraph. hasTask(it) }. collect { " ${ it.path} " . toString()}
245
263
if (tasks. isEmpty() == false ) {
246
- throw new GradleException (
247
- String . format(
248
- Locale . ROOT ,
249
- " Docker is rqeuired to run the following task%s: \n %s" ,
250
- tasks. size() > 1 ? " s" : " " ,
251
- tasks. join(' \n ' )))
264
+ if (dockerBinary == null ) {
265
+ throw new GradleException (
266
+ String . format(
267
+ Locale . ROOT ,
268
+ " Docker is required to run the following task%s: \n %s" ,
269
+ tasks. size() > 1 ? " s" : " " ,
270
+ tasks. join(' \n ' )))
271
+ } else {
272
+ assert exitCode > 0 && dockerErrorOutput != null
273
+ throw new GradleException (
274
+ String . format(
275
+ Locale . ROOT ,
276
+ " a problem occurred running Docker yet it is required to run the following task%s: \n %s\n " +
277
+ " the problem is that Docker exited with exit code [%d] with error output [%s]" ,
278
+ tasks. size() > 1 ? " s" : " " ,
279
+ tasks. join(' \n ' ),
280
+ exitCode,
281
+ dockerErrorOutput))
282
+ }
252
283
}
253
284
}
254
285
}
0 commit comments