From be67f51f9dbf4f4a04d566e9004790c8eb1b91cc Mon Sep 17 00:00:00 2001 From: Simon Templer Date: Sat, 30 Mar 2024 00:38:01 +0100 Subject: [PATCH] feat: add task to verify if version is a release version --- .releaserc.yml | 1 + .../gradle/version/VersionPlugin.groovy | 28 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/.releaserc.yml b/.releaserc.yml index 446250b..4920f5b 100644 --- a/.releaserc.yml +++ b/.releaserc.yml @@ -10,6 +10,7 @@ plugins: - - "@semantic-release/exec" - publishCmd: ./gradlew publishPlugins prepareCmd: ./gradlew "-PnewVersion=${nextRelease.version}" setReleaseVersion + verifyReleaseCmd: ./gradlew verifyReleaseVersion - - "@semantic-release/git" - assets: - CHANGELOG.md diff --git a/src/main/groovy/to/wetransform/gradle/version/VersionPlugin.groovy b/src/main/groovy/to/wetransform/gradle/version/VersionPlugin.groovy index 5265288..7436b87 100644 --- a/src/main/groovy/to/wetransform/gradle/version/VersionPlugin.groovy +++ b/src/main/groovy/to/wetransform/gradle/version/VersionPlugin.groovy @@ -4,6 +4,9 @@ import org.gradle.api.Plugin; import org.gradle.api.Project; class VersionPlugin implements Plugin { + + private static def SEM_VER_REGEX = /(\d+)\.(\d+)\.(\d+)/ + void apply(Project project) { //XXX not sure how to easiest use the service - instead the repo is opened manually // project.apply(plugin: 'org.ajoberstar.grgit.service') @@ -31,6 +34,19 @@ class VersionPlugin implements Plugin { } } + project.task('verifyReleaseVersion') { + group 'Version' + description 'Check if a release version is configured, otherwise (if the version is a -SNAPSHOT version) fail' + + doLast { + def version = project.version + + assert version + assert !version.endsWith('-SNAPSHOT') + assert version =~ SEM_VER_REGEX + } + } + // set version project.afterEvaluate { @@ -47,9 +63,15 @@ class VersionPlugin implements Plugin { def releaseVersion = null if (versionFile.exists()) { releaseVersion = versionFile.text.trim() - } - //TODO parse/verify version + // verify version + if (releaseVersion) { + def matcher = releaseVersion =~ SEM_VER_REGEX + if (!matcher) { + throw new IllegalStateException("Provided version for last release is not a valid semantic version: $releaseVersion") + } + } + } if (!releaseVersion) { // assume initial snapshot @@ -86,7 +108,7 @@ class VersionPlugin implements Plugin { } else { // build snapshot version with next minor version - def matcher = releaseVersion =~ /(\d+)\.(\d+)\.(\d+)/ + def matcher = releaseVersion =~ SEM_VER_REGEX if (matcher) { project.logger.info("Current commit is not tagged or repository is dirty, using snapshot version based on last release")