Skip to content

Commit c6dbd69

Browse files
committed
feat: support "-v/--version" to get version of casbin-java-cli
1 parent bad4575 commit c6dbd69

File tree

3 files changed

+225
-0
lines changed

3 files changed

+225
-0
lines changed

pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,49 @@
7878
</execution>
7979
</executions>
8080
</plugin>
81+
<plugin>
82+
<groupId>org.apache.maven.plugins</groupId>
83+
<artifactId>maven-resources-plugin</artifactId>
84+
<version>3.3.0</version>
85+
<executions>
86+
<execution>
87+
<id>generate-version-info</id>
88+
<phase>validate</phase>
89+
<goals>
90+
<goal>copy-resources</goal>
91+
</goals>
92+
<configuration>
93+
<outputDirectory>${project.build.outputDirectory}/META-INF</outputDirectory>
94+
<resources>
95+
<resource>
96+
<directory>${project.basedir}</directory>
97+
<includes>
98+
<include>pom.xml</include>
99+
</includes>
100+
</resource>
101+
</resources>
102+
</configuration>
103+
</execution>
104+
</executions>
105+
</plugin>
106+
<plugin>
107+
<groupId>pl.project13.maven</groupId>
108+
<artifactId>git-commit-id-plugin</artifactId>
109+
<version>4.9.10</version>
110+
<executions>
111+
<execution>
112+
<goals>
113+
<goal>revision</goal>
114+
</goals>
115+
</execution>
116+
</executions>
117+
<configuration>
118+
<abbrevLength>7</abbrevLength>
119+
<generateGitPropertiesFile>true</generateGitPropertiesFile>
120+
<generateGitPropertiesFilename>${project.build.outputDirectory}/META-INF/git.properties</generateGitPropertiesFilename>
121+
<format>properties</format>
122+
</configuration>
123+
</plugin>
81124
</plugins>
82125
</build>
83126
</project>

src/main/java/org/casbin/Client.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
import org.apache.commons.cli.*;
44
import org.casbin.generate.DynamicClassGenerator;
55
import org.casbin.jcasbin.util.function.CustomFunction;
6+
import org.casbin.util.DependencyHandler;
67
import org.casbin.util.Util;
8+
import org.xml.sax.SAXException;
79

10+
import javax.xml.parsers.ParserConfigurationException;
11+
import javax.xml.parsers.SAXParser;
12+
import javax.xml.parsers.SAXParserFactory;
13+
import java.io.IOException;
14+
import java.io.InputStream;
815
import java.util.*;
916

1017
public class Client {
@@ -21,6 +28,14 @@ public static String run(String... args) {
2128
if(Objects.equals(commandName, "-h") || Objects.equals(commandName, "--help")){
2229
printHelpMessage();
2330
return result;
31+
} else if(Objects.equals(commandName, "-v") || Objects.equals(commandName, "--version")){
32+
try{
33+
System.out.println("casbin-java-cli " + getGitVersionInfo() + "\njcasbin " + getDependencyVersion("org.casbin","jcasbin"));
34+
}catch (Exception e) {
35+
System.out.println("Failed to retrieve version information.");
36+
e.printStackTrace();
37+
}
38+
return result;
2439
}
2540

2641
// processing line breaks in parameters
@@ -117,4 +132,60 @@ private static void printHelpMessage() {
117132
" For more information, visit https://github.com/casbin/casbin");
118133

119134
}
135+
136+
/**
137+
* Retrieves Git version information.
138+
*
139+
* @return The Git version info as a string (commit ID or tag name).
140+
* @throws IOException If an error occurs while reading the git.properties file.
141+
*/
142+
public static String getGitVersionInfo() throws IOException {
143+
Properties properties = new Properties();
144+
InputStream input = Client.class.getResourceAsStream("/META-INF/git.properties");
145+
if (input != null) {
146+
properties.load(input);
147+
}
148+
149+
if (properties.isEmpty()) {
150+
System.out.println("Git properties not found!");
151+
return null;
152+
}
153+
154+
String commitId = properties.getProperty("git.commit.id.abbrev", "Unknown");
155+
String tag = properties.getProperty("git.closest.tag.name", "Unknown");
156+
String commitCount = properties.getProperty("git.closest.tag.commit.count", "Unknown");
157+
158+
if(tag.isEmpty()) {
159+
tag = properties.getProperty("git.tags", "Unknown");
160+
}
161+
if (commitCount.isEmpty()) {
162+
return tag;
163+
}
164+
return commitId;
165+
}
166+
167+
/**
168+
* Retrieves dependency version information.
169+
*
170+
* @param groupId The group ID of the dependency to search for.
171+
* @param artifactId The artifact ID of the dependency to search for.
172+
* @return The version of the specified dependency, or "Unknown" if not found.
173+
* @throws ParserConfigurationException If a configuration error occurs during parsing.
174+
* @throws SAXException If a SAX error occurs during parsing.
175+
* @throws IOException If an error occurs while reading the POM file.
176+
*/
177+
public static String getDependencyVersion(String groupId, String artifactId) throws ParserConfigurationException, SAXException, IOException {
178+
InputStream inputStream = Client.class.getResourceAsStream("/META-INF/pom.xml");
179+
if (inputStream == null) {
180+
System.out.println("POM file not found!");
181+
return "Unknown";
182+
}
183+
184+
SAXParserFactory factory = SAXParserFactory.newInstance();
185+
SAXParser parser = factory.newSAXParser();
186+
DependencyHandler handler = new DependencyHandler(groupId, artifactId);
187+
parser.parse(inputStream, handler);
188+
189+
return handler.getDependencyVersion();
190+
}
120191
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2024 The casbin Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package org.casbin.util;
16+
17+
import org.xml.sax.helpers.DefaultHandler;
18+
import org.xml.sax.*;
19+
20+
/***
21+
* A custom handler for parsing XML dependencies (POM file format) using SAX (Simple API for XML).
22+
* This handler looks for a specific dependency identified by the group ID and artifact ID,
23+
* and extracts its version information.
24+
*/
25+
public class DependencyHandler extends DefaultHandler {
26+
private final String targetGroupId;
27+
private final String targetArtifactId;
28+
private String currentElement;
29+
private String currentGroupId;
30+
private String currentArtifactId;
31+
private String currentVersion;
32+
private String dependencyVersion;
33+
34+
/**
35+
* Constructor to initialize the handler with the target groupId and artifactId.
36+
*
37+
* @param groupId The groupId of the dependency to search for.
38+
* @param artifactId The artifactId of the dependency to search for.
39+
*/
40+
public DependencyHandler(String groupId, String artifactId) {
41+
this.targetGroupId = groupId;
42+
this.targetArtifactId = artifactId;
43+
}
44+
45+
/**
46+
* Called when a new element is encountered during the XML parsing.
47+
*
48+
* @param uri The namespace URI (if any).
49+
* @param localName The local name of the element.
50+
* @param qName The qualified name of the element.
51+
* @param attributes The attributes of the element.
52+
*/
53+
@Override
54+
public void startElement(String uri, String localName, String qName, Attributes attributes) {
55+
currentElement = qName;
56+
if ("dependency".equals(currentElement)) {
57+
currentGroupId = null;
58+
currentArtifactId = null;
59+
currentVersion = null;
60+
}
61+
}
62+
63+
/**
64+
* Called when the end of an element is reached during XML parsing.
65+
*
66+
* @param uri The namespace URI (if any).
67+
* @param localName The local name of the element.
68+
* @param qName The qualified name of the element.
69+
*/
70+
@Override
71+
public void endElement(String uri, String localName, String qName) {
72+
if ("dependency".equals(qName)) {
73+
if (targetGroupId.equals(currentGroupId) && targetArtifactId.equals(currentArtifactId)) {
74+
dependencyVersion = currentVersion;
75+
}
76+
}
77+
currentElement = null;
78+
}
79+
80+
/**
81+
* Called to process the character data inside an element during XML parsing.
82+
*
83+
* @param ch The character array containing the text.
84+
* @param start The start index of the text.
85+
* @param length The length of the text.
86+
*/
87+
@Override
88+
public void characters(char[] ch, int start, int length) {
89+
String content = new String(ch, start, length).trim();
90+
if (content.isEmpty()) {
91+
return;
92+
}
93+
94+
if ("groupId".equals(currentElement)) {
95+
currentGroupId = content;
96+
} else if ("artifactId".equals(currentElement)) {
97+
currentArtifactId = content;
98+
} else if ("version".equals(currentElement)) {
99+
currentVersion = content;
100+
}
101+
}
102+
103+
/**
104+
* Returns the version of the dependency if found, otherwise returns "Unknown".
105+
*
106+
* @return The version of the target dependency.
107+
*/
108+
public String getDependencyVersion() {
109+
return dependencyVersion != null ? dependencyVersion : "Unknown";
110+
}
111+
}

0 commit comments

Comments
 (0)