Skip to content

Commit 4ba1572

Browse files
authored
Create extract/tag versions gradle task for 7.17 (#104532)
Create a gradle task to extract version information for the new build release tag build infrastructure. This also defines a dummy tagVersions task that is implemented on v8 but doesn't have anything to do on 7.17.
1 parent 6481e8a commit 4ba1572

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.gradle.internal.release;
10+
11+
import com.github.javaparser.StaticJavaParser;
12+
import com.github.javaparser.ast.CompilationUnit;
13+
import com.github.javaparser.ast.body.FieldDeclaration;
14+
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
15+
16+
import org.gradle.api.DefaultTask;
17+
import org.gradle.api.logging.Logger;
18+
import org.gradle.api.logging.Logging;
19+
import org.gradle.api.tasks.TaskAction;
20+
import org.gradle.api.tasks.options.Option;
21+
import org.gradle.initialization.layout.BuildLayout;
22+
23+
import java.io.IOException;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
26+
import java.nio.file.StandardOpenOption;
27+
import java.util.List;
28+
import java.util.function.Consumer;
29+
30+
import javax.inject.Inject;
31+
32+
public class ExtractCurrentVersionsTask extends DefaultTask {
33+
private static final Logger LOGGER = Logging.getLogger(ExtractCurrentVersionsTask.class);
34+
35+
static final String SERVER_MODULE_PATH = "server/src/main/java/";
36+
static final String VERSION_FILE_PATH = SERVER_MODULE_PATH + "org/elasticsearch/Version.java";
37+
38+
final Path rootDir;
39+
40+
private Path outputFile;
41+
42+
@Inject
43+
public ExtractCurrentVersionsTask(BuildLayout layout) {
44+
rootDir = layout.getRootDirectory().toPath();
45+
}
46+
47+
@Option(option = "output-file", description = "File to output tag information to")
48+
public void outputFile(String file) {
49+
this.outputFile = Path.of(file);
50+
}
51+
52+
@TaskAction
53+
public void executeTask() throws IOException {
54+
if (outputFile == null) {
55+
throw new IllegalArgumentException("Output file not specified");
56+
}
57+
58+
LOGGER.lifecycle("Extracting latest version information");
59+
60+
// get the current version from Version.java
61+
int version = readLatestVersion(rootDir.resolve(VERSION_FILE_PATH));
62+
LOGGER.lifecycle("Version: {}", version);
63+
64+
LOGGER.lifecycle("Writing version information to {}", outputFile);
65+
Files.write(
66+
outputFile,
67+
List.of("Version:" + version),
68+
StandardOpenOption.CREATE,
69+
StandardOpenOption.WRITE,
70+
StandardOpenOption.TRUNCATE_EXISTING
71+
);
72+
}
73+
74+
static class FieldIdExtractor implements Consumer<FieldDeclaration> {
75+
private Integer highestVersionId;
76+
77+
Integer highestVersionId() {
78+
return highestVersionId;
79+
}
80+
81+
@Override
82+
public void accept(FieldDeclaration fieldDeclaration) {
83+
var ints = fieldDeclaration.findAll(IntegerLiteralExpr.class);
84+
switch (ints.size()) {
85+
case 0 -> {
86+
// No ints in the field declaration, ignore
87+
}
88+
case 1 -> {
89+
int id = ints.get(0).asNumber().intValue();
90+
if (highestVersionId != null && highestVersionId > id) {
91+
LOGGER.warn("Version ids [{}, {}] out of order", highestVersionId, id);
92+
} else {
93+
highestVersionId = id;
94+
}
95+
}
96+
default -> LOGGER.warn("Multiple integers found in version field declaration [{}]", fieldDeclaration); // and ignore it
97+
}
98+
}
99+
}
100+
101+
private static int readLatestVersion(Path javaVersionsFile) throws IOException {
102+
CompilationUnit java = StaticJavaParser.parse(javaVersionsFile);
103+
104+
FieldIdExtractor extractor = new FieldIdExtractor();
105+
java.walk(FieldDeclaration.class, extractor); // walks in code file order
106+
if (extractor.highestVersionId == null) {
107+
throw new IllegalArgumentException("No version ids found in " + javaVersionsFile);
108+
}
109+
return extractor.highestVersionId;
110+
}
111+
}

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/release/ReleaseToolsPlugin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public void apply(Project project) {
4848
project.getTasks()
4949
.register("updateVersions", UpdateVersionsTask.class, t -> project.getTasks().named("spotlessApply").get().mustRunAfter(t));
5050

51+
project.getTasks().register("extractCurrentVersions", ExtractCurrentVersionsTask.class);
52+
project.getTasks().register("tagVersions", TagVersionsTask.class); // no-op, nothing to tag
53+
5154
final FileTree yamlFiles = projectDirectory.dir("docs/changelog")
5255
.getAsFileTree()
5356
.matching(new PatternSet().include("**/*.yml", "**/*.yaml"));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.gradle.internal.release;
10+
11+
import org.gradle.api.DefaultTask;
12+
import org.gradle.api.tasks.options.Option;
13+
14+
import java.util.List;
15+
16+
/**
17+
* This is a no-op task that is only implemented for v8+
18+
*/
19+
public class TagVersionsTask extends DefaultTask {
20+
21+
@Option(option = "release", description = "Dummy option")
22+
public void release(String version) {}
23+
24+
@Option(option = "tag-version", description = "Dummy option")
25+
public void tagVersions(List<String> version) {}
26+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.gradle.internal.release;
10+
11+
import com.github.javaparser.StaticJavaParser;
12+
import com.github.javaparser.ast.body.FieldDeclaration;
13+
14+
import org.junit.Test;
15+
16+
import static org.hamcrest.MatcherAssert.assertThat;
17+
import static org.hamcrest.Matchers.is;
18+
19+
public class ExtractCurrentVersionsTaskTests {
20+
21+
@Test
22+
public void testFieldExtractor() {
23+
var unit = StaticJavaParser.parse("""
24+
public class Version {
25+
public static final Version V_1 = def(1);
26+
public static final Version V_2 = def(2);
27+
public static final Version V_3 = def(3);
28+
29+
// ignore fields with no or more than one int
30+
public static final Version REF = V_3;
31+
public static final Version COMPUTED = compute(100, 200);
32+
}""");
33+
34+
ExtractCurrentVersionsTask.FieldIdExtractor extractor = new ExtractCurrentVersionsTask.FieldIdExtractor();
35+
unit.walk(FieldDeclaration.class, extractor);
36+
assertThat(extractor.highestVersionId(), is(3));
37+
}
38+
}

0 commit comments

Comments
 (0)