Groovy web console

subscribe to the feed Subscribe
to this
site
Get SemVer Version (via #groovywebconsole)
tweet this snippet Tweet
this
script

Get SemVer Version

Published 1 month ago by Bob Leponge
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def json = '{ "name": "components-tools", "version": "2.4.1", "author": "Oui Teams" }'
def res = new groovy.json.JsonSlurper().parseText(json)
def version = res.find { it.key == "version" }

def getUpdatedVersion(String type, String version) {
    //println "Current version: ${version}"
    //println "Type: '${type}'"

    def split = version.split("\\.")

    if (type == "patch") {
        split[2] = ++Integer.parseInt(split[2])
    } else if (type == "minor") {
        split[1] = ++Integer.parseInt(split[1])
        split[2] = 0
    } else if (type == "major") {
        split[0] = ++Integer.parseInt(split[0])
        split[1] = 0
        split[2] = 0
    } else {
        println "This version type (${type}) doesn't exist. It must be either one of 'patch', 'minor' or 'major'."
    }

    //println "Updated version: ${split.join('.')}"
    return split.join('.')
}

println "'patch' => ${getUpdatedVersion('patch', version.getValue())}"
println "'minor' => ${getUpdatedVersion('minor', version.getValue())}"
println "'major' => ${getUpdatedVersion('major', version.getValue())}"