Skip to content

Commit

Permalink
Add the option to add path-prefixes that should not be transformed
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Jun 7, 2024
1 parent bece732 commit c49f3b1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
6 changes: 6 additions & 0 deletions cli/src/main/java/net/neoforged/jst/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class Main implements Callable<Integer> {
@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.")
Path librariesList;

@CommandLine.Option(names = "--ignore-prefix", description = "Do not transforms entry whose path start with this prefix.")
List<String> ignoredPrefixes = new ArrayList<>();

@CommandLine.Option(names = "--classpath", description = "Additional classpath entries to use. Is combined with --libraries-list.", converter = ClasspathConverter.class)
List<Path> addToClasspath = new ArrayList<>();

Expand Down Expand Up @@ -69,6 +72,9 @@ public Integer call() throws Exception {
for (Path path : addToClasspath) {
processor.addLibrary(path);
}
for (String ignoredPrefix : ignoredPrefixes) {
processor.addIgnoredPrefix(ignoredPrefix);
}

processor.setMaxQueueDepth(maxQueueDepth);

Expand Down
19 changes: 18 additions & 1 deletion cli/src/main/java/net/neoforged/jst/cli/SourceFileProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -27,6 +28,8 @@ class SourceFileProcessor implements AutoCloseable {
private final IntelliJEnvironmentImpl ijEnv = new IntelliJEnvironmentImpl();
private int maxQueueDepth = 50;

private final List<String> ignoredPrefixes = new ArrayList<>();

public SourceFileProcessor() throws IOException {
ijEnv.addCurrentJdkToClassPath();
}
Expand Down Expand Up @@ -83,7 +86,7 @@ private void processEntry(FileEntry entry, VirtualFile sourceRoot, List<SourceTr
byte[] content = in.readAllBytes();
var lastModified = entry.lastModified();

if (!transformers.isEmpty() && entry.hasExtension("java")) {
if (!isIgnored(entry.relativePath()) && !transformers.isEmpty() && entry.hasExtension("java")) {
var orgContent = content;
content = transformSource(sourceRoot, entry.relativePath(), transformers, content);
if (orgContent != content) {
Expand All @@ -94,6 +97,15 @@ private void processEntry(FileEntry entry, VirtualFile sourceRoot, List<SourceTr
}
}

private boolean isIgnored(String relativePath) {
for (String ignoredPrefix : ignoredPrefixes) {
if (relativePath.startsWith(ignoredPrefix)) {
return true;
}
}
return false;
}

byte[] transformSource(VirtualFile contentRoot, String path, List<SourceTransformer> transformers, byte[] originalContentBytes) {
// Instead of parsing the content we actually read from the file, we read the virtual file that is
// visible to IntelliJ from adding the source jar. The reasoning is that IntelliJ will cache this internally
Expand Down Expand Up @@ -138,6 +150,11 @@ public void addLibrary(Path library) {
ClasspathSetup.addLibrary(library, ijEnv);
}

public void addIgnoredPrefix(String ignoredPrefix) {
System.out.println("Not transforming entries starting with " + ignoredPrefix);
this.ignoredPrefixes.add(ignoredPrefix);
}

@Override
public void close() throws IOException {
ijEnv.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public void visitElement(@NotNull PsiElement element) {
if (foundClass == null) {
throw new IllegalStateException("Failed to find how class " + psiClass.getQualifiedName() + " was loaded while processing it");
} else if (foundClass != psiClass) {
throw new IllegalStateException("Class " + psiClass + " was loaded from two different sources: " +
psiClass.getContainingFile() + " and " + foundClass.getContainingFile());
throw new IllegalStateException("Class " + psiClass.getQualifiedName() + " was loaded from two different sources: " +
psiClass.getContainingFile().getVirtualFile().getPath() + " and " +
foundClass.getContainingFile().getVirtualFile().getPath());
}
}

Expand Down

0 comments on commit c49f3b1

Please sign in to comment.