Skip to content

Commit

Permalink
feat: fix bug that unable to fetch CLI version (#30)
Browse files Browse the repository at this point in the history
* feat: support "-v/--version" to get version of casbin-java-cli

* fix: unable to fetch CLI version
  • Loading branch information
sukidayou authored Dec 16, 2024
1 parent 783be0c commit b23b514
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
43 changes: 43 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,45 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>fetch-tags</id>
<phase>initialize</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>git</executable>
<arguments>
<argument>fetch</argument>
<argument>--tags</argument>
<argument>--force</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>generate-version</id>
<phase>initialize</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>bash</executable>
<arguments>
<argument>-c</argument>
<argument>
git describe --tags --abbrev=0
</argument>
</arguments>
<outputFile>${project.build.outputDirectory}/META-INF/lastCli.version</outputFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
Expand All @@ -119,6 +158,10 @@
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/META-INF/git.properties</generateGitPropertiesFilename>
<format>properties</format>
<gitDescribe>
<always>true</always>
<tags>true</tags>
</gitDescribe>
</configuration>
</plugin>
</plugins>
Expand Down
61 changes: 59 additions & 2 deletions src/main/java/org/casbin/util/VersionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,52 @@
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/***
* Utility class for retrieving version-related information.
*/
public class VersionUtil {

/***
* Calculates the new version based on the current version and the type of commit.
*
* @param currentVersion The current version string in the format "vX.Y.Z".
* @param commitType The type of commit that caused the version change (e.g., "fix", "feat", "breaking change").
* @return The calculated new version string in the format "vX.Y.Z".
*/
public static String calculateNewVersion(String currentVersion, String commitType) {
String[] versionParts = currentVersion.substring(1).split("\\.");
int major = Integer.parseInt(versionParts[0]);
int minor = Integer.parseInt(versionParts[1]);
int patch = Integer.parseInt(versionParts[2]);

switch (commitType.toLowerCase()) {
case "fix":
patch++;
break;
case "feat":
minor++;
patch = 0;
break;
case "breaking change":
major++;
minor = 0;
patch = 0;
break;
default:
return currentVersion;
}
return "v" + major + "." + minor + "." + patch;
}

/**
* Retrieves Cli version information.
*
Expand All @@ -50,12 +87,32 @@ public static String getCliVersion() throws IOException {
String tag = properties.getProperty("git.closest.tag.name", "Unknown");
String commitCount = properties.getProperty("git.closest.tag.commit.count", "Unknown");

if(tag.isEmpty()) {
String commitMessage = properties.getProperty("git.commit.message.full", "Unknown");
Pattern pattern = Pattern.compile("^(fix|feat|chore|docs|style|refactor|test|perf|build|ci):");
Matcher matcher = pattern.matcher(commitMessage);

if(tag.isEmpty() || tag.equals("Unknown")) {
tag = properties.getProperty("git.tags", "Unknown");
if(tag.isEmpty() || tag.equals("Unknown")) {
InputStream versionInputStream = Client.class.getResourceAsStream("/META-INF/lastCli.version");
if (versionInputStream != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(versionInputStream, StandardCharsets.UTF_8));
String version = reader.readLine();
if (version != null && matcher.find()) {
tag = calculateNewVersion(version, matcher.group(1));
}
}
}
}else if(!commitCount.isEmpty() && !commitCount.equals("0") && !commitCount.equals("Unknown") && matcher.find()) {
tag = calculateNewVersion(tag, matcher.group(1));
commitCount = "0";
}
if (commitCount.isEmpty()) {

if ((commitCount.isEmpty() || commitCount.equals("0") || commitCount.equals("Unknown"))
&& (!tag.isEmpty() && !tag.equals("Unknown"))) {
return tag;
}

return commitId;
}

Expand Down

0 comments on commit b23b514

Please sign in to comment.