Commit 5620069f authored by Sander Bollen's avatar Sander Bollen
Browse files

semantic version convenience methods

parent 2727f142
......@@ -64,4 +64,86 @@ package object utils {
case _ if fallBack => Try(raw)
case _ => Try(throw new Exception(s"Can not extract number from string $raw"))
}
val semanticVersionRegex = "(\\d+)\\.(\\d+)\\.(\\d+)(-.*)?".r
/**
* Check whether a version string is a semantic version.
*
* @param version version string
* @return boolean
*/
def isSemanticVersion(version: String): Boolean = {
version.matches(semanticVersionRegex.toString)
}
/**
* Get major number from a semantic version
*
* @param version version string
* @return
*/
def majorVersion(version: String): Option[Int] = {
if (!isSemanticVersion(version)) {
None
} else {
semanticVersionRegex.findFirstMatchIn(version) match {
case Some(matcher) => tryToParseNumber(matcher.group(1)) match {
case Success(s) => s match {
case i: Int => Some(i)
case _ => None
}
case _ => None
}
case _ => None
}
}
}
/**
* Get minor number from a semantic version
*
* @param version version string
* @return
*/
def minorVersion(version: String): Option[Int] = {
if (!isSemanticVersion(version)) {
None
} else {
semanticVersionRegex.findFirstMatchIn(version) match {
case Some(matcher) => tryToParseNumber(matcher.group(2)) match {
case Success(s) => s match {
case i: Int => Some(i)
case _ => None
}
case _ => None
}
case _ => None
}
}
}
/**
* Get patch number from a semantic version
*
* @param version version string
* @return
*/
def patchVersion(version: String): Option[Int] = {
if (!isSemanticVersion(version)) {
None
} else {
semanticVersionRegex.findFirstMatchIn(version) match {
case Some(matcher) => tryToParseNumber(matcher.group(3)) match {
case Success(s) => s match {
case i: Int => Some(i)
case _ => None
}
case _ => None
}
case _ => None
}
}
}
}
package nl.lumc.sasc.biopet.utils
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
/**
* Created by Sander Bollen on 12-10-16.
*/
class UtilsTest extends TestNGSuite with Matchers {
val semanticVersion = "1.2.3"
val semanticVersionWithBuild = "1.2.3-alpha0.1"
val nonSemanticVersion = "v1222.1"
@Test
def testIsSemantic() = {
isSemanticVersion(semanticVersion) shouldBe true
isSemanticVersion(semanticVersionWithBuild) shouldBe true
isSemanticVersion(nonSemanticVersion) shouldBe false
}
@Test
def testMajorVersion() = {
majorVersion(semanticVersion) shouldBe Some(1)
majorVersion(semanticVersionWithBuild) shouldBe Some(1)
}
@Test
def testMinorVersion() = {
minorVersion(semanticVersion) shouldBe Some(2)
minorVersion(semanticVersionWithBuild) shouldBe Some(2)
}
@Test
def testPatchVersion() = {
patchVersion(semanticVersion) shouldBe Some(3)
patchVersion(semanticVersionWithBuild) shouldBe Some(3)
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment