Skip to content

Commit 64ff6c2

Browse files
authored
Merge pull request #53 from avast/ImprovedDockerComposeDetection
Improved Docker Compose files detection
2 parents 1c2abb3 + 5a5a5c4 commit 64ff6c2

File tree

7 files changed

+70
-28
lines changed

7 files changed

+70
-28
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ repositories {
2828

2929
dependencies {
3030
compile gradleApi()
31-
compile 'org.yaml:snakeyaml:1.16'
31+
compile 'org.yaml:snakeyaml:1.17'
3232
testCompile gradleTestKit()
3333
testCompile ('org.spockframework:spock-core:1.0-groovy-2.4') {
3434
// Conflicting module versions: 2.4.4 vs. 2.4.1
@@ -37,7 +37,7 @@ dependencies {
3737
}
3838

3939
task wrapper(type: Wrapper) {
40-
gradleVersion = '3.1'
40+
gradleVersion = '3.2.1'
4141
}
4242

4343
task sourcesJar(type: Jar) {

gradle/wrapper/gradle-wrapper.jar

1.27 KB
Binary file not shown.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Sep 27 20:55:02 CEST 2016
1+
#Mon Jan 02 20:24:04 CET 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-bin.zip

gradlew

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env bash
1+
#!/usr/bin/env sh
22

33
##############################################################################
44
##
@@ -154,16 +154,19 @@ if $cygwin ; then
154154
esac
155155
fi
156156

157-
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158-
function splitJvmOpts() {
159-
JVM_OPTS=("$@")
157+
# Escape application args
158+
save ( ) {
159+
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160+
echo " "
160161
}
161-
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162-
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
162+
APP_ARGS=$(save "$@")
163+
164+
# Collect all arguments for the java command, following the shell quoting and substitution rules
165+
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
163166

164167
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
165-
if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then
168+
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
166169
cd "$(dirname "$0")"
167170
fi
168171

169-
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
172+
exec "$JAVACMD" "$@"

src/main/groovy/com/avast/gradle/dockercompose/ComposeExtension.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import com.avast.gradle.dockercompose.tasks.ComposeUp
55
import groovy.transform.PackageScope
66
import org.gradle.api.Project
77
import org.gradle.api.Task
8+
import org.gradle.process.ExecSpec
89
import org.gradle.process.JavaForkOptions
910
import org.gradle.process.ProcessForkOptions
11+
import org.gradle.util.VersionNumber
1012

1113
import java.time.Duration
1214

@@ -81,6 +83,18 @@ class ComposeExtension {
8183
res.addAll(args)
8284
res
8385
}
86+
87+
VersionNumber getDockerComposeVersion() {
88+
def p = project
89+
new ByteArrayOutputStream().withStream { os ->
90+
p.exec { ExecSpec e ->
91+
e.environment = environment
92+
e.commandLine composeCommand('--version')
93+
e.standardOutput = os
94+
}
95+
VersionNumber.parse(os.toString().trim().findAll(/(\d+\.){2}(\d+)/).head())
96+
}
97+
}
8498
}
8599

86100
enum RemoveImages {

src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeDown.groovy

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ComposeDown extends DefaultTask {
2323
e.commandLine extension.composeCommand('stop')
2424
}
2525
if (extension.removeContainers) {
26-
if (getDockerComposeVersion() >= VersionNumber.parse('1.6.0')) {
26+
if (extension.getDockerComposeVersion() >= VersionNumber.parse('1.6.0')) {
2727
String[] args = ['down']
2828
switch (extension.removeImages) {
2929
case RemoveImages.All:
@@ -49,15 +49,4 @@ class ComposeDown extends DefaultTask {
4949
}
5050
}
5151
}
52-
53-
VersionNumber getDockerComposeVersion() {
54-
new ByteArrayOutputStream().withStream { os ->
55-
project.exec { ExecSpec e ->
56-
e.environment = extension.environment
57-
e.commandLine extension.composeCommand('--version')
58-
e.standardOutput = os
59-
}
60-
VersionNumber.parse(os.toString().trim().findAll(/(\d+\.){2}(\d+)/).head())
61-
}
62-
}
6352
}

src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.avast.gradle.dockercompose.ServiceInfo
77
import org.gradle.api.DefaultTask
88
import org.gradle.api.tasks.TaskAction
99
import org.gradle.process.ExecSpec
10+
import org.gradle.util.VersionNumber
1011
import org.yaml.snakeyaml.Yaml
1112

1213
import java.time.Instant
@@ -103,14 +104,49 @@ class ComposeUp extends DefaultTask {
103104
}
104105

105106
Iterable<String> getServiceNames() {
106-
String[] composeFiles = extension.useComposeFiles.empty ? ['docker-compose.yml', 'docker-compose.override.yml'] : extension.useComposeFiles
107-
composeFiles
108-
.findAll { project.file(it).exists() }
109-
.collectMany { composeFile ->
107+
if (extension.getDockerComposeVersion() >= VersionNumber.parse('1.6.0')) {
108+
new ByteArrayOutputStream().withStream { os ->
109+
project.exec { ExecSpec e ->
110+
e.environment = extension.environment
111+
e.commandLine extension.composeCommand('config', '--services')
112+
e.standardOutput = os
113+
}
114+
os.toString().readLines()
115+
}
116+
} else {
117+
def composeFiles = extension.useComposeFiles.empty ? getStandardComposeFiles() : getCustomComposeFiles()
118+
composeFiles.collectMany { composeFile ->
110119
def compose = (Map<String, Object>) (new Yaml().load(project.file(composeFile).text))
111120
// if there is 'version: 2' on top-level then information about services is in 'services' sub-tree
112121
'2'.equals(compose.get('version')) ? ((Map) compose.get('services')).keySet() : compose.keySet()
113122
}.unique()
123+
124+
}
125+
}
126+
127+
Iterable<File> getStandardComposeFiles() {
128+
def res = []
129+
def f = findInParentDirectories('docker-compose.yml', project.projectDir)
130+
if (f != null) res.add(f)
131+
f = findInParentDirectories('docker-compose.override.yml', project.projectDir)
132+
if (f != null) res.add(f)
133+
res
134+
}
135+
136+
Iterable<File> getCustomComposeFiles() {
137+
extension.useComposeFiles.collect {
138+
def f = project.file(it)
139+
if (!f.exists()) {
140+
throw new IllegalArgumentException("Custom Docker Compose file not found: $f")
141+
}
142+
f
143+
}
144+
}
145+
146+
File findInParentDirectories(String filename, File directory) {
147+
if ((directory) == null) return null
148+
def f = new File(directory, filename)
149+
f.exists() ? f : findInParentDirectories(filename, directory.parentFile)
114150
}
115151

116152
String getContainerId(String serviceName) {

0 commit comments

Comments
 (0)