Skip to content

Commit

Permalink
Failsafe version of checkVersion Task
Browse files Browse the repository at this point in the history
Prevent stopping  check Task if an exception happens during checkVersion execution.
Print proper warning message if a version violates semantic versioning.

Closes asciidoctor#479
  • Loading branch information
ancho committed Jul 9, 2016
1 parent 5541c9a commit 68e988b
Showing 1 changed file with 58 additions and 18 deletions.
76 changes: 58 additions & 18 deletions gradle/versioncheck.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,75 @@ task checkVersion() {
throw new GradleException("I need the gem name to check the version. Add a 'gem_name' property to the gradle.properties file. (e.g.: gem_name=asciidoctor-diagram)")
}

def gem_version_pattern = ~/(((\d+\.){1,2}\d+)(\.)?(.*))/
def gem_latest_version_url = "https://rubygems.org/api/v1/versions/${gem_name}/latest.json".toURL()
doLast {

def json = new JsonSlurper().parse( gem_latest_version_url )
def gem_latest_version = json.version
String gem_latest_version = getLatestGemVersion(gem_name)
String javaVersion = latestGemVersionAsJavaVersion(gem_latest_version)

def matcher = gem_latest_version =~ gem_version_pattern
def javaVersion = gem_latest_version
try {

doLast {
Version projectVersion = Version.valueOf(version as String)
Version gemJavaVersion = Version.valueOf(javaVersion)

if ( gem_latest_version ==~ gem_version_pattern ) {
matcher.find()
if (matcher.group(4)) {
javaVersion = "${matcher.group(2)}-${matcher.group(5)}"
if (projectVersion.lessThan(gemJavaVersion)) {
logger.warn "\nWARNING\nVersion mismatch: Current Version: {}, Gem Version: {}\n",version , gem_latest_version
} else {
println "up-to-date"
}
}
catch (Exception e) {
logger.error "\nWARNING\nSemantic version violation: Current Version: {}, Gem Version: {}\n",version , gem_latest_version
}
}
}

/**
* Consume rubygems webservice to determine latest gem version
*
* @param gemName The gem's package name. eg.: asciidoctor-diagram
* @return latest gem version as String
*/
private String getLatestGemVersion(String gemName) {
def gemVersion

try {
def gem_latest_version_url = "https://rubygems.org/api/v1/versions/${gemName}/latest.json".toURL()
def json = new JsonSlurper().parse(gem_latest_version_url)
gemVersion = json.version
}
catch (Exception e) {
logger.error "Problems getting latest gem version. {}", e.message
throw new StopExecutionException()
}
gemVersion
}

Version projectVersion = Version.valueOf(version)
Version gemJavaVersion = Version.valueOf(javaVersion)
/**
* Transforms a gem version into a java version representation.
*
* e.g.: The gem version of asciidoctorj-epub3 1.5.0.alpha.6 transforms to 1.5.0-alpha.6
*
* @param gem_latest_version The latest gem version as String
* @return a java compatible presentation of a gem version
*/
private String latestGemVersionAsJavaVersion(String gem_latest_version) {
def gem_version_pattern = ~/(((\d+\.){1,2}\d+)(\.)?(.*))/
def matcher = gem_latest_version =~ gem_version_pattern
def javaVersion = gem_latest_version

if ( projectVersion.lessThan(gemJavaVersion) ) {
logger.warn "\nWARNING\nVersion mismatch: Current Version: $version, Gem Version: $gem_latest_version\n"
}
else {
println "up-to-date"
try {
if (gem_latest_version ==~ gem_version_pattern) {
matcher.find()
if (matcher.group(4)) {
javaVersion = "${matcher.group(2)}-${matcher.group(5)}"
}
}
}
catch (Exception e) {
logger.error "Problems transforming gem version {} to java version", gem_latest_version
throw new StopExecutionException()
}
javaVersion
}

checkVersion.onlyIf { !gradle.startParameter.isOffline() }
Expand Down

0 comments on commit 68e988b

Please sign in to comment.