Skip to content

Commit b23b514

Browse files
authored
feat: fix bug that unable to fetch CLI version (#30)
* feat: support "-v/--version" to get version of casbin-java-cli * fix: unable to fetch CLI version
1 parent 783be0c commit b23b514

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

pom.xml

+43
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,45 @@
103103
</execution>
104104
</executions>
105105
</plugin>
106+
<plugin>
107+
<groupId>org.codehaus.mojo</groupId>
108+
<artifactId>exec-maven-plugin</artifactId>
109+
<version>3.5.0</version>
110+
<executions>
111+
<execution>
112+
<id>fetch-tags</id>
113+
<phase>initialize</phase>
114+
<goals>
115+
<goal>exec</goal>
116+
</goals>
117+
<configuration>
118+
<executable>git</executable>
119+
<arguments>
120+
<argument>fetch</argument>
121+
<argument>--tags</argument>
122+
<argument>--force</argument>
123+
</arguments>
124+
</configuration>
125+
</execution>
126+
<execution>
127+
<id>generate-version</id>
128+
<phase>initialize</phase>
129+
<goals>
130+
<goal>exec</goal>
131+
</goals>
132+
<configuration>
133+
<executable>bash</executable>
134+
<arguments>
135+
<argument>-c</argument>
136+
<argument>
137+
git describe --tags --abbrev=0
138+
</argument>
139+
</arguments>
140+
<outputFile>${project.build.outputDirectory}/META-INF/lastCli.version</outputFile>
141+
</configuration>
142+
</execution>
143+
</executions>
144+
</plugin>
106145
<plugin>
107146
<groupId>pl.project13.maven</groupId>
108147
<artifactId>git-commit-id-plugin</artifactId>
@@ -119,6 +158,10 @@
119158
<generateGitPropertiesFile>true</generateGitPropertiesFile>
120159
<generateGitPropertiesFilename>${project.build.outputDirectory}/META-INF/git.properties</generateGitPropertiesFilename>
121160
<format>properties</format>
161+
<gitDescribe>
162+
<always>true</always>
163+
<tags>true</tags>
164+
</gitDescribe>
122165
</configuration>
123166
</plugin>
124167
</plugins>

src/main/java/org/casbin/util/VersionUtil.java

+59-2
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,52 @@
2020
import javax.xml.parsers.ParserConfigurationException;
2121
import javax.xml.parsers.SAXParser;
2222
import javax.xml.parsers.SAXParserFactory;
23+
import java.io.BufferedReader;
2324
import java.io.IOException;
2425
import java.io.InputStream;
26+
import java.io.InputStreamReader;
27+
import java.nio.charset.StandardCharsets;
2528
import 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
*/
3035
public 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

Comments
 (0)