Skip to content

Commit 0080777

Browse files
committed
Track relocations globally instead of per-dependency
1 parent 67843a6 commit 0080777

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.eternalcode.core.loader.relocation;
22

3-
import com.eternalcode.core.loader.dependency.Dependency;
43
import com.eternalcode.core.loader.repository.LocalRepository;
5-
import com.google.gson.Gson;
6-
import com.google.gson.reflect.TypeToken;
74
import java.io.File;
85
import java.nio.charset.StandardCharsets;
96
import java.nio.file.Files;
@@ -15,22 +12,21 @@ public class RelocationCacheResolver {
1512

1613
private static final String RELOCATIONS_FILE = "relocations.txt";
1714

18-
private final LocalRepository localRepository;
15+
private final File relocationsFile;
1916

2017
public RelocationCacheResolver(LocalRepository localRepository) {
21-
this.localRepository = localRepository;
18+
this.relocationsFile = localRepository.getRepositoryFolder().resolve(RELOCATIONS_FILE).toFile();
2219
}
2320

24-
public boolean shouldForceRelocate(Dependency dependency, List<Relocation> relocations) {
25-
return this.getRawSavedRelocations(dependency)
21+
public boolean shouldForceRelocate(List<Relocation> relocations) {
22+
return this.getSavedRelocations()
2623
.map(savedRelocations -> !savedRelocations.equals(toRawRelocations(relocations)))
2724
.orElse(true);
2825
}
2926

30-
public void markAsRelocated(Dependency dependency, List<Relocation> relocations) {
27+
public void markAsRelocated(List<Relocation> relocations) {
3128
try {
32-
File relocationsCache = dependency.toResource(localRepository, RELOCATIONS_FILE).toFile();
33-
Files.writeString(relocationsCache.toPath(), toRawRelocations(relocations), StandardCharsets.UTF_8);
29+
Files.writeString(relocationsFile.toPath(), toRawRelocations(relocations), StandardCharsets.UTF_8);
3430
} catch (Exception exception) {
3531
throw new RuntimeException("Failed to save relocations", exception);
3632
}
@@ -42,16 +38,14 @@ private static String toRawRelocations(List<Relocation> relocations) {
4238
.collect(Collectors.joining("\n"));
4339
}
4440

45-
private Optional<String> getRawSavedRelocations(Dependency dependency) {
41+
private Optional<String> getSavedRelocations() {
4642
try {
47-
File relocationsCache = dependency.toResource(localRepository, RELOCATIONS_FILE).toFile();
48-
if (!relocationsCache.exists()) {
43+
if (!relocationsFile.exists()) {
4944
return Optional.empty();
5045
}
51-
return Optional.of(Files.readString(relocationsCache.toPath()));
46+
return Optional.of(Files.readString(relocationsFile.toPath(), StandardCharsets.UTF_8));
5247
} catch (Exception exception) {
5348
return Optional.empty();
5449
}
5550
}
56-
5751
}

eternalcore-plugin/src/main/java/com/eternalcode/core/loader/relocation/RelocationHandler.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.eternalcode.core.loader.dependency.DependencyException;
3131
import com.eternalcode.core.loader.dependency.DependencyLoadResult;
3232
import com.eternalcode.core.loader.dependency.DependencyLoader;
33-
3433
import com.eternalcode.core.loader.repository.Repository;
3534
import java.io.File;
3635
import java.io.IOException;
@@ -76,14 +75,14 @@ public Path relocateDependency(Repository localRepository, Path dependencyPath,
7675

7776
Path relocatedJar = dependency.toMavenJar(localRepository, "relocated").toPath();
7877

79-
if (Files.exists(relocatedJar) && !this.cacheResolver.shouldForceRelocate(dependency, relocations)) {
78+
if (Files.exists(relocatedJar) && !this.cacheResolver.shouldForceRelocate(relocations)) {
8079
return relocatedJar;
8180
}
8281

83-
return this.relocate(dependency, dependencyPath, relocatedJar, relocations);
82+
return this.relocate(dependencyPath, relocatedJar, relocations);
8483
}
8584

86-
private Path relocate(Dependency dependency, Path input, Path output, List<Relocation> relocations) {
85+
private Path relocate(Path input, Path output, List<Relocation> relocations) {
8786
Map<String, String> mappings = new HashMap<>();
8887

8988
for (Relocation relocation : relocations) {
@@ -96,7 +95,7 @@ private Path relocate(Dependency dependency, Path input, Path output, List<Reloc
9695
Files.createFile(output);
9796
Object relocator = this.jarRelocatorConstructor.newInstance(input.toFile(), output.toFile(), mappings);
9897
this.jarRelocatorRunMethod.invoke(relocator);
99-
this.cacheResolver.markAsRelocated(dependency, relocations);
98+
this.cacheResolver.markAsRelocated(relocations);
10099

101100
return output;
102101
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | IOException exception) {

eternalcore-plugin/src/main/java/com/eternalcode/core/loader/repository/LocalRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
public class LocalRepository extends Repository {
77

8+
private final Path repositoryFolder;
9+
810
public LocalRepository(Path repositoryFolder) {
911
super(assertFolder(repositoryFolder));
12+
this.repositoryFolder = repositoryFolder;
1013
}
1114

1215
private static String assertFolder(Path repositoryFolder) {
@@ -18,4 +21,7 @@ private static String assertFolder(Path repositoryFolder) {
1821
}
1922
}
2023

24+
public Path getRepositoryFolder() {
25+
return repositoryFolder;
26+
}
2127
}

0 commit comments

Comments
 (0)