forked from spring-projects/spring-security
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gradle task for updating to next development version
Issue spring-projectsgh-10461
- Loading branch information
1 parent
d7ac254
commit 79f9080
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...in/java/org/springframework/security/convention/versions/UpdateToSnapshotVersionTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2019-2022 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.convention.versions; | ||
|
||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.Optional; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public abstract class UpdateToSnapshotVersionTask extends DefaultTask { | ||
|
||
private static final String RELEASE_VERSION_PATTERN = "^([0-9]+)\\.([0-9]+)\\.([0-9]+)(-M\\d+|-RC\\d+)?$"; | ||
|
||
@Input | ||
@Optional | ||
private Boolean commit; | ||
|
||
@TaskAction | ||
public void updateToSnapshotVersion() { | ||
String currentVersion = getProject().getVersion().toString(); | ||
File gradlePropertiesFile = getProject().getRootProject().file(Project.GRADLE_PROPERTIES); | ||
if (!gradlePropertiesFile.exists()) { | ||
return; | ||
} | ||
String nextVersion = calculateNextSnapshotVersion(currentVersion); | ||
System.out.println("Updating the project version in " + Project.GRADLE_PROPERTIES + " from " + currentVersion | ||
+ " to " + nextVersion); | ||
FileUtils.replaceFileText(gradlePropertiesFile, (gradlePropertiesText) -> { | ||
gradlePropertiesText = gradlePropertiesText.replace("version=" + currentVersion, "version=" + nextVersion); | ||
return gradlePropertiesText; | ||
}); | ||
if (this.commit) { | ||
System.out.println("Committing the version update"); | ||
File rootDir = getProject().getRootDir(); | ||
String commitMessage = "Next development version"; | ||
CommandLineUtils.runCommand(rootDir, "git", "commit", "-am", commitMessage); | ||
} | ||
} | ||
|
||
private String calculateNextSnapshotVersion(String currentVersion) { | ||
Pattern releaseVersionPattern = Pattern.compile(RELEASE_VERSION_PATTERN); | ||
Matcher releaseVersion = releaseVersionPattern.matcher(currentVersion); | ||
|
||
if (releaseVersion.find()) { | ||
String majorSegment = releaseVersion.group(1); | ||
String minorSegment = releaseVersion.group(2); | ||
String patchSegment = releaseVersion.group(3); | ||
String modifier = releaseVersion.group(4); | ||
if (modifier == null) { | ||
patchSegment = String.valueOf(Integer.parseInt(patchSegment) + 1); | ||
} | ||
System.out.println("modifier = " + modifier); | ||
return String.format("%s.%s.%s-SNAPSHOT", majorSegment, minorSegment, patchSegment); | ||
} | ||
else { | ||
throw new IllegalStateException( | ||
"Cannot calculate next snapshot version because the current project version does not conform to the expected format"); | ||
} | ||
} | ||
|
||
public Boolean getCommit() { | ||
return commit; | ||
} | ||
|
||
public void setCommit(Boolean commit) { | ||
this.commit = commit; | ||
} | ||
|
||
} |