1
1
import java.io.File
2
2
import java.lang.IllegalArgumentException
3
3
4
- class BuildVersion (
5
- private val major : Int = 1 ,
6
- private val minor : Int = 0 ,
7
- private val patch : Int = 0 ,
8
- private val preRelease : String? = null ,
9
- private val buildMetadata : String? = null
10
- ) {
4
+ class BuildVersion (private val versionFile : File ) {
11
5
companion object {
12
-
13
6
private val VERSION_PATTERN = Regex (
14
7
""" (0|[1-9]\d*)?(?:\.)?(0|[1-9]\d*)?(?:\.)?(0|[1-9]\d*)?(?:-([\dA-z\-]+(?:\.[\dA-z\-]+)*))?(?:\+([\dA-z\-]+(?:\.[\dA-z\-]+)*))?"""
15
8
)
@@ -21,30 +14,33 @@ class BuildVersion(
21
14
private val BUILD_METADATA_PATTERN = Regex (
22
15
""" [\dA-z\-]+(?:\.[\dA-z\-]+)*"""
23
16
)
17
+ }
18
+
19
+ private var major: Int
20
+
21
+ private var minor: Int
24
22
25
- @JvmStatic
26
- fun parse (versionText : String ): BuildVersion {
23
+ private var patch: Int
24
+
25
+ private var preRelease: String?
26
+
27
+ private var buildMetadata: String?
28
+
29
+ init {
30
+ if (versionFile.exists() && versionFile.canRead()) {
31
+ val versionText = versionFile.readText()
27
32
val result = VERSION_PATTERN .matchEntire(versionText)
28
33
? : throw IllegalArgumentException (" Unable to parse build version: $versionText " )
29
- return BuildVersion (
30
- major = result.groupValues[1 ].toInt(),
31
- minor = result.groupValues[2 ].toInt(),
32
- patch = result.groupValues[3 ].toInt(),
33
- preRelease = if (result.groupValues[4 ].isEmpty()) null else result.groupValues[4 ],
34
- buildMetadata = if (result.groupValues[5 ].isEmpty()) null else result.groupValues[5 ]
35
- )
36
- }
37
34
38
- @JvmStatic
39
- fun parse ( versionFile : File ): BuildVersion =
40
- if (versionFile.exists() && versionFile.canRead()) {
41
- parse(versionFile.readText ())
42
- } else {
43
- throw IllegalArgumentException ( " Unable to read version file: ${versionFile.path} " )
44
- }
45
- }
35
+ major = result.groupValues[ 1 ].toInt()
36
+ minor = result.groupValues[ 2 ].toInt()
37
+ patch = result.groupValues[ 3 ].toInt()
38
+ preRelease = if (result.groupValues[ 4 ].isEmpty ()) null else result.groupValues[ 4 ]
39
+ buildMetadata = if (result.groupValues[ 5 ].isEmpty()) null else result.groupValues[ 5 ]
40
+ } else {
41
+ throw IllegalArgumentException ( " Unable to read version file: ${versionFile.path} " )
42
+ }
46
43
47
- init {
48
44
require(major >= 0 ) { " Major version must be a positive number" }
49
45
require(minor >= 0 ) { " Minor version must be a positive number" }
50
46
require(patch >= 0 ) { " Patch version must be a positive number" }
@@ -55,21 +51,60 @@ class BuildVersion(
55
51
}
56
52
57
53
buildMetadata?.let {
58
- require(buildMetadata .matches(BUILD_METADATA_PATTERN )) { " Build metadata is not valid" }
54
+ require(it .matches(BUILD_METADATA_PATTERN )) { " Build metadata is not valid" }
59
55
}
60
56
}
61
57
62
- val versionCode: Int = major * 10000 + minor * 1000 + patch
58
+ val versionCode: Int
59
+ get() = major * 10000 + minor * 1000 + patch
63
60
64
- val versionName: String = buildString {
65
- append(" $major .$minor .$patch " )
61
+ val versionName: String
62
+ get() = buildString {
63
+ append(" $major .$minor .$patch " )
66
64
67
- preRelease?.let {
68
- append(" -$it " )
65
+ preRelease?.let {
66
+ append(" -$it " )
67
+ }
68
+
69
+ buildMetadata?.let {
70
+ append(" +$it " )
71
+ }
69
72
}
70
73
71
- buildMetadata?.let {
72
- append(" +$it " )
74
+ // Trim pre-release and build metadata
75
+ fun prepareProdRelease () {
76
+ preRelease = null
77
+ buildMetadata = null
78
+ }
79
+
80
+ fun incrementMajor () {
81
+ major++
82
+ minor = 0
83
+ patch = 0
84
+
85
+ versionFile.writeText(versionName)
86
+ }
87
+
88
+ fun incrementMinor () {
89
+ minor++
90
+ patch = 0
91
+
92
+ if (minor >= 1000 ) {
93
+ major++
94
+ minor = 0
73
95
}
96
+
97
+ versionFile.writeText(versionName)
98
+ }
99
+
100
+ fun incrementPatch () {
101
+ patch++
102
+
103
+ if (patch >= 1000 ) {
104
+ minor++
105
+ patch = 0
106
+ }
107
+
108
+ versionFile.writeText(versionName)
74
109
}
75
110
}
0 commit comments