|
11 | 11 |
|
12 | 12 | import org.gradle.api.DefaultTask; |
13 | 13 | import org.gradle.api.file.ConfigurableFileCollection; |
| 14 | +import org.gradle.api.file.RegularFileProperty; |
14 | 15 | import org.gradle.api.provider.Property; |
15 | 16 | import org.gradle.api.services.ServiceReference; |
16 | 17 | import org.gradle.api.tasks.Input; |
| 18 | +import org.gradle.api.tasks.InputFile; |
17 | 19 | import org.gradle.api.tasks.InputFiles; |
18 | 20 | import org.gradle.api.tasks.Optional; |
19 | 21 | import org.gradle.api.tasks.PathSensitive; |
|
22 | 24 | import org.gradle.api.tasks.options.Option; |
23 | 25 |
|
24 | 26 | import java.io.IOException; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Path; |
25 | 30 | import java.util.ArrayList; |
26 | 31 | import java.util.Collections; |
27 | 32 | import java.util.HashSet; |
@@ -74,6 +79,14 @@ public abstract class GenerateTransportVersionDefinitionTask extends DefaultTask |
74 | 79 | @Input |
75 | 80 | public abstract Property<String> getCurrentUpperBoundName(); |
76 | 81 |
|
| 82 | + /** |
| 83 | + * An additional upper bound file that will be consulted when generating a transport version. |
| 84 | + * The larger of this and the current upper bound will be used to create the new primary id. |
| 85 | + */ |
| 86 | + @InputFile |
| 87 | + @Optional |
| 88 | + public abstract RegularFileProperty getAlternateUpperBoundFile(); |
| 89 | + |
77 | 90 | @TaskAction |
78 | 91 | public void run() throws IOException { |
79 | 92 | TransportVersionResourcesService resources = getResourceService().get(); |
@@ -119,7 +132,7 @@ private List<TransportVersionId> updateUpperBounds( |
119 | 132 | if (targetId == null) { |
120 | 133 | // Case: an id doesn't yet exist for this upper bound, so create one |
121 | 134 | int targetIncrement = upperBoundName.equals(currentUpperBoundName) ? increment : 1; |
122 | | - targetId = TransportVersionId.fromInt(existingUpperBound.definitionId().complete() + targetIncrement); |
| 135 | + targetId = createTargetId(existingUpperBound, targetIncrement); |
123 | 136 | var newUpperBound = new TransportVersionUpperBound(upperBoundName, definitionName, targetId); |
124 | 137 | resources.writeUpperBound(newUpperBound); |
125 | 138 | } |
@@ -237,4 +250,21 @@ private TransportVersionId maybeGetExistingId( |
237 | 250 | return null; // no existing id for this upper bound |
238 | 251 | } |
239 | 252 |
|
| 253 | + private TransportVersionId createTargetId(TransportVersionUpperBound existingUpperBound, int increment) throws IOException { |
| 254 | + int currentId = existingUpperBound.definitionId().complete(); |
| 255 | + |
| 256 | + // allow for an alternate upper bound file to be consulted. This supports Serverless basing its |
| 257 | + // own transport version ids on the greater of server or serverless |
| 258 | + if (getAlternateUpperBoundFile().isPresent()) { |
| 259 | + Path altUpperBoundPath = getAlternateUpperBoundFile().get().getAsFile().toPath(); |
| 260 | + String contents = Files.readString(altUpperBoundPath, StandardCharsets.UTF_8); |
| 261 | + var altUpperBound = TransportVersionUpperBound.fromString(altUpperBoundPath, contents); |
| 262 | + if (altUpperBound.definitionId().complete() > currentId) { |
| 263 | + currentId = altUpperBound.definitionId().complete(); |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + return TransportVersionId.fromInt(currentId + increment); |
| 268 | + } |
| 269 | + |
240 | 270 | } |
0 commit comments