|
21 | 21 |
|
22 | 22 | package com.vaticle.dependencies.tool.release.notes |
23 | 23 |
|
24 | | -data class Version(val major: Int, val minor: Int, val patch: Int, val alpha: Int?): Comparable<Version> { |
| 24 | +data class Version(val major: Int, val minor: Int, val patch: Int, val rc: Int?, val alpha: Int?): Comparable<Version> { |
25 | 25 | companion object { |
26 | 26 | fun parse(version: String): Version { |
27 | | - val version1 = version.split("-") |
28 | | - require(version1.isNotEmpty() && version1.size <= 3) { |
29 | | - "version '$version1' does not follow the form 'x.y.z', 'x.y.z-alpha', or 'x.y.z-alpha-n'" |
30 | | - } |
31 | | - val version2 = version1[0].split(".") |
32 | | - if (version1.size == 1) { |
33 | | - require(version2.size == 3) { "version must be of the form x.y.z" } |
34 | | - return Version( |
35 | | - major = version2[0].toInt(), |
36 | | - minor = version2[1].toInt(), |
37 | | - patch = version2[2].toInt(), |
38 | | - alpha = null |
39 | | - ) |
40 | | - } else if (version1.size == 2) { |
41 | | - return Version( |
42 | | - major = version2[0].toInt(), |
43 | | - minor = version2[1].toInt(), |
44 | | - patch = version2[2].toInt(), |
45 | | - alpha = 1 |
46 | | - ) |
47 | | - } else { |
48 | | - return Version( |
49 | | - major = version2[0].toInt(), |
50 | | - minor = version2[1].toInt(), |
51 | | - patch = version2[2].toInt(), |
52 | | - alpha = version1[2].toInt() |
53 | | - ) |
| 27 | + val components = version.split("-") |
| 28 | + val (major, minor, patch) = components[0].split(".").map(String::toInt) |
| 29 | + return when (components.size) { |
| 30 | + 1 -> Version(major = major, minor = minor, patch = patch, rc = null, alpha = null) |
| 31 | + 2 -> when { |
| 32 | + components[1] == "alpha" -> Version(major = major, minor = minor, patch = patch, rc = null, alpha = 1) |
| 33 | + components[1].startsWith("rc") -> Version( |
| 34 | + major = major, |
| 35 | + minor = minor, |
| 36 | + patch = patch, |
| 37 | + rc = components[1].removePrefix("rc").toInt(), |
| 38 | + alpha = null |
| 39 | + ) |
| 40 | + else -> { |
| 41 | + throw IllegalArgumentException( |
| 42 | + "version '$version' does not follow the form 'x.y.z', 'x.y.z-rcN', 'x.y.z-alpha', or 'x.y.z-alpha-N'" |
| 43 | + ) |
| 44 | + } |
| 45 | + } |
| 46 | + 3 -> { |
| 47 | + require(components[1] == "alpha") { |
| 48 | + "version '$version' does not follow the form 'x.y.z', 'x.y.z-rcN', 'x.y.z-alpha', or 'x.y.z-alpha-N'" |
| 49 | + } |
| 50 | + Version(major = major, minor = minor, patch = patch, rc = null, alpha = components[2].toInt()) |
| 51 | + } |
| 52 | + else -> { |
| 53 | + throw IllegalArgumentException( |
| 54 | + "version '$version' does not follow the form 'x.y.z', 'x.y.z-rcN', 'x.y.z-alpha', or 'x.y.z-alpha-N'" |
| 55 | + ) |
| 56 | + } |
54 | 57 | } |
55 | 58 | } |
56 | 59 | } |
57 | 60 |
|
58 | 61 | override fun compareTo(other: Version): Int { |
59 | 62 | val major = major.compareTo(other.major) |
60 | | - if (major == 0) { |
61 | | - val minor = minor.compareTo(other.minor) |
62 | | - if (minor == 0) { |
63 | | - val patch = patch.compareTo(other.patch) |
64 | | - if (patch == 0) { |
65 | | - if (alpha == null) { |
66 | | - if (other.alpha == null) return 0 |
67 | | - else return 1 |
68 | | - } else { |
69 | | - if (other.alpha != null) return alpha.compareTo(other.alpha) |
70 | | - else return -1 |
71 | | - } |
72 | | - } else return patch |
73 | | - } else return minor |
74 | | - } else return major |
| 63 | + if (major != 0) return major |
| 64 | + |
| 65 | + val minor = minor.compareTo(other.minor) |
| 66 | + if (minor != 0) return minor |
| 67 | + |
| 68 | + val patch = patch.compareTo(other.patch) |
| 69 | + if (patch != 0) return patch |
| 70 | + |
| 71 | + if (rc != null) { |
| 72 | + if (other.rc != null) return rc.compareTo(other.rc) |
| 73 | + else if (other.alpha != null) return 1 |
| 74 | + else return -1 |
| 75 | + } |
| 76 | + |
| 77 | + if (alpha != null) { |
| 78 | + if (other.alpha != null) return alpha.compareTo(other.alpha) |
| 79 | + else return -1 |
| 80 | + } |
| 81 | + |
| 82 | + return 1 // x.y.z is always after x.y.z-prereleases |
75 | 83 | } |
76 | 84 |
|
77 | 85 | override fun toString(): String { |
78 | | - val alpha = |
79 | | - if (alpha != null) { |
80 | | - if (alpha == 1) "-alpha" |
81 | | - else "-alpha-$alpha" |
82 | | - } else "" |
83 | | - return "$major.$minor.$patch$alpha" |
| 86 | + val prerelease = when { |
| 87 | + rc != null -> "-rc$rc" |
| 88 | + alpha == 1 -> "-alpha" |
| 89 | + alpha != null -> "-alpha-$alpha" |
| 90 | + else -> "" |
| 91 | + } |
| 92 | + return "$major.$minor.$patch$prerelease" |
84 | 93 | } |
| 94 | + |
| 95 | + fun isPrerelease(): Boolean = rc != null || alpha != null |
85 | 96 | } |
0 commit comments