|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | + |
| 22 | +import org.gradle.api.DefaultTask; |
| 23 | +import org.gradle.api.Plugin; |
| 24 | +import org.gradle.api.Project; |
| 25 | +import org.gradle.api.plugins.JavaBasePlugin; |
| 26 | +import org.gradle.api.tasks.TaskAction; |
| 27 | +import org.gradle.api.tasks.TaskProvider; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Marcus da Coregio |
| 31 | + */ |
| 32 | +public class CheckExpectedBranchVersionPlugin implements Plugin<Project> { |
| 33 | + |
| 34 | + @Override |
| 35 | + public void apply(Project project) { |
| 36 | + TaskProvider<CheckExpectedBranchVersionTask> checkExpectedBranchVersionTask = project.getTasks().register("checkExpectedBranchVersion", CheckExpectedBranchVersionTask.class, (task) -> { |
| 37 | + task.setGroup("Build"); |
| 38 | + task.setDescription("Check if the project version matches the branch version"); |
| 39 | + }); |
| 40 | + project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME, checkTask -> checkTask.dependsOn(checkExpectedBranchVersionTask)); |
| 41 | + } |
| 42 | + |
| 43 | + public static class CheckExpectedBranchVersionTask extends DefaultTask { |
| 44 | + |
| 45 | + @TaskAction |
| 46 | + public void run() throws IOException { |
| 47 | + Project project = getProject(); |
| 48 | + String version = (String) project.getVersion(); |
| 49 | + String branchVersion = getBranchVersion(project); |
| 50 | + if (!branchVersion.matches("^[0-9]+\\.[0-9]+\\.x$")) { |
| 51 | + System.out.println("Branch version does not match *.x, ignoring"); |
| 52 | + return; |
| 53 | + } |
| 54 | + if (!versionsMatch(version, branchVersion)) { |
| 55 | + throw new IllegalStateException(String.format("Project version [%s] does not match branch version [%s]. " + |
| 56 | + "Please verify that the branch contains the right version.", version, branchVersion)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static String getBranchVersion(Project project) throws IOException { |
| 61 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 62 | + project.exec((exec) -> { |
| 63 | + exec.commandLine("git", "symbolic-ref", "--short", "HEAD"); |
| 64 | + exec.setErrorOutput(System.err); |
| 65 | + exec.setStandardOutput(baos); |
| 66 | + }); |
| 67 | + return baos.toString(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private boolean versionsMatch(String projectVersion, String branchVersion) { |
| 72 | + String[] projectVersionParts = projectVersion.split("\\."); |
| 73 | + String[] branchVersionParts = branchVersion.split("\\."); |
| 74 | + if (projectVersionParts.length < 2 || branchVersionParts.length < 2) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + return projectVersionParts[0].equals(branchVersionParts[0]) && projectVersionParts[1].equals(branchVersionParts[1]); |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments