Skip to content

Commit 300ed25

Browse files
RollczivLuckyyyJakubk15
authored
GH-792 Fix updating of libraries. (dependency loader) (#798)
* 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]>
1 parent 44823f0 commit 300ed25

File tree

4 files changed

+95
-19
lines changed

4 files changed

+95
-19
lines changed

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

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
import com.eternalcode.core.loader.resource.ResourceLocator;
66
import java.util.Objects;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
79

810
public class Dependency {
911

12+
private static final Pattern VERSION_PATTERN = Pattern.compile("(?<major>[0-9]+)\\.(?<minor>[0-9]+)\\.?(?<patch>[0-9]?)(-(?<label>[-+.a-zA-Z0-9]+))?");
13+
1014
private static final String JAR_MAVEN_FORMAT = "%s/%s/%s/%s/%s-%s.jar";
1115
private static final String JAR_MAVEN_FORMAT_WITH_CLASSIFIER = "%s/%s/%s/%s/%s-%s-%s.jar";
1216
private static final String POM_XML_FORMAT = "%s/%s/%s/%s/%s-%s.pom";
@@ -78,27 +82,85 @@ public String getVersion() {
7882
return this.version;
7983
}
8084

85+
public boolean isNewerThan(Dependency dependency) {
86+
int thisMajor = this.getMajorVersion();
87+
int dependencyMajor = dependency.getMajorVersion();
88+
89+
if (thisMajor > dependencyMajor) {
90+
return true;
91+
}
92+
93+
int thisMinor = this.getMinorVersion();
94+
int dependencyMinor = dependency.getMinorVersion();
95+
96+
if (thisMinor > dependencyMinor) {
97+
return true;
98+
}
99+
100+
int thisPatch = this.getPatchVersion();
101+
int dependencyPatch = dependency.getPatchVersion();
102+
103+
if (thisPatch > dependencyPatch) {
104+
return true;
105+
}
106+
107+
return false;
108+
}
109+
110+
public int getMajorVersion() {
111+
return this.getSemanticVersionPart("major");
112+
}
113+
114+
public int getMinorVersion() {
115+
return this.getSemanticVersionPart("minor");
116+
}
117+
118+
public int getPatchVersion() {
119+
return this.getSemanticVersionPart("patch");
120+
}
121+
122+
public String getLabelVersion() {
123+
Matcher matcher = VERSION_PATTERN.matcher(this.version);
124+
125+
if (!matcher.matches()) {
126+
throw new IllegalArgumentException("Invalid version format: " + this.version + " for dependency: " + this);
127+
}
128+
129+
return matcher.group("label");
130+
}
131+
132+
private int getSemanticVersionPart(String name) {
133+
Matcher matcher = VERSION_PATTERN.matcher(this.version);
134+
135+
if (!matcher.matches()) {
136+
throw new IllegalArgumentException("Invalid version format: " + this.version + " for dependency: " + this);
137+
}
138+
139+
String versionNumber = matcher.group(name);
140+
141+
if (versionNumber.isEmpty()) {
142+
return 0;
143+
}
144+
145+
return Integer.parseInt(versionNumber);
146+
}
147+
81148
@Override
82149
public String toString() {
83150
return this.getGroupArtifactId() + ":" + this.version;
84151
}
85152

86153
@Override
87154
public boolean equals(Object o) {
88-
if (this == o) {
89-
return true;
90-
}
91-
92-
if (!(o instanceof Dependency that)) {
93-
return false;
94-
}
95-
96-
return Objects.equals(this.groupId, that.groupId) && Objects.equals(this.artifactId, that.artifactId);
155+
if (this == o) return true;
156+
if (o == null || getClass() != o.getClass()) return false;
157+
Dependency that = (Dependency) o;
158+
return Objects.equals(this.groupId, that.groupId) && Objects.equals(this.artifactId, that.artifactId) && Objects.equals(this.version, that.version);
97159
}
98160

99161
@Override
100162
public int hashCode() {
101-
return Objects.hash(this.groupId, this.artifactId);
163+
return Objects.hash(this.groupId, this.artifactId, this.version);
102164
}
103165

104166
public static Dependency of(String groupId, String artifactId, String version) {
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
package com.eternalcode.core.loader.dependency;
22

33
import java.util.Collection;
4-
import java.util.LinkedHashSet;
4+
import java.util.Collections;
5+
import java.util.LinkedHashMap;
56

67
public class DependencyCollector {
78

8-
private final LinkedHashSet<Dependency> fullScannedDependencies = new LinkedHashSet<>();
9+
private final LinkedHashMap<String, Dependency> fullScannedDependencies = new LinkedHashMap<>();
910

1011
public boolean hasScannedDependency(Dependency dependency) {
11-
return this.fullScannedDependencies.contains(dependency);
12+
return this.fullScannedDependencies.containsKey(dependency.getGroupArtifactId());
1213
}
1314

1415
public void scannedDependency(Dependency dependency) {
15-
this.fullScannedDependencies.add(dependency);
16+
Dependency current = this.fullScannedDependencies.get(dependency.getGroupArtifactId());
17+
18+
if (current == null) {
19+
this.fullScannedDependencies.put(dependency.getGroupArtifactId(), dependency);
20+
return;
21+
}
22+
23+
if (dependency.isNewerThan(current)) {
24+
this.fullScannedDependencies.put(dependency.getGroupArtifactId(), dependency);
25+
}
1626
}
1727

1828
public void scannedDependencies(Collection<Dependency> dependencies) {
19-
this.fullScannedDependencies.addAll(dependencies);
29+
for (Dependency dependency : dependencies) {
30+
this.scannedDependency(dependency);
31+
}
2032
}
2133

22-
public LinkedHashSet<Dependency> scannedDependencies() {
23-
return this.fullScannedDependencies;
34+
public Collection<Dependency> scannedDependencies() {
35+
return Collections.unmodifiableCollection(this.fullScannedDependencies.values());
2436
}
2537

2638
}

eternalcore-plugin/src/main/java/com/eternalcode/core/loader/dependency/DependencyLoadResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.eternalcode.core.loader.classloader.IsolatedClassLoader;
44

55
import java.nio.file.Path;
6-
import java.util.LinkedHashSet;
6+
import java.util.Collection;
77
import java.util.List;
88

9-
public record DependencyLoadResult(IsolatedClassLoader loader, LinkedHashSet<Dependency> dependencies, List<Path> paths) {
9+
public record DependencyLoadResult(IsolatedClassLoader loader, Collection<Dependency> dependencies, List<Path> paths) {
1010
}

eternalcore-plugin/src/main/java/com/eternalcode/core/loader/dependency/DependencyLoaderImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public DependencyLoadResult load(IsolatedClassLoader loader, List<Dependency> de
7373
Path loaded = this.loaded.get(dependency);
7474

7575
if (loaded != null) {
76+
// 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?)
7677
loader.addPath(loaded);
7778
paths.add(loaded);
7879
continue;
@@ -87,6 +88,7 @@ public DependencyLoadResult load(IsolatedClassLoader loader, List<Dependency> de
8788
continue;
8889
}
8990

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

9294
this.loaded.put(dependency, relocatedDependency);

0 commit comments

Comments
 (0)