Skip to content

Commit

Permalink
Use version catalog to drive Java platform
Browse files Browse the repository at this point in the history
Update the updateRelease/Snapshot plugin to use new property in the
libs.versions.toml file

See spring-projects#523
  • Loading branch information
onobc committed Dec 27, 2023
1 parent 3141f78 commit f103f9e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,57 @@
package org.springframework.pulsar.gradle.versions;

import java.io.File;
import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.artifacts.VersionCatalogsExtension;

import org.springframework.lang.Nullable;

public abstract class UpdateProjectVersionTask extends DefaultTask {

static final String VERSION_PROPERTY = "version";

static final String SPRING_BOOT_VERSION_PROPERTY = "springBootVersionForDocs";
static final String SPRING_BOOT_VERSION_PROPERTY = "spring-boot-for-docs";

static final Pattern VERSION_PATTERN = Pattern.compile("^([0-9]+)\\.([0-9]+)\\.([0-9]+)(-M\\d+|-RC\\d+|-SNAPSHOT)?$");

protected void updateVersionInGradleProperties(String newVersion) {
this.updatePropertyInGradleProperties(VERSION_PROPERTY, (p) -> p.getVersion().toString(), newVersion);
this.updatePropertyInFile(Project.GRADLE_PROPERTIES, VERSION_PROPERTY,
(p) -> p.getVersion().toString(),
(__) -> newVersion,
(currentValue) -> "%s=%s".formatted(VERSION_PROPERTY, currentValue),
(__) -> "%s=%s".formatted(VERSION_PROPERTY, newVersion));
}

protected void updatePropertyInGradleProperties(String propertyName, String newPropertyValue) {
this.updatePropertyInGradleProperties(propertyName,
(p) -> Objects.toString(p.findProperty(propertyName), ""), newPropertyValue);
protected void updateVersionInTomlVersions(String versionPropertyName,
Function<String, String> newPropertyValueGivenCurrentValue) {
this.updatePropertyInFile("gradle/libs.versions.toml", versionPropertyName,
(p) -> currentVersionForProperty(p, versionPropertyName),
newPropertyValueGivenCurrentValue,
(currentValue) -> "%s = \"%s\"".formatted(versionPropertyName, currentValue),
(newValue) -> "%s = \"%s\"".formatted(versionPropertyName, newValue));
}

protected void updatePropertyInGradleProperties(
String propertyName,
Function<Project, String> currentPropertyValueFromProject,
String newPropertyValue) {
String currentPropertyValue = currentPropertyValueFromProject.apply(getProject());
File gradlePropertiesFile = getProject().getRootProject().file(Project.GRADLE_PROPERTIES);
if (!gradlePropertiesFile.exists()) {
throw new RuntimeException("No gradle.properties to update property in");
protected void updatePropertyInFile(String propertyFile, String propertyName,
Function<Project, String> currentPropertyValueGivenProject,
Function<String, String> newPropertyValueGivenCurrentValue,
Function<String, String> expectedCurrentPropertyEntryInFile,
Function<String, String> newPropertyEntryInFile) {
File file = getProject().getRootProject().file(propertyFile);
if (!file.exists()) {
throw new RuntimeException("File not found at " + propertyFile);
}
System.out.printf("Updating the %s property in %s from %s to %s%n",
propertyName, Project.GRADLE_PROPERTIES, currentPropertyValue, newPropertyValue);
FileUtils.replaceFileText(gradlePropertiesFile, (gradlePropertiesText) -> {
gradlePropertiesText = gradlePropertiesText.replace(
"%s=%s".formatted(propertyName, currentPropertyValue),
"%s=%s".formatted(propertyName, newPropertyValue));
return gradlePropertiesText;
});
String currentValue = currentPropertyValueGivenProject.apply(getProject());
String newValue = newPropertyValueGivenCurrentValue.apply(currentValue);
System.out.printf("Updating the %s property in %s from %s to %s%n", propertyName,
propertyFile, currentValue, newValue);
FileUtils.replaceFileText(file, (propertiesText) -> propertiesText.replace(
expectedCurrentPropertyEntryInFile.apply(currentValue),
newPropertyEntryInFile.apply(newValue)));
}

protected VersionInfo parseVersion(String version) {
Expand All @@ -78,6 +85,13 @@ protected VersionInfo parseVersion(String version) {
}
}

protected String currentVersionForProperty(Project project, String versionProperty) {
VersionCatalogsExtension catalog = project.getExtensions().getByType(VersionCatalogsExtension.class);
return catalog.named("libs").findVersion(versionProperty)
.orElseThrow(() -> new IllegalStateException("% property not found".formatted(versionProperty)))
.getDisplayName();
}

record VersionInfo(String major, String minor, String patch, @Nullable String modifier) {
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,13 +16,9 @@

package org.springframework.pulsar.gradle.versions;

import java.util.Objects;

import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;

import org.springframework.util.Assert;

public abstract class UpdateToReleaseVersionTask extends UpdateProjectVersionTask {

@Input
Expand All @@ -31,7 +27,7 @@ public abstract class UpdateToReleaseVersionTask extends UpdateProjectVersionTas
@TaskAction
public void updateToReleaseVersion() {
updateVersionInGradleProperties(this.releaseVersion);
updatePropertyInGradleProperties(SPRING_BOOT_VERSION_PROPERTY, calculateReleaseBootVersion());
updateVersionInTomlVersions(SPRING_BOOT_VERSION_PROPERTY, this::calculateReleaseBootVersion);
}

public String getReleaseVersion() {
Expand All @@ -42,9 +38,7 @@ public void setReleaseVersion(String releaseVersion) {
this.releaseVersion = releaseVersion;
}

private String calculateReleaseBootVersion() {
String currentBootVersion = Objects.toString(getProject().findProperty(SPRING_BOOT_VERSION_PROPERTY), null);
Assert.notNull(currentBootVersion, () -> "% property not found".formatted(SPRING_BOOT_VERSION_PROPERTY));
private String calculateReleaseBootVersion(String currentBootVersion) {
VersionInfo bootVersionSegments = parseVersion(currentBootVersion);
String releaseBootVersion = "%s.%s.%s".formatted(bootVersionSegments.major(), bootVersionSegments.minor(), bootVersionSegments.patch());
String releaseVersionModifier = parseVersion(this.releaseVersion).modifier();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,21 +16,15 @@

package org.springframework.pulsar.gradle.versions;

import java.util.Objects;

import org.gradle.api.tasks.TaskAction;

import org.springframework.util.Assert;

public abstract class UpdateToSnapshotVersionTask extends UpdateProjectVersionTask {

@TaskAction
public void updateToSnapshotVersion() {
String currentVersion = getProject().getVersion().toString();
updateVersionInGradleProperties(calculateNextSnapshotVersion(currentVersion));
String currentBootVersion = Objects.toString(getProject().findProperty(SPRING_BOOT_VERSION_PROPERTY), null);
Assert.notNull(currentBootVersion, () -> "% property not found".formatted(SPRING_BOOT_VERSION_PROPERTY));
updatePropertyInGradleProperties(SPRING_BOOT_VERSION_PROPERTY, calculateNextSnapshotVersion(currentBootVersion));
updateVersionInTomlVersions(SPRING_BOOT_VERSION_PROPERTY, this::calculateNextSnapshotVersion);
}

private String calculateNextSnapshotVersion(String version) {
Expand All @@ -45,12 +39,4 @@ private String calculateNextSnapshotVersion(String version) {
}
return "%s.%s.%s-SNAPSHOT".formatted(majorSegment, minorSegment, patchSegment);
}

private String calculateCurrentSnapshotVersion(String version) {
VersionInfo versionSegments = parseVersion(version);
String majorSegment = versionSegments.major();
String minorSegment = versionSegments.minor();
String patchSegment = versionSegments.patch();
return "%s.%s.%s-SNAPSHOT".formatted(majorSegment, minorSegment, patchSegment);
}
}

0 comments on commit f103f9e

Please sign in to comment.