Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Dec 29, 2023
1 parent bf9c2e3 commit e825301
Show file tree
Hide file tree
Showing 25 changed files with 690 additions and 330 deletions.
32 changes: 32 additions & 0 deletions api/src/main/java/net/neoforged/jst/api/FileEntries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.neoforged.jst.api;

import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public final class FileEntries {
private FileEntries() {
}

/**
* Creates a file entry for a given NIO path.
* Since file entries need to know their path relative to the source root, the source root has to be
* given as an additional parameter.
*/
public static FileEntry ofPath(Path sourceRoot, Path path) {
if (path.equals(sourceRoot)) {
throw new IllegalStateException("path must not be the source root itself, since this results in an empty relative path");
}
if (!path.startsWith(sourceRoot)) {
throw new IllegalStateException("path must be a child of sourceRoot");
}
return new PathFileEntry(sourceRoot, path);
}

/**
* Creates a file entry for an existing zip entry. Will source the content from the given zip file.
*/
public static FileEntry ofZipEntry(ZipFile zipFile, ZipEntry zipEntry) {
return new ZipFileEntry(zipFile, zipEntry);
}
}
3 changes: 2 additions & 1 deletion api/src/main/java/net/neoforged/jst/api/FileEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.attribute.FileTime;
import java.util.Locale;

public interface FileEntry {
Expand All @@ -18,7 +19,7 @@ public interface FileEntry {
/**
* @return Millis since epoch denoting when the file was last modified. 0 for directories.
*/
long lastModified();
FileTime lastModified();

/**
* @return An input stream to read this content.
Expand Down
7 changes: 6 additions & 1 deletion api/src/main/java/net/neoforged/jst/api/FileSink.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.attribute.FileTime;

public interface FileSink extends AutoCloseable {
@Override
default void close() throws IOException {
}

boolean canHaveMultipleEntries();

boolean isOrdered();

void put(FileEntry entry, byte[] content) throws IOException;
void putDirectory(String relativePath) throws IOException;

void putFile(String relativePath, FileTime lastModified, byte[] content) throws IOException;
}
2 changes: 2 additions & 0 deletions api/src/main/java/net/neoforged/jst/api/FileSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface FileSource extends AutoCloseable {

Stream<FileEntry> streamEntries() throws IOException;

boolean canHaveMultipleEntries();

boolean isOrdered();

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package net.neoforged.jst.cli.io;

import net.neoforged.jst.api.FileEntry;
package net.neoforged.jst.api;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;

final class PathEntry implements FileEntry {
private final Path relativeTo;
final class PathFileEntry implements FileEntry {
private final Path path;
private final String relativePath;
private final boolean directory;
private final long lastModified;
private final FileTime lastModified;

public PathEntry(Path relativeTo, Path path) {
public PathFileEntry(Path relativeTo, Path path) {
this.directory = Files.isDirectory(path);
this.relativeTo = relativeTo;
this.path = path;
var relativized = relativeTo.relativize(path).toString();
relativized = relativized.replace('\\', '/');
this.relativePath = relativized;
try {
this.lastModified = Files.getLastModifiedTime(path).toMillis();
this.lastModified = Files.getLastModifiedTime(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand All @@ -40,7 +37,7 @@ public String relativePath() {
}

@Override
public long lastModified() {
public FileTime lastModified() {
return lastModified;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package net.neoforged.jst.cli.io;
package net.neoforged.jst.api;

import net.neoforged.jst.api.FileEntry;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.attribute.FileTime;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand All @@ -27,8 +28,8 @@ public String relativePath() {
}

@Override
public long lastModified() {
return zipEntry.getLastModifiedTime().toMillis();
public FileTime lastModified() {
return zipEntry.getLastModifiedTime();
}

@Override
Expand Down
85 changes: 11 additions & 74 deletions cli/src/main/java/net/neoforged/jst/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@
@CommandLine.Command(name = "jst", mixinStandardHelpOptions = true, usageHelpWidth = 100)
public class Main implements Callable<Integer> {
@CommandLine.Parameters(index = "0", paramLabel = "INPUT", description = "Path to a single Java-file, a source-archive or a folder containing the source to transform.")
private Path inputPath;
Path inputPath;

@CommandLine.Parameters(index = "1", paramLabel = "OUTPUT", description = "Path to where the resulting source should be placed.")
private Path outputPath;
Path outputPath;

@CommandLine.Option(names = "--in-format", description = "Specify the format of INPUT explicitly. AUTO (the default) performs auto-detection. Other options are SINGLE_FILE for Java files, ARCHIVE for source jars or zips, and FOLDER for folders containing Java code.")
private PathType inputFormat = PathType.AUTO;
PathType inputFormat = PathType.AUTO;

@CommandLine.Option(names = "--out-format", description = "Specify the format of OUTPUT explicitly. Allows the same options as --in-format.")
private PathType outputFormat = PathType.AUTO;
PathType outputFormat = PathType.AUTO;

@CommandLine.Option(names = "--libraries-list", description = "Specifies a file that contains a path to an archive or directory to add to the classpath on each line.")
private Path librariesList;
Path librariesList;

@CommandLine.Option(names = "--max-queue-depth", description = "When both input and output support ordering (archives), the transformer will try to maintain that order. To still process items in parallel, a queue is used. Larger queue depths lead to higher memory usage.")
int maxQueueDepth = 100;

private final HashSet<SourceTransformer> enabledTransformers = new HashSet<>();

Expand All @@ -44,6 +47,7 @@ public static int innerMain(String... args) {

var main = new Main();
var commandLine = new CommandLine(main);
commandLine.setCaseInsensitiveEnumValuesAllowed(true);
var spec = commandLine.getCommandSpec();

main.setupPluginCliOptions(plugins, spec);
Expand All @@ -60,6 +64,8 @@ public Integer call() throws Exception {
processor.addLibrariesList(librariesList);
}

processor.setMaxQueueDepth(maxQueueDepth);

var orderedTransformers = new ArrayList<>(enabledTransformers);

try (var sink = FileSinks.create(outputPath, outputFormat, source)) {
Expand Down Expand Up @@ -106,73 +112,4 @@ public <T> T set(T value) {
spec.addArgGroup(builder.build());
}
}

//
// void poo() {
// String[] args = new String[0];
//
// Path inputPath = null, outputPath = null, namesAndDocsPath = null, librariesPath = null;
// boolean enableJavadoc = true;
// int queueDepth = 50;
//
// for (int i = 0; i < args.length; i++) {
// var arg = args[i];
// switch (arg) {
// case "--in":
// if (i + 1 >= args.length) {
// System.err.println("Missing argument for --in");
// System.exit(1);
// }
// inputPath = Paths.get(args[++i]);
// break;
// case "--out":
// if (i + 1 >= args.length) {
// System.err.println("Missing argument for --out");
// System.exit(1);
// }
// outputPath = Paths.get(args[++i]);
// break;
// case "--libraries":
// if (i + 1 >= args.length) {
// System.err.println("Missing argument for --libraries");
// System.exit(1);
// }
// librariesPath = Paths.get(args[++i]);
// break;
// case "--names":
// if (i + 1 >= args.length) {
// System.err.println("Missing argument for --names");
// System.exit(1);
// }
// namesAndDocsPath = Paths.get(args[++i]);
// break;
// case "--skip-javadoc":
// enableJavadoc = false;
// break;
// case "--queue-depth":
// if (i + 1 >= args.length) {
// System.err.println("Missing argument for --queue-depth");
// System.exit(1);
// }
// queueDepth = Integer.parseUnsignedInt(args[++i]);
// break;
// case "--help":
// printUsage(System.out);
// System.exit(0);
// break;
// default:
// System.err.println("Unknown argument: " + arg);
// printUsage(System.err);
// System.exit(1);
// break;
// }
// }
//
// if (inputPath == null || outputPath == null || namesAndDocsPath == null) {
// System.err.println("Missing arguments");
// printUsage(System.err);
// System.exit(1);
// }
//
// }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package net.neoforged.jst.cli;

import net.neoforged.jst.api.FileEntry;
import net.neoforged.jst.api.FileSink;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.attribute.FileTime;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
Expand Down Expand Up @@ -61,17 +61,27 @@ public void submitAsync(Consumer<FileSink> producer) {
}
}

private static final class ParallelSink implements FileSink {
private final class ParallelSink implements FileSink {
private final List<WorkResult> workResults = new ArrayList<>();

@Override
public boolean canHaveMultipleEntries() {
return sink.canHaveMultipleEntries();
}

@Override
public boolean isOrdered() {
return false;
}

@Override
public void put(FileEntry entry, byte[] content) {
workResults.add(new WorkResult(entry, content));
public void putDirectory(String relativePath) {
workResults.add(new WorkResult(true, relativePath, null, null));
}

@Override
public void putFile(String relativePath, FileTime lastModified, byte[] content) {
workResults.add(new WorkResult(false, relativePath, lastModified, content));
}
}

Expand All @@ -87,7 +97,11 @@ private void drainTo(int drainTo) throws InterruptedException, IOException {
throw new RuntimeException(e.getCause());
}
for (var workResult : workResults) {
sink.put(workResult.entry, workResult.content);
if (workResult.directory) {
sink.putDirectory(workResult.relativePath);
} else {
sink.putFile(workResult.relativePath, workResult.lastModified, workResult.content);
}
}
}
}
Expand All @@ -103,6 +117,6 @@ public void close() throws IOException {
}
}

private record WorkResult(FileEntry entry, byte[] content) {
private record WorkResult(boolean directory, String relativePath, FileTime lastModified, byte[] content) {
}
}
2 changes: 1 addition & 1 deletion cli/src/main/java/net/neoforged/jst/cli/PathType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public enum PathType {
AUTO,
SINGLE_FILE,
FILE,
ARCHIVE,
FOLDER
}
Loading

0 comments on commit e825301

Please sign in to comment.