-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support "-v/--version" to get version of casbin-java-cli (#28)
- Loading branch information
Showing
4 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2024 The casbin Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.casbin.util; | ||
|
||
import org.xml.sax.helpers.DefaultHandler; | ||
import org.xml.sax.*; | ||
|
||
/*** | ||
* A custom handler for parsing XML dependencies (POM file format) using SAX (Simple API for XML). | ||
* This handler looks for a specific dependency identified by the group ID and artifact ID, | ||
* and extracts its version information. | ||
*/ | ||
public class DependencyHandler extends DefaultHandler { | ||
private final String targetGroupId; | ||
private final String targetArtifactId; | ||
private String currentElement; | ||
private String currentGroupId; | ||
private String currentArtifactId; | ||
private String currentVersion; | ||
private String dependencyVersion; | ||
|
||
/** | ||
* Constructor to initialize the handler with the target groupId and artifactId. | ||
* | ||
* @param groupId The groupId of the dependency to search for. | ||
* @param artifactId The artifactId of the dependency to search for. | ||
*/ | ||
public DependencyHandler(String groupId, String artifactId) { | ||
this.targetGroupId = groupId; | ||
this.targetArtifactId = artifactId; | ||
} | ||
|
||
/** | ||
* Called when a new element is encountered during the XML parsing. | ||
* | ||
* @param uri The namespace URI (if any). | ||
* @param localName The local name of the element. | ||
* @param qName The qualified name of the element. | ||
* @param attributes The attributes of the element. | ||
*/ | ||
@Override | ||
public void startElement(String uri, String localName, String qName, Attributes attributes) { | ||
currentElement = qName; | ||
if ("dependency".equals(currentElement)) { | ||
currentGroupId = null; | ||
currentArtifactId = null; | ||
currentVersion = null; | ||
} | ||
} | ||
|
||
/** | ||
* Called when the end of an element is reached during XML parsing. | ||
* | ||
* @param uri The namespace URI (if any). | ||
* @param localName The local name of the element. | ||
* @param qName The qualified name of the element. | ||
*/ | ||
@Override | ||
public void endElement(String uri, String localName, String qName) { | ||
if ("dependency".equals(qName)) { | ||
if (targetGroupId.equals(currentGroupId) && targetArtifactId.equals(currentArtifactId)) { | ||
dependencyVersion = currentVersion; | ||
} | ||
} | ||
currentElement = null; | ||
} | ||
|
||
/** | ||
* Called to process the character data inside an element during XML parsing. | ||
* | ||
* @param ch The character array containing the text. | ||
* @param start The start index of the text. | ||
* @param length The length of the text. | ||
*/ | ||
@Override | ||
public void characters(char[] ch, int start, int length) { | ||
String content = new String(ch, start, length).trim(); | ||
if (content.isEmpty()) { | ||
return; | ||
} | ||
|
||
if ("groupId".equals(currentElement)) { | ||
currentGroupId = content; | ||
} else if ("artifactId".equals(currentElement)) { | ||
currentArtifactId = content; | ||
} else if ("version".equals(currentElement)) { | ||
currentVersion = content; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the version of the dependency if found, otherwise returns "Unknown". | ||
* | ||
* @return The version of the target dependency. | ||
*/ | ||
public String getDependencyVersion() { | ||
return dependencyVersion != null ? dependencyVersion : "Unknown"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2024 The casbin Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.casbin.util; | ||
|
||
import org.casbin.Client; | ||
import org.xml.sax.SAXException; | ||
|
||
import javax.xml.parsers.ParserConfigurationException; | ||
import javax.xml.parsers.SAXParser; | ||
import javax.xml.parsers.SAXParserFactory; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Properties; | ||
|
||
/*** | ||
* Utility class for retrieving version-related information. | ||
*/ | ||
public class VersionUtil { | ||
|
||
/** | ||
* Retrieves Cli version information. | ||
* | ||
* @return The Cli version info as a string (commit ID or tag name). | ||
* @throws IOException If an error occurs while reading the git.properties file. | ||
*/ | ||
public static String getCliVersion() throws IOException { | ||
Properties properties = new Properties(); | ||
InputStream input = Client.class.getResourceAsStream("/META-INF/git.properties"); | ||
if (input != null) { | ||
properties.load(input); | ||
} | ||
|
||
if (properties.isEmpty()) { | ||
throw new IOException("Git properties not found!"); | ||
} | ||
|
||
String commitId = properties.getProperty("git.commit.id.abbrev", "Unknown"); | ||
String tag = properties.getProperty("git.closest.tag.name", "Unknown"); | ||
String commitCount = properties.getProperty("git.closest.tag.commit.count", "Unknown"); | ||
|
||
if(tag.isEmpty()) { | ||
tag = properties.getProperty("git.tags", "Unknown"); | ||
} | ||
if (commitCount.isEmpty()) { | ||
return tag; | ||
} | ||
return commitId; | ||
} | ||
|
||
/** | ||
* Retrieves Casbin version information. | ||
* | ||
* @param groupId The group ID of the dependency to search for. | ||
* @param artifactId The artifact ID of the dependency to search for. | ||
* @return The version of the specified dependency, or "Unknown" if not found. | ||
* @throws ParserConfigurationException If a configuration error occurs during parsing. | ||
* @throws SAXException If a SAX error occurs during parsing. | ||
* @throws IOException If an error occurs while reading the POM file. | ||
*/ | ||
public static String getCasbinVersion(String groupId, String artifactId) throws ParserConfigurationException, SAXException, IOException { | ||
InputStream inputStream = Client.class.getResourceAsStream("/META-INF/pom.xml"); | ||
if (inputStream == null) { | ||
throw new IOException("POM file not found!"); | ||
} | ||
|
||
SAXParserFactory factory = SAXParserFactory.newInstance(); | ||
SAXParser parser = factory.newSAXParser(); | ||
DependencyHandler handler = new DependencyHandler(groupId, artifactId); | ||
parser.parse(inputStream, handler); | ||
|
||
return handler.getDependencyVersion(); | ||
} | ||
} |