PT 173730396 - Further support for version validation
Added further support to generate warning messages for a java project from spring project metadata.
This commit is contained in:
@@ -12,7 +12,6 @@ package org.springframework.ide.vscode.commons.java;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -23,9 +22,14 @@ import org.slf4j.LoggerFactory;
|
||||
public class SpringProjectUtil {
|
||||
|
||||
public static final Logger log = LoggerFactory.getLogger(SpringProjectUtil.class);
|
||||
|
||||
private static final Pattern MAJOR_MINOR_VERSION = Pattern.compile("(\\d+\\.)(\\d+)");
|
||||
|
||||
private static final Pattern MAJOR_MINOR_VERSION = Pattern.compile("(0|[1-9]\\d*)\\.(0|[1-9]\\d*)");
|
||||
|
||||
// Pattern copied from https://semver.org/
|
||||
private static final Pattern VERSION = Pattern.compile("(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?");
|
||||
|
||||
private static final Pattern SPRING_NAME = Pattern.compile("([a-z]+)(-[a-z]+)*");
|
||||
|
||||
public static boolean isSpringProject(IJavaProject jp) {
|
||||
return hasSpecificLibraryOnClasspath(jp, "spring-core", true);
|
||||
}
|
||||
@@ -37,45 +41,6 @@ public class SpringProjectUtil {
|
||||
public static boolean hasBootActuators(IJavaProject jp) {
|
||||
return hasSpecificLibraryOnClasspath(jp, "spring-boot-actuator-", true);
|
||||
}
|
||||
|
||||
public static boolean hasSpecificLibraryOnClasspath(IJavaProject jp, String libraryNamePrefix, boolean onlyLibs) {
|
||||
try {
|
||||
IClasspath cp = jp.getClasspath();
|
||||
if (cp!=null) {
|
||||
return IClasspathUtil.getBinaryRoots(cp, (cpe) -> !cpe.isSystem()).stream().anyMatch(cpe -> isEntry(cpe, libraryNamePrefix, onlyLibs));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to determine whether '" + jp.getElementName() + "' is Spring Boot project", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static File getLibraryOnClasspath(IJavaProject jp, String libraryNamePrefix) {
|
||||
try {
|
||||
IClasspath cp = jp.getClasspath();
|
||||
if (cp!=null) {
|
||||
boolean onlyLibs = true;
|
||||
Optional<File> found = IClasspathUtil.getBinaryRoots(cp, (cpe) -> !cpe.isSystem()).stream().filter(cpe -> isEntry(cpe, libraryNamePrefix, onlyLibs)).findFirst();
|
||||
if (found.isPresent()) {
|
||||
return found.get();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to get library for project '" + jp.getElementName() + "' that start with prefix: " + libraryNamePrefix, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getMajMinVersion(IJavaProject jp, String libraryNamePrefix) {
|
||||
if (jp != null) {
|
||||
File libraryOnClasspath = getLibraryOnClasspath(jp, libraryNamePrefix);
|
||||
if (libraryOnClasspath != null) {
|
||||
String name = libraryOnClasspath.getName();
|
||||
return getMajMinVersion(name);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getMajMinVersion(String name) {
|
||||
Matcher matcher = MAJOR_MINOR_VERSION.matcher(name);
|
||||
@@ -86,6 +51,31 @@ public class SpringProjectUtil {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getVersion(String name) {
|
||||
Matcher matcher = VERSION.matcher(name);
|
||||
if (matcher.find()) {
|
||||
int start = matcher.start();
|
||||
int end = matcher.end();
|
||||
return name.substring(start, end);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param libName e.g. spring-boot-3.0.0.RELEASE.jar
|
||||
* @return "slug" portion: "spring-boot"
|
||||
*/
|
||||
public static String getProjectSlug(String libName) {
|
||||
Matcher matcher = SPRING_NAME.matcher(libName);
|
||||
if (matcher.find()) {
|
||||
int start = matcher.start();
|
||||
int end = matcher.end();
|
||||
return libName.substring(start, end);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<File> getLibrariesOnClasspath(IJavaProject jp, String libraryNamePrefix) {
|
||||
try {
|
||||
@@ -100,6 +90,18 @@ public class SpringProjectUtil {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean hasSpecificLibraryOnClasspath(IJavaProject jp, String libraryNamePrefix, boolean onlyLibs) {
|
||||
try {
|
||||
IClasspath cp = jp.getClasspath();
|
||||
if (cp!=null) {
|
||||
return IClasspathUtil.getBinaryRoots(cp, (cpe) -> !cpe.isSystem()).stream().anyMatch(cpe -> isEntry(cpe, libraryNamePrefix, onlyLibs));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to determine whether '" + jp.getElementName() + "' is Spring Boot project", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isEntry(File cpe, String libNamePrefix, boolean onlyLibs) {
|
||||
String name = cpe.getName();
|
||||
|
||||
Reference in New Issue
Block a user