CheckExpectedBranchVersionPlugin.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. package org.springframework.security;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import org.gradle.api.DefaultTask;
  20. import org.gradle.api.Plugin;
  21. import org.gradle.api.Project;
  22. import org.gradle.api.plugins.JavaBasePlugin;
  23. import org.gradle.api.tasks.TaskAction;
  24. import org.gradle.api.tasks.TaskProvider;
  25. /**
  26. * @author Marcus da Coregio
  27. */
  28. public class CheckExpectedBranchVersionPlugin implements Plugin<Project> {
  29. @Override
  30. public void apply(Project project) {
  31. TaskProvider<CheckExpectedBranchVersionTask> checkExpectedBranchVersionTask = project.getTasks().register("checkExpectedBranchVersion", CheckExpectedBranchVersionTask.class, (task) -> {
  32. task.setGroup("Build");
  33. task.setDescription("Check if the project version matches the branch version");
  34. });
  35. project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME, checkTask -> checkTask.dependsOn(checkExpectedBranchVersionTask));
  36. }
  37. public static class CheckExpectedBranchVersionTask extends DefaultTask {
  38. @TaskAction
  39. public void run() throws IOException {
  40. Project project = getProject();
  41. if (project.hasProperty("skipCheckExpectedBranchVersion")) {
  42. return;
  43. }
  44. String version = (String) project.getVersion();
  45. String branchVersion = getBranchVersion(project);
  46. if (!branchVersion.matches("^[0-9]+\\.[0-9]+\\.x$")) {
  47. System.out.println("Branch version does not match *.x, ignoring");
  48. return;
  49. }
  50. if (!versionsMatch(version, branchVersion)) {
  51. throw new IllegalStateException(String.format("Project version [%s] does not match branch version [%s]. " +
  52. "Please verify that the branch contains the right version.", version, branchVersion));
  53. }
  54. }
  55. private static String getBranchVersion(Project project) throws IOException {
  56. try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
  57. project.exec((exec) -> {
  58. exec.commandLine("git", "symbolic-ref", "--short", "HEAD");
  59. exec.setErrorOutput(System.err);
  60. exec.setStandardOutput(baos);
  61. });
  62. return baos.toString();
  63. }
  64. }
  65. private boolean versionsMatch(String projectVersion, String branchVersion) {
  66. String[] projectVersionParts = projectVersion.split("\\.");
  67. String[] branchVersionParts = branchVersion.split("\\.");
  68. if (projectVersionParts.length < 2 || branchVersionParts.length < 2) {
  69. return false;
  70. }
  71. return projectVersionParts[0].equals(branchVersionParts[0]) && projectVersionParts[1].equals(branchVersionParts[1]);
  72. }
  73. }
  74. }