Skip to content

Commit 4759db0

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

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
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 java.io.IOException;
811
import java.util.*;
12+
import java.io.BufferedReader;
13+
import java.io.InputStreamReader;
14+
import javax.xml.parsers.ParserConfigurationException;
15+
import javax.xml.parsers.SAXParser;
16+
import javax.xml.parsers.SAXParserFactory;
17+
import java.io.File;
918

1019
public class Client {
1120

@@ -21,6 +30,15 @@ public static String run(String... args) {
2130
if(Objects.equals(commandName, "-h") || Objects.equals(commandName, "--help")){
2231
printHelpMessage();
2332
return result;
33+
} else if(Objects.equals(commandName, "-v") || Objects.equals(commandName, "--version")){
34+
try{
35+
System.out.println("casbin-java-cli " + getGitVersion() + "\njcasbin " + getDependencyVersion("org.casbin","jcasbin"));
36+
}catch (Exception e) {
37+
System.out.println("Failed to retrieve version information.");
38+
e.printStackTrace();
39+
System.out.println("Run './casbin --help or ./casbin -h' for usage.");
40+
}
41+
return result;
2442
}
2543

2644
// processing line breaks in parameters
@@ -117,4 +135,65 @@ private static void printHelpMessage() {
117135
" For more information, visit https://github.com/casbin/casbin");
118136

119137
}
138+
139+
/**
140+
* Retrieves the Git version.
141+
*
142+
* @return The latest Git tag if available; otherwise, the short hash of the latest commit.
143+
* @throws Exception If both the tag and commit hash cannot be retrieved.
144+
*/
145+
public static String getGitVersion() throws Exception {
146+
String tag = executeGitCommand("git describe --tags --abbrev=0");
147+
if (tag == null || tag.isEmpty()) {
148+
String commitHash = executeGitCommand("git rev-parse --short HEAD");
149+
if (commitHash == null || commitHash.isEmpty()) {
150+
throw new RuntimeException("Failed to get Git version (Tag or Commit Hash)");
151+
}
152+
return commitHash.trim();
153+
}
154+
return tag.trim();
155+
}
156+
157+
/**
158+
* Executes a Git command and retrieves its output.
159+
*
160+
* @param command The Git command to execute.
161+
* @return The first line of the command's output if successful; otherwise, null.
162+
* @throws Exception If an error occurs while running the command or reading its output.
163+
*/
164+
private static String executeGitCommand(String command) throws Exception {
165+
Process process = Runtime.getRuntime().exec(command);
166+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
167+
String result = reader.readLine();
168+
int exitCode = process.waitFor();
169+
if (exitCode != 0) {
170+
return null;
171+
}
172+
return result;
173+
}
174+
}
175+
176+
/**
177+
* Retrieves the version of a specific dependency from the Maven POM file located in the project root directory.
178+
*
179+
* @param groupId the groupId of the dependency to search for.
180+
* @param artifactId the artifactId of the dependency to search for.
181+
* @return the version of the specified dependency, or null if the dependency is not found.
182+
* @throws ParserConfigurationException if a configuration error occurs during the creation of the SAX parser.
183+
* @throws IOException if an I/O error occurs while reading the POM file.
184+
* @throws SAXException if a parsing error occurs while processing the POM file.
185+
*/
186+
public static String getDependencyVersion(String groupId, String artifactId) throws ParserConfigurationException, IOException, SAXException {
187+
String projectRootPath = System.getProperty("user.dir");
188+
String pomFilePath = projectRootPath + File.separator + "pom.xml";
189+
190+
SAXParserFactory factory = SAXParserFactory.newInstance();
191+
SAXParser saxParser = factory.newSAXParser();
192+
193+
DependencyHandler handler = new DependencyHandler(groupId, artifactId);
194+
195+
saxParser.parse(new File(pomFilePath), handler);
196+
197+
return handler.getVersion();
198+
}
120199
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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.Attributes;
18+
import org.xml.sax.SAXException;
19+
import org.xml.sax.helpers.DefaultHandler;
20+
21+
/***
22+
* A custom SAX handler for parsing Maven POM files to retrieve the version
23+
* of a specific dependency based on its groupId and artifactId.
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 = null;
30+
private String currentArtifactId = null;
31+
private String version = null;
32+
private boolean isDependency = false;
33+
34+
/**
35+
* Constructs a DependencyHandler instance.
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+
* Gets the version of the target dependency.
47+
*
48+
* @return the version of the dependency if found, or null if not found.
49+
*/
50+
public String getVersion() {
51+
return version;
52+
}
53+
54+
/**
55+
* Called when the start of an XML element is encountered.
56+
*
57+
* @param uri the Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
58+
* @param localName the local name (without prefix), or the empty string if Namespace processing is not being performed.
59+
* @param qName the qualified name (with prefix, if present).
60+
* @param attributes the attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
61+
* @throws SAXException if a SAX error occurs.
62+
*/
63+
@Override
64+
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
65+
currentElement = qName;
66+
67+
if ("dependency".equals(qName)) {
68+
isDependency = true;
69+
}
70+
}
71+
72+
/**
73+
* Called when the end of an XML element is encountered.
74+
*
75+
* @param uri the Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
76+
* @param localName the local name (without prefix), or the empty string if Namespace processing is not being performed.
77+
* @param qName the qualified name (with prefix, if present).
78+
* @throws SAXException if a SAX error occurs.
79+
*/
80+
@Override
81+
public void endElement(String uri, String localName, String qName) throws SAXException {
82+
if ("dependency".equals(qName)) {
83+
if (targetGroupId.equals(currentGroupId) && targetArtifactId.equals(currentArtifactId)) {
84+
isDependency = false;
85+
}
86+
87+
currentGroupId = null;
88+
currentArtifactId = null;
89+
}
90+
currentElement = "";
91+
}
92+
93+
/**
94+
* Called when character data inside an XML element is encountered.
95+
*
96+
* @param ch the characters.
97+
* @param start the start position in the character array.
98+
* @param length the number of characters to read from the array.
99+
* @throws SAXException if a SAX error occurs.
100+
*/
101+
@Override
102+
public void characters(char[] ch, int start, int length) throws SAXException {
103+
if (isDependency) {
104+
String content = new String(ch, start, length).trim();
105+
106+
switch (currentElement) {
107+
case "groupId":
108+
currentGroupId = content;
109+
break;
110+
case "artifactId":
111+
currentArtifactId = content;
112+
break;
113+
case "version":
114+
if (targetGroupId.equals(currentGroupId) && targetArtifactId.equals(currentArtifactId)) {
115+
version = content;
116+
}
117+
break;
118+
}
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)