|
1 | 1 | package com.avast.gradle.dockercompose |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.core.type.TypeReference |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper |
3 | 5 | import groovy.transform.CompileStatic |
4 | 6 | import groovy.transform.PackageScope |
5 | 7 | import org.gradle.api.Project |
6 | 8 | import org.gradle.api.Task |
7 | 9 | import org.gradle.api.file.DirectoryProperty |
| 10 | +import org.gradle.api.file.RegularFile |
8 | 11 | import org.gradle.api.file.RegularFileProperty |
9 | 12 | import org.gradle.api.provider.ListProperty |
10 | 13 | import org.gradle.api.provider.MapProperty |
@@ -89,6 +92,8 @@ abstract class ComposeSettings { |
89 | 92 | abstract DirectoryProperty getDockerComposeWorkingDirectory() |
90 | 93 | abstract Property<Duration> getDockerComposeStopTimeout() |
91 | 94 |
|
| 95 | + private final Provider<RegularFile> servicesInfosFile |
| 96 | + |
92 | 97 | @Inject |
93 | 98 | ComposeSettings(Project project, String name = '', String parentName = '') { |
94 | 99 | this.nestedName = parentName + name |
@@ -157,6 +162,7 @@ abstract class ComposeSettings { |
157 | 162 |
|
158 | 163 | this.dockerExecutor = project.objects.newInstance(DockerExecutor, this) |
159 | 164 | this.tasksConfigurator = new TasksConfigurator(this, project, name) |
| 165 | + servicesInfosFile = tasksConfigurator.upTask.flatMap { it.servicesInfosFile } |
160 | 166 | } |
161 | 167 |
|
162 | 168 | private static String generateSafeProjectNamePrefix(Project project) { |
@@ -223,7 +229,11 @@ abstract class ComposeSettings { |
223 | 229 | } |
224 | 230 |
|
225 | 231 | Map<String, ServiceInfo> getServicesInfos() { |
226 | | - tasksConfigurator.getServicesInfos() |
| 232 | + // Preserve the legacy behavior of returning an empty map if this is called before composeUp succeeds. |
| 233 | + // composeUp.servicesInfosFile.map { ... }.get() will fail if called before composeUp completes. |
| 234 | + // composeUp.servicesInfosFile.get() will work if called before composeUp completes. |
| 235 | + def f = servicesInfosFile.get().asFile |
| 236 | + f.exists() ? new ObjectMapper().readValue(f, new TypeReference<Map<String, ServiceInfo>>() {}) : [:] |
227 | 237 | } |
228 | 238 |
|
229 | 239 | void exposeAsEnvironment(ProcessForkOptions task) { |
|
0 commit comments