|
| 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 | +} |
0 commit comments