Skip to content

Commit 783be0c

Browse files
authored
feat: support "-v/--version" to get version of casbin-java-cli (#28)
1 parent bad4575 commit 783be0c

File tree

4 files changed

+246
-0
lines changed

4 files changed

+246
-0
lines changed

pom.xml

+43
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

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import java.util.*;
99

10+
import static org.casbin.util.VersionUtil.*;
11+
1012
public class Client {
1113

1214
public static String run(String... args) {
@@ -21,6 +23,11 @@ public static String run(String... args) {
2123
if(Objects.equals(commandName, "-h") || Objects.equals(commandName, "--help")){
2224
printHelpMessage();
2325
return result;
26+
} else if(Objects.equals(commandName, "-v") || Objects.equals(commandName, "--version")){
27+
String cliVersion = getCliVersion();
28+
String jcasbinVersion = getCasbinVersion("org.casbin","jcasbin");
29+
System.out.printf("casbin-java-cli %s\njcasbin %s%n",cliVersion,jcasbinVersion);
30+
return result;
2431
}
2532

2633
// processing line breaks in parameters
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.casbin.Client;
18+
import org.xml.sax.SAXException;
19+
20+
import javax.xml.parsers.ParserConfigurationException;
21+
import javax.xml.parsers.SAXParser;
22+
import javax.xml.parsers.SAXParserFactory;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.util.Properties;
26+
27+
/***
28+
* Utility class for retrieving version-related information.
29+
*/
30+
public class VersionUtil {
31+
32+
/**
33+
* Retrieves Cli version information.
34+
*
35+
* @return The Cli version info as a string (commit ID or tag name).
36+
* @throws IOException If an error occurs while reading the git.properties file.
37+
*/
38+
public static String getCliVersion() throws IOException {
39+
Properties properties = new Properties();
40+
InputStream input = Client.class.getResourceAsStream("/META-INF/git.properties");
41+
if (input != null) {
42+
properties.load(input);
43+
}
44+
45+
if (properties.isEmpty()) {
46+
throw new IOException("Git properties not found!");
47+
}
48+
49+
String commitId = properties.getProperty("git.commit.id.abbrev", "Unknown");
50+
String tag = properties.getProperty("git.closest.tag.name", "Unknown");
51+
String commitCount = properties.getProperty("git.closest.tag.commit.count", "Unknown");
52+
53+
if(tag.isEmpty()) {
54+
tag = properties.getProperty("git.tags", "Unknown");
55+
}
56+
if (commitCount.isEmpty()) {
57+
return tag;
58+
}
59+
return commitId;
60+
}
61+
62+
/**
63+
* Retrieves Casbin version information.
64+
*
65+
* @param groupId The group ID of the dependency to search for.
66+
* @param artifactId The artifact ID of the dependency to search for.
67+
* @return The version of the specified dependency, or "Unknown" if not found.
68+
* @throws ParserConfigurationException If a configuration error occurs during parsing.
69+
* @throws SAXException If a SAX error occurs during parsing.
70+
* @throws IOException If an error occurs while reading the POM file.
71+
*/
72+
public static String getCasbinVersion(String groupId, String artifactId) throws ParserConfigurationException, SAXException, IOException {
73+
InputStream inputStream = Client.class.getResourceAsStream("/META-INF/pom.xml");
74+
if (inputStream == null) {
75+
throw new IOException("POM file not found!");
76+
}
77+
78+
SAXParserFactory factory = SAXParserFactory.newInstance();
79+
SAXParser parser = factory.newSAXParser();
80+
DependencyHandler handler = new DependencyHandler(groupId, artifactId);
81+
parser.parse(inputStream, handler);
82+
83+
return handler.getDependencyVersion();
84+
}
85+
}

0 commit comments

Comments
 (0)