Skip to content

Commit cec83ae

Browse files
authored
Merge pull request #317 from PolyhedralDev/ver/6.0.1
Update addons when new minor and patch versions are available
2 parents 3fbb93d + 90a4834 commit cec83ae

File tree

4 files changed

+110
-5
lines changed

4 files changed

+110
-5
lines changed

build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
preRelease(true)
22

3-
versionProjects(":common:api", version("6.0.0"))
4-
versionProjects(":common:implementation", version("6.0.0"))
5-
versionProjects(":platforms", version("6.0.0"))
3+
versionProjects(":common:api", version("6.0.1"))
4+
versionProjects(":common:implementation", version("6.0.1"))
5+
versionProjects(":platforms", version("6.0.1"))
66

77

88
allprojects {

common/addons/structure-terrascript-loader/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
22

3-
version = version("1.0.0")
3+
version = version("1.0.1")
44

55
dependencies {
66
api("commons-io:commons-io:2.7")

common/api/src/main/java/com/dfsek/terra/api/util/generic/pair/Pair.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,49 @@
1111
import org.jetbrains.annotations.NotNull;
1212

1313
import java.util.Objects;
14+
import java.util.function.Consumer;
15+
import java.util.function.Function;
16+
import java.util.function.Predicate;
17+
import java.util.function.UnaryOperator;
1418

1519

1620
public final class Pair<L, R> {
1721
private static final Pair<?, ?> NULL = new Pair<>(null, null);
1822
private final L left;
1923
private final R right;
2024

25+
public static <L, R, T> Function<Pair<L, R>, Pair<T, R>> mapLeft(Function<L, T> function) {
26+
return pair -> of(function.apply(pair.left), pair.right);
27+
}
28+
29+
public static <L, R, T> Function<Pair<L, R>, Pair<L, T>> mapRight(Function<R, T> function) {
30+
return pair -> of(pair.left, function.apply(pair.right));
31+
}
32+
33+
public static <L> Predicate<Pair<L, ?>> testLeft(Predicate<L> predicate) {
34+
return pair -> predicate.test(pair.left);
35+
}
36+
37+
public static <R> Predicate<Pair<?, R>> testRight(Predicate<R> predicate) {
38+
return pair -> predicate.test(pair.right);
39+
}
40+
41+
public static <L> Consumer<Pair<L, ?>> consumeLeft(Consumer<L> consumer) {
42+
return pair -> consumer.accept(pair.left);
43+
}
44+
45+
public static <R> Consumer<Pair<?, R>> consumeRight(Consumer<R> consumer) {
46+
return pair -> consumer.accept(pair.right);
47+
}
48+
49+
public static <R> Function<Pair<?, R>, R> unwrapRight() {
50+
return pair -> pair.right;
51+
}
52+
53+
public static <L> Function<Pair<L, ?>, L> unwrapLeft() {
54+
return pair -> pair.left;
55+
}
56+
2157
private Pair(L left, R right) {
2258
this.left = left;
2359
this.right = right;
@@ -108,4 +144,9 @@ public boolean equals(Object obj) {
108144
return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right);
109145
}
110146
}
147+
148+
@Override
149+
public String toString() {
150+
return String.format("{%s,%s}", left, right);
151+
}
111152
}

common/implementation/base/src/main/java/com/dfsek/terra/AbstractPlatform.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
package com.dfsek.terra;
1919

2020
import com.dfsek.tectonic.api.TypeRegistry;
21+
22+
import com.dfsek.terra.api.util.generic.pair.Pair;
23+
2124
import org.apache.commons.io.FileUtils;
2225
import org.apache.commons.io.IOUtils;
2326
import org.jetbrains.annotations.NotNull;
@@ -33,12 +36,15 @@
3336
import java.io.UncheckedIOException;
3437
import java.net.URL;
3538
import java.nio.charset.StandardCharsets;
39+
import java.nio.file.Files;
3640
import java.nio.file.Path;
3741
import java.util.ArrayList;
3842
import java.util.Collections;
3943
import java.util.Comparator;
4044
import java.util.List;
4145
import java.util.Map;
46+
import java.util.Set;
47+
import java.util.stream.Collectors;
4248

4349
import com.dfsek.terra.addon.BootstrapAddonLoader;
4450
import com.dfsek.terra.addon.DependencySorter;
@@ -212,13 +218,71 @@ protected void dumpResources() {
212218
logger.info("No resources config found. Skipping resource dumping.");
213219
return;
214220
}
221+
222+
Path data = getDataFolder().toPath();
223+
224+
Path addonsPath = data.resolve("addons");
225+
Set<Pair<Path, String>> paths = Files
226+
.walk(addonsPath)
227+
.map(path -> Pair.of(path, data.relativize(path).toString()))
228+
229+
.map(Pair.mapRight(s -> {
230+
if(s.contains("+")) { // remove commit hash
231+
return s.substring(0, s.lastIndexOf('+'));
232+
}
233+
return s;
234+
}))
235+
236+
.filter(Pair.testRight(s -> s.contains("."))) // remove patch version
237+
.map(Pair.mapRight(s -> s.substring(0, s.lastIndexOf('.'))))
238+
239+
.filter(Pair.testRight(s -> s.contains("."))) // remove minor version
240+
.map(Pair.mapRight(s -> s.substring(0, s.lastIndexOf('.'))))
241+
242+
.collect(Collectors.toSet());
243+
244+
Set<String> pathsNoMajor = paths
245+
.stream()
246+
.filter(Pair.testRight(s -> s.contains(".")))
247+
.map(Pair.mapRight(s -> s.substring(0, s.lastIndexOf('.')))) // remove major version
248+
.map(Pair.unwrapRight())
249+
.collect(Collectors.toSet());
250+
251+
252+
// Terra-aaa-aaa-1.2.3-BETA+1e6af8923d.jar
215253
String resourceYaml = IOUtils.toString(resourcesConfig, StandardCharsets.UTF_8);
216254
Map<String, List<String>> resources = new Yaml().load(resourceYaml);
217255
resources.forEach((dir, entries) -> entries.forEach(entry -> {
218-
String resourcePath = String.format("%s/%s", dir, entry);
256+
String resourcePath = dir + File.separatorChar + entry;
219257
File resource = new File(getDataFolder(), resourcePath);
220258
if(resource.exists())
221259
return; // dont overwrite
260+
261+
paths
262+
.stream()
263+
.filter(Pair.testRight(resourcePath::startsWith))
264+
.forEach(Pair.consumeLeft(path -> {
265+
logger.info("Removing outdated resource {}, replacing with {}", path, resourcePath);
266+
try {
267+
Files.delete(path);
268+
} catch(IOException e) {
269+
throw new UncheckedIOException(e);
270+
}
271+
}));
272+
273+
if(pathsNoMajor
274+
.stream()
275+
.anyMatch(resourcePath::startsWith) && // if any share name
276+
paths
277+
.stream()
278+
.map(Pair.unwrapRight())
279+
.noneMatch(resourcePath::startsWith)) { // but dont share major version
280+
logger.warn(
281+
"Addon {} has a new major version available. It will not be automatically updated; you will need to ensure " +
282+
"compatibility and update manually.",
283+
resourcePath);
284+
}
285+
222286
logger.info("Dumping resource {}...", resource.getAbsolutePath());
223287
try {
224288
resource.getParentFile().mkdirs();

0 commit comments

Comments
 (0)