2020import javax .xml .parsers .ParserConfigurationException ;
2121import javax .xml .parsers .SAXParser ;
2222import javax .xml .parsers .SAXParserFactory ;
23+ import java .io .BufferedReader ;
2324import java .io .IOException ;
2425import java .io .InputStream ;
26+ import java .io .InputStreamReader ;
27+ import java .nio .charset .StandardCharsets ;
2528import java .util .Properties ;
29+ import java .util .regex .Matcher ;
30+ import java .util .regex .Pattern ;
2631
2732/***
2833 * Utility class for retrieving version-related information.
2934 */
3035public class VersionUtil {
3136
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+
3269 /**
3370 * Retrieves Cli version information.
3471 *
@@ -50,12 +87,32 @@ public static String getCliVersion() throws IOException {
5087 String tag = properties .getProperty ("git.closest.tag.name" , "Unknown" );
5188 String commitCount = properties .getProperty ("git.closest.tag.commit.count" , "Unknown" );
5289
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" )) {
5495 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" ;
55109 }
56- if (commitCount .isEmpty ()) {
110+
111+ if ((commitCount .isEmpty () || commitCount .equals ("0" ) || commitCount .equals ("Unknown" ))
112+ && (!tag .isEmpty () && !tag .equals ("Unknown" ))) {
57113 return tag ;
58114 }
115+
59116 return commitId ;
60117 }
61118
0 commit comments