Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Mirrors
biopet.biopet
Commits
5620069f
Commit
5620069f
authored
Oct 12, 2016
by
Sander Bollen
Browse files
semantic version convenience methods
parent
2727f142
Changes
2
Hide whitespace changes
Inline
Side-by-side
biopet-utils/src/main/scala/nl/lumc/sasc/biopet/utils/package.scala
View file @
5620069f
...
...
@@ -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
}
}
}
}
biopet-utils/src/test/scala/nl/lumc/sasc/biopet/utils/UtilsTest.scala
0 → 100644
View file @
5620069f
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
)
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment