Skip to content

Commit

Permalink
Cache dependency versions
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Oct 4, 2023
1 parent 55d259f commit 63cf7c5
Show file tree
Hide file tree
Showing 23 changed files with 212 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.Version;
import org.springframework.ide.vscode.commons.protocol.java.Classpath;
import org.springframework.ide.vscode.commons.protocol.java.Classpath.CPE;

public class SpringProjectUtil {

public static final String SPRING_BOOT = "spring-boot";

// Pattern copied from https://semver.org/
private static final String VERSION_PATTERN_STR = "(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 String GENERATION_VERSION_STR = "([0-9]+)";

public static final Logger log = LoggerFactory.getLogger(SpringProjectUtil.class);
Expand Down Expand Up @@ -114,10 +113,9 @@ private static boolean isEntry(File cpe, String libNamePrefix, boolean onlyLibs)

public static Version getDependencyVersion(IJavaProject jp, String dependency) {
try {
for (File f : IClasspathUtil.getBinaryRoots(jp.getClasspath(), (cpe) -> !cpe.isSystem())) {
Version version = getDependencyVersion(f.getName(), dependency);
if (version != null) {
return version;
for (CPE cpe : jp.getClasspath().getClasspathEntries()) {
if (Classpath.isBinary(cpe) && !cpe.isSystem() && new File(cpe.getPath()).getName().startsWith(dependency)) {
return cpe.getVersion();
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -156,10 +154,9 @@ public static boolean hasDependencyStartingWith(IJavaProject jp, String dependen

public static Version getSpringBootVersion(IJavaProject jp) {
try {
for (File f : IClasspathUtil.getBinaryRoots(jp.getClasspath(), (cpe) -> !cpe.isSystem())) {
Version version = getDependencyVersion(f.getName(), SPRING_BOOT);
if (version != null) {
return version;
for (CPE cpe : jp.getClasspath().getClasspathEntries()) {
if (Classpath.isBinary(cpe) && !cpe.isSystem() && new File(cpe.getPath()).getName().startsWith(SPRING_BOOT)) {
return cpe.getVersion();
}
}
} catch (Exception e) {
Expand All @@ -168,33 +165,6 @@ public static Version getSpringBootVersion(IJavaProject jp) {
return null;
}

public static Version getDependencyVersion(String fileName, String dependency) {
if (fileName.startsWith(dependency)) {
StringBuilder sb = new StringBuilder();
sb.append('^');
sb.append(dependency);
sb.append('-');
sb.append(VERSION_PATTERN_STR);
sb.append("\\.jar$");
Pattern pattern = Pattern.compile(sb.toString());

Matcher matcher = pattern.matcher(fileName);
if (matcher.find() && matcher.groupCount() > 5) {
String major = matcher.group(1);
String minor = matcher.group(2);
String patch = matcher.group(3);
String qualifier = matcher.group(5);
return new Version(
Integer.parseInt(major),
Integer.parseInt(minor),
Integer.parseInt(patch),
qualifier
);
}
}
return null;
}

public static Predicate<IJavaProject> springBootVersionGreaterOrEqual(int major, int minor, int patch) {
return project -> {
Version version = getDependencyVersion(project, SPRING_BOOT);
Expand All @@ -216,39 +186,4 @@ public static Predicate<IJavaProject> springBootVersionGreaterOrEqual(int major,
};
}

public static Version getVersion(String version) {
Pattern pattern = Pattern.compile(VERSION_PATTERN_STR);
Matcher matcher = pattern.matcher(version);
if (matcher.find() && matcher.groupCount() > 4) {
String major = matcher.group(1);
String minor = matcher.group(2);
String patch = matcher.group(3);
String qualifier = matcher.group(5);
return new Version(
Integer.parseInt(major),
Integer.parseInt(minor),
Integer.parseInt(patch),
qualifier
);
} else {
String[] tokens = version.split("\\.");
if (tokens.length <= 3) {
if (tokens.length >= 1) {
int major = Integer.parseInt(tokens[0]);
if (tokens.length >= 2) {
int minor = Integer.parseInt(tokens[1]);
if (tokens.length == 3) {
int patch = Integer.parseInt(tokens[2]);
return new Version(major, minor, patch, null);
} else {
return new Version(major, minor, 0, null);
}
} else {
return new Version(major, 0, 0, null);
}
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
* Contributors:
* VMware, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.java;
package org.springframework.ide.vscode.commons;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Version implements Comparable<Version> {

private static final Pattern VERSION_PATTERN = 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 int major;
private int minor;
private int patch;
Expand Down Expand Up @@ -95,5 +99,41 @@ public boolean equals(Object obj) {
return major == other.major && minor == other.minor && patch == other.patch
&& Objects.equals(qualifier, other.qualifier);
}

public static Version parse(String version) {
Matcher matcher = VERSION_PATTERN.matcher(version);
if (matcher.find() && matcher.groupCount() > 4) {
String major = matcher.group(1);
String minor = matcher.group(2);
String patch = matcher.group(3);
String qualifier = matcher.group(5);
return new Version(
Integer.parseInt(major),
Integer.parseInt(minor),
Integer.parseInt(patch),
qualifier
);
} else {
String[] tokens = version.split("\\.");
if (tokens.length <= 3) {
if (tokens.length >= 1) {
int major = Integer.parseInt(tokens[0]);
if (tokens.length >= 2) {
int minor = Integer.parseInt(tokens[1]);
if (tokens.length == 3) {
int patch = Integer.parseInt(tokens[2]);
return new Version(major, minor, patch, null);
} else {
return new Version(major, minor, 0, null);
}
} else {
return new Version(major, 0, 0, null);
}
}
}
}
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.ide.vscode.commons.Version;

public class Classpath {

// Pattern copied from https://semver.org/
private static final Pattern VERSION_PATTERN = 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-]+)*))?\\.jar$");

public static final String ENTRY_KIND_SOURCE = "source";
public static final String ENTRY_KIND_BINARY = "binary";
Expand Down Expand Up @@ -64,6 +71,8 @@ public static class CPE {
private boolean isJavaContent = false;

private Map<String, String> extra;

transient private Version version;

public CPE() {}

Expand Down Expand Up @@ -205,6 +214,15 @@ public boolean equals(Object obj) {
&& Objects.equals(kind, other.kind) && Objects.equals(outputFolder, other.outputFolder)
&& Objects.equals(path, other.path) && Objects.equals(sourceContainerUrl, other.sourceContainerUrl);
}

public Version getVersion() {
if (version == null) {
if (getKind() == ENTRY_KIND_BINARY) {
version = getDependencyVersion(new File(getPath()).getName());
}
}
return version;
}

}

Expand Down Expand Up @@ -232,5 +250,16 @@ public static boolean isProjectTestJavaSource(CPE cpe) {
return isProjectJavaSource(cpe) && cpe.isTest();
}

static Version getDependencyVersion(String fileName) {
Matcher matcher = VERSION_PATTERN.matcher(fileName);
if (matcher.find() && matcher.groupCount() > 5) {
String major = matcher.group(1);
String minor = matcher.group(2);
String patch = matcher.group(3);
String qualifier = matcher.group(5);
return new Version(Integer.parseInt(major), Integer.parseInt(minor), Integer.parseInt(patch), qualifier);
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*******************************************************************************
* Copyright (c) 2023 VMware, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* VMware, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

public class VersionTests {

@Test
void testVersionCalculation1() throws Exception {
Version version = Version.parse("2.7.5");
assertEquals(2, version.getMajor());
assertEquals(7, version.getMinor());
assertEquals(5, version.getPatch());
assertNull(version.getQualifier());

version = Version.parse("3.0.0-SNAPSHOT");
assertEquals(3, version.getMajor());
assertEquals(0, version.getMinor());
assertEquals(0, version.getPatch());
assertEquals(version.getQualifier(), "SNAPSHOT");


version = Version.parse("2.6.14-RC2");
assertEquals(2, version.getMajor());
assertEquals(6, version.getMinor());
assertEquals(14, version.getPatch());
assertEquals(version.getQualifier(), "RC2");
}

@Test
void testVersionCalculation2() throws Exception {
Version version = Version.parse("2.7");
assertEquals(2, version.getMajor());
assertEquals(7, version.getMinor());
assertEquals(0, version.getPatch());
assertNull(version.getQualifier());

version = Version.parse("2");
assertEquals(2, version.getMajor());
assertEquals(0, version.getMinor());
assertEquals(0, version.getPatch());
assertNull(version.getQualifier());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************************
* Copyright (c) 2023 VMware, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* VMware, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.protocol.java;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;
import org.springframework.ide.vscode.commons.Version;

public class ClasspathTests {

@Test
void testDependencyVersionCalculation2() throws Exception {
Version version = Classpath.getDependencyVersion("spring-boot-1.2.3.jar");
assertEquals(1, version.getMajor(), 1);
assertEquals(2, version.getMinor(), 2);
assertEquals(3, version.getPatch());
assertNull(version.getQualifier());

version = Classpath.getDependencyVersion("spring-boot-1.2.3-RELEASE.jar");
assertEquals(version.getMajor(), 1);
assertEquals(version.getMinor(), 2);
assertEquals(version.getPatch(), 3);
assertEquals(version.getQualifier(), "RELEASE");

version = Classpath.getDependencyVersion("spring-boot-1.2.3.RELEASE.jar");
assertEquals(1, version.getMajor(), 1);
assertEquals(2, version.getMinor(), 2);
assertEquals(3, version.getPatch());
assertEquals("RELEASE", version.getQualifier());

version = Classpath.getDependencyVersion("spring-boot-1.2.3.BUILD-SNAPSHOT.jar");
assertEquals(1, version.getMajor(), 1);
assertEquals(2, version.getMinor(), 2);
assertEquals(3, version.getPatch());
assertEquals("BUILD-SNAPSHOT", version.getQualifier());

version = Classpath.getDependencyVersion("spring-boot-actuator-1.2.3.BUILD-SNAPSHOT.jar");
assertEquals(1, version.getMajor(), 1);
assertEquals(2, version.getMinor(), 2);
assertEquals(3, version.getPatch());
assertEquals("BUILD-SNAPSHOT", version.getQualifier());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import org.springframework.ide.vscode.boot.java.beans.FeignClientBeanSymbolAddOnInformation;
import org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation;
import org.springframework.ide.vscode.boot.java.handlers.SymbolAddOnInformation;
import org.springframework.ide.vscode.commons.Version;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.java.SpringProjectUtil;
import org.springframework.ide.vscode.commons.java.Version;
import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixRegistry;
import org.springframework.ide.vscode.commons.languageserver.reconcile.IProblemCollector;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import org.eclipse.jdt.core.dom.SimpleType;
import org.openrewrite.java.spring.security5.AuthorizeHttpRequests;
import org.springframework.ide.vscode.boot.java.Boot2JavaProblemType;
import org.springframework.ide.vscode.commons.Version;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.java.SpringProjectUtil;
import org.springframework.ide.vscode.commons.java.Version;
import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixRegistry;
import org.springframework.ide.vscode.commons.languageserver.reconcile.IProblemCollector;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.Boot2JavaProblemType;
import org.springframework.ide.vscode.boot.java.rewrite.RewriteRefactorings;
import org.springframework.ide.vscode.commons.Version;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.java.SpringProjectUtil;
import org.springframework.ide.vscode.commons.java.Version;
import org.springframework.ide.vscode.commons.languageserver.quickfix.Quickfix.QuickfixData;
import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixRegistry;
import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

import org.openrewrite.java.spring.boot2.HttpSecurityLambdaDsl;
import org.springframework.ide.vscode.boot.java.Boot2JavaProblemType;
import org.springframework.ide.vscode.commons.Version;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.java.SpringProjectUtil;
import org.springframework.ide.vscode.commons.java.Version;
import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixRegistry;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemType;

Expand Down
Loading

0 comments on commit 63cf7c5

Please sign in to comment.