only remove old resources if replacement exists

This commit is contained in:
dfsek
2022-06-07 11:19:53 -07:00
parent c811fd31b1
commit 1858bab210
@@ -18,30 +18,6 @@
package com.dfsek.terra; package com.dfsek.terra;
import com.dfsek.tectonic.api.TypeRegistry; import com.dfsek.tectonic.api.TypeRegistry;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.dfsek.terra.addon.BootstrapAddonLoader; import com.dfsek.terra.addon.BootstrapAddonLoader;
import com.dfsek.terra.addon.DependencySorter; import com.dfsek.terra.addon.DependencySorter;
@@ -73,6 +49,21 @@ import com.dfsek.terra.registry.LockedRegistryImpl;
import com.dfsek.terra.registry.OpenRegistryImpl; import com.dfsek.terra.registry.OpenRegistryImpl;
import com.dfsek.terra.registry.master.ConfigRegistry; import com.dfsek.terra.registry.master.ConfigRegistry;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
/** /**
* Skeleton implementation of {@link Platform} * Skeleton implementation of {@link Platform}
@@ -256,6 +247,13 @@ public abstract class AbstractPlatform implements Platform {
if(resource.exists()) if(resource.exists())
return; // dont overwrite return; // dont overwrite
try(InputStream is = getClass().getResourceAsStream("/" + resourcePath);
OutputStream os = new FileOutputStream(resource)) {
if(is == null) {
logger.error("Resource {} doesn't exist on the classpath!", resourcePath);
return;
}
paths paths
.stream() .stream()
.filter(Pair.testRight(resourcePath::startsWith)) .filter(Pair.testRight(resourcePath::startsWith))
@@ -276,25 +274,16 @@ public abstract class AbstractPlatform implements Platform {
.map(Pair.unwrapRight()) .map(Pair.unwrapRight())
.noneMatch(resourcePath::startsWith)) { // but dont share major version .noneMatch(resourcePath::startsWith)) { // but dont share major version
logger.warn( logger.warn(
"Addon {} has a new major version available. It will not be automatically updated; you will need to ensure " + "Addon {} has a new major version available. It will not be automatically updated; you will need to " +
"ensure " +
"compatibility and update manually.", "compatibility and update manually.",
resourcePath); resourcePath);
} }
logger.info("Dumping resource {}...", resource.getAbsolutePath()); logger.info("Dumping resource {}...", resource.getAbsolutePath());
try {
resource.getParentFile().mkdirs(); resource.getParentFile().mkdirs();
resource.createNewFile(); resource.createNewFile();
} catch(IOException e) {
throw new UncheckedIOException(e);
}
logger.debug("Copying resource {}", resourcePath);
try(InputStream is = getClass().getResourceAsStream("/" + resourcePath);
OutputStream os = new FileOutputStream(resource)) {
if(is == null) {
logger.error("Resource {} doesn't exist on the classpath!", resourcePath);
return;
}
IOUtils.copy(is, os); IOUtils.copy(is, os);
} catch(IOException e) { } catch(IOException e) {
throw new UncheckedIOException(e); throw new UncheckedIOException(e);