Skip to content

Commit

Permalink
GH-792 Fix updating of libraries. (dependency loader) (#798)
Browse files Browse the repository at this point in the history
* Fix updating of libraries. (dependency loader)

* Update eternalcore-plugin/src/main/java/com/eternalcode/core/loader/dependency/Dependency.java

Co-authored-by: Jakubk15 <[email protected]>

* Update eternalcore-plugin/src/main/java/com/eternalcode/core/loader/dependency/Dependency.java

Co-authored-by: Jakubk15 <[email protected]>

---------

Co-authored-by: Martin Sulikowski <[email protected]>
Co-authored-by: Jakubk15 <[email protected]>
  • Loading branch information
3 people authored Jun 22, 2024
1 parent 44823f0 commit 300ed25
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

import com.eternalcode.core.loader.resource.ResourceLocator;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Dependency {

private static final Pattern VERSION_PATTERN = Pattern.compile("(?<major>[0-9]+)\\.(?<minor>[0-9]+)\\.?(?<patch>[0-9]?)(-(?<label>[-+.a-zA-Z0-9]+))?");

private static final String JAR_MAVEN_FORMAT = "%s/%s/%s/%s/%s-%s.jar";
private static final String JAR_MAVEN_FORMAT_WITH_CLASSIFIER = "%s/%s/%s/%s/%s-%s-%s.jar";
private static final String POM_XML_FORMAT = "%s/%s/%s/%s/%s-%s.pom";
Expand Down Expand Up @@ -78,27 +82,85 @@ public String getVersion() {
return this.version;
}

public boolean isNewerThan(Dependency dependency) {
int thisMajor = this.getMajorVersion();
int dependencyMajor = dependency.getMajorVersion();

if (thisMajor > dependencyMajor) {
return true;
}

int thisMinor = this.getMinorVersion();
int dependencyMinor = dependency.getMinorVersion();

if (thisMinor > dependencyMinor) {
return true;
}

int thisPatch = this.getPatchVersion();
int dependencyPatch = dependency.getPatchVersion();

if (thisPatch > dependencyPatch) {
return true;
}

return false;
}

public int getMajorVersion() {
return this.getSemanticVersionPart("major");
}

public int getMinorVersion() {
return this.getSemanticVersionPart("minor");
}

public int getPatchVersion() {
return this.getSemanticVersionPart("patch");
}

public String getLabelVersion() {
Matcher matcher = VERSION_PATTERN.matcher(this.version);

if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid version format: " + this.version + " for dependency: " + this);
}

return matcher.group("label");
}

private int getSemanticVersionPart(String name) {
Matcher matcher = VERSION_PATTERN.matcher(this.version);

if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid version format: " + this.version + " for dependency: " + this);
}

String versionNumber = matcher.group(name);

if (versionNumber.isEmpty()) {
return 0;
}

return Integer.parseInt(versionNumber);
}

@Override
public String toString() {
return this.getGroupArtifactId() + ":" + this.version;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof Dependency that)) {
return false;
}

return Objects.equals(this.groupId, that.groupId) && Objects.equals(this.artifactId, that.artifactId);
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dependency that = (Dependency) o;
return Objects.equals(this.groupId, that.groupId) && Objects.equals(this.artifactId, that.artifactId) && Objects.equals(this.version, that.version);
}

@Override
public int hashCode() {
return Objects.hash(this.groupId, this.artifactId);
return Objects.hash(this.groupId, this.artifactId, this.version);
}

public static Dependency of(String groupId, String artifactId, String version) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
package com.eternalcode.core.loader.dependency;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Collections;
import java.util.LinkedHashMap;

public class DependencyCollector {

private final LinkedHashSet<Dependency> fullScannedDependencies = new LinkedHashSet<>();
private final LinkedHashMap<String, Dependency> fullScannedDependencies = new LinkedHashMap<>();

public boolean hasScannedDependency(Dependency dependency) {
return this.fullScannedDependencies.contains(dependency);
return this.fullScannedDependencies.containsKey(dependency.getGroupArtifactId());
}

public void scannedDependency(Dependency dependency) {
this.fullScannedDependencies.add(dependency);
Dependency current = this.fullScannedDependencies.get(dependency.getGroupArtifactId());

if (current == null) {
this.fullScannedDependencies.put(dependency.getGroupArtifactId(), dependency);
return;
}

if (dependency.isNewerThan(current)) {
this.fullScannedDependencies.put(dependency.getGroupArtifactId(), dependency);
}
}

public void scannedDependencies(Collection<Dependency> dependencies) {
this.fullScannedDependencies.addAll(dependencies);
for (Dependency dependency : dependencies) {
this.scannedDependency(dependency);
}
}

public LinkedHashSet<Dependency> scannedDependencies() {
return this.fullScannedDependencies;
public Collection<Dependency> scannedDependencies() {
return Collections.unmodifiableCollection(this.fullScannedDependencies.values());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.eternalcode.core.loader.classloader.IsolatedClassLoader;

import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.Collection;
import java.util.List;

public record DependencyLoadResult(IsolatedClassLoader loader, LinkedHashSet<Dependency> dependencies, List<Path> paths) {
public record DependencyLoadResult(IsolatedClassLoader loader, Collection<Dependency> dependencies, List<Path> paths) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public DependencyLoadResult load(IsolatedClassLoader loader, List<Dependency> de
Path loaded = this.loaded.get(dependency);

if (loaded != null) {
// TODO: jeśli już wcześniej pobrano zależność to można zweryfikować czy relokacja jest poprawna (może jakiś plik z informacjami o relokacjach?)
loader.addPath(loaded);
paths.add(loaded);
continue;
Expand All @@ -87,6 +88,7 @@ public DependencyLoadResult load(IsolatedClassLoader loader, List<Dependency> de
continue;
}

// TODO: to jest trochę blocking można to zrobić w parallel streamie
Path relocatedDependency = this.relocationHandler.relocateDependency(downloadedDependencyPath, dependency, relocations);

this.loaded.put(dependency, relocatedDependency);
Expand Down

0 comments on commit 300ed25

Please sign in to comment.