20
20
import javax .xml .parsers .ParserConfigurationException ;
21
21
import javax .xml .parsers .SAXParser ;
22
22
import javax .xml .parsers .SAXParserFactory ;
23
+ import java .io .BufferedReader ;
23
24
import java .io .IOException ;
24
25
import java .io .InputStream ;
26
+ import java .io .InputStreamReader ;
27
+ import java .nio .charset .StandardCharsets ;
25
28
import java .util .Properties ;
29
+ import java .util .regex .Matcher ;
30
+ import java .util .regex .Pattern ;
26
31
27
32
/***
28
33
* Utility class for retrieving version-related information.
29
34
*/
30
35
public class VersionUtil {
31
36
37
+ /***
38
+ * Calculates the new version based on the current version and the type of commit.
39
+ *
40
+ * @param currentVersion The current version string in the format "vX.Y.Z".
41
+ * @param commitType The type of commit that caused the version change (e.g., "fix", "feat", "breaking change").
42
+ * @return The calculated new version string in the format "vX.Y.Z".
43
+ */
44
+ public static String calculateNewVersion (String currentVersion , String commitType ) {
45
+ String [] versionParts = currentVersion .substring (1 ).split ("\\ ." );
46
+ int major = Integer .parseInt (versionParts [0 ]);
47
+ int minor = Integer .parseInt (versionParts [1 ]);
48
+ int patch = Integer .parseInt (versionParts [2 ]);
49
+
50
+ switch (commitType .toLowerCase ()) {
51
+ case "fix" :
52
+ patch ++;
53
+ break ;
54
+ case "feat" :
55
+ minor ++;
56
+ patch = 0 ;
57
+ break ;
58
+ case "breaking change" :
59
+ major ++;
60
+ minor = 0 ;
61
+ patch = 0 ;
62
+ break ;
63
+ default :
64
+ return currentVersion ;
65
+ }
66
+ return "v" + major + "." + minor + "." + patch ;
67
+ }
68
+
32
69
/**
33
70
* Retrieves Cli version information.
34
71
*
@@ -50,12 +87,32 @@ public static String getCliVersion() throws IOException {
50
87
String tag = properties .getProperty ("git.closest.tag.name" , "Unknown" );
51
88
String commitCount = properties .getProperty ("git.closest.tag.commit.count" , "Unknown" );
52
89
53
- if (tag .isEmpty ()) {
90
+ String commitMessage = properties .getProperty ("git.commit.message.full" , "Unknown" );
91
+ Pattern pattern = Pattern .compile ("^(fix|feat|chore|docs|style|refactor|test|perf|build|ci):" );
92
+ Matcher matcher = pattern .matcher (commitMessage );
93
+
94
+ if (tag .isEmpty () || tag .equals ("Unknown" )) {
54
95
tag = properties .getProperty ("git.tags" , "Unknown" );
96
+ if (tag .isEmpty () || tag .equals ("Unknown" )) {
97
+ InputStream versionInputStream = Client .class .getResourceAsStream ("/META-INF/lastCli.version" );
98
+ if (versionInputStream != null ) {
99
+ BufferedReader reader = new BufferedReader (new InputStreamReader (versionInputStream , StandardCharsets .UTF_8 ));
100
+ String version = reader .readLine ();
101
+ if (version != null && matcher .find ()) {
102
+ tag = calculateNewVersion (version , matcher .group (1 ));
103
+ }
104
+ }
105
+ }
106
+ }else if (!commitCount .isEmpty () && !commitCount .equals ("0" ) && !commitCount .equals ("Unknown" ) && matcher .find ()) {
107
+ tag = calculateNewVersion (tag , matcher .group (1 ));
108
+ commitCount = "0" ;
55
109
}
56
- if (commitCount .isEmpty ()) {
110
+
111
+ if ((commitCount .isEmpty () || commitCount .equals ("0" ) || commitCount .equals ("Unknown" ))
112
+ && (!tag .isEmpty () && !tag .equals ("Unknown" ))) {
57
113
return tag ;
58
114
}
115
+
59
116
return commitId ;
60
117
}
61
118
0 commit comments