Skip to content

Commit

Permalink
Add MoreFiles.{delete,copy}Recursively
Browse files Browse the repository at this point in the history
This'll be useful for creating and clearing up up temporary directories
in the future.
  • Loading branch information
SquidDev committed Jun 11, 2024
1 parent f4e5e6a commit e261dbe
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 53 deletions.
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ fabric-unpick = "2.3.1"
asm = "9.6"
checkerFramework = "3.42.0"
gson = "2.10.1"
guava = "33.0.0-jre"
jetbrainsAnnotations = "24.1.0"
slf4j = "1.7.0"
vineflower = "1.9.3"
Expand All @@ -30,7 +29,6 @@ asm-util = { module = "org.ow2.asm:asm-util", version.ref = "asm" }

checkerFramework = { module = "org.checkerframework:checker-qual", version.ref = "checkerFramework" }
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
jetbrainsAnnotations = { module = "org.jetbrains:annotations", version.ref = "jetbrainsAnnotations" }
slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j" }
Expand Down
1 change: 0 additions & 1 deletion projects/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies {
implementation(libs.slf4j)

testCompileOnly(libs.jetbrainsAnnotations)
testImplementation(libs.guava)
testImplementation(libs.bundles.test)
testRuntimeOnly(libs.bundles.testRuntime)
testRuntimeOnly(libs.slf4j.simple)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ private static void copyEntries(FileSystemReference source, Path destination, Co
var sourceFile = source.getPath(file);
var outputFile = output.getPath(file);

var parent = outputFile.getParent();
if (parent != null) Files.createDirectories(parent);

MoreFiles.createParentDirectories(outputFile);
Files.copy(sourceFile, outputFile, StandardCopyOption.COPY_ATTRIBUTES);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.zip.ZipOutputStream;

Expand Down Expand Up @@ -252,4 +252,90 @@ public void close() {
tryDelete(path);
}
}


/**
* Copy files from {@code from} to {@code to}. Much like {@link Files#copy(Path, OutputStream)}, but recurses into
* directories.
*
* @param from The source files.
* @param to The destination files.
* @param options Additional copy options.
* @throws IOException If copying failed.
*/
public static void copyRecursively(Path from, Path to, CopyOption... options) throws IOException {
Files.walkFileTree(from, new Copier(from, to, options));
}

/**
* Copy files from one directory to another.
*/
private static final class Copier extends SimpleFileVisitor<Path> {
private final Path sourceDir;
private final Path targetDir;
private final CopyOption[] copyOptions;

private Copier(Path sourceDir, Path targetDir, CopyOption[] copyOptions) {
this.sourceDir = sourceDir;
this.targetDir = targetDir;
this.copyOptions = copyOptions;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
var relative = sourceDir.relativize(file);
var target = targetDir.getFileSystem() == relative.getFileSystem() ? targetDir.resolve(relative) : targetDir.resolve(relative.toString());
Files.copy(file, target, copyOptions);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attributes) throws IOException {
var newDir = targetDir.resolve(sourceDir.relativize(dir));
Files.createDirectories(newDir);
return FileVisitResult.CONTINUE;
}
}

/**
* Delete the provided file, and all files under it.
*
* @param path The file to delete.
* @throws IOException If deletion failed.
*/
public static void deleteRecursively(Path path) throws IOException {
Files.walkFileTree(path, Deleter.instance);
}

/**
* Delete all visited directories.
*/
private static final class Deleter extends SimpleFileVisitor<Path> {
private static final Deleter instance = new Deleter();

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
}


/**
* Create the parent directories to a file, if needed.
*
* @param path The path to create the parent directories of.
* @throws IOException If we cannot create the directory.
* @see Files#createDirectories(Path, FileAttribute[])
*/
public static void createParentDirectories(Path path) throws IOException {
var parent = path.getParent();
if (parent != null) Files.createDirectories(parent);
}
}
1 change: 0 additions & 1 deletion projects/plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ dependencies {
shadow(libs.bundles.unshadowedDeps)

testCompileOnly(libs.jetbrainsAnnotations)
testImplementation(libs.guava)
testImplementation(libs.bundles.test)
testRuntimeOnly(libs.bundles.testRuntime)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package cc.tweaked.vanillaextract;

import cc.tweaked.vanillaextract.core.minecraft.TransformedMinecraftProvider;
import com.google.common.io.MoreFiles;
import com.google.common.io.RecursiveDeleteOption;
import cc.tweaked.vanillaextract.core.util.MoreFiles;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -33,13 +32,13 @@ public static GradleProject create(String name) {
public void beforeEach(ExtensionContext context) throws IOException {
// Try to delete the cache directory if it exists
var projectDir = this.projectDir = Files.createTempDirectory("VanillaExtract-" + originalDir.getFileName().toString());
Copier.copy(originalDir, projectDir);
MoreFiles.copyRecursively(originalDir, projectDir);
}

@Override
public void afterEach(ExtensionContext context) throws IOException {
if (projectDir != null) {
MoreFiles.deleteRecursively(projectDir, RecursiveDeleteOption.ALLOW_INSECURE);
MoreFiles.deleteRecursively(projectDir);
projectDir = null;
}
}
Expand Down

0 comments on commit e261dbe

Please sign in to comment.