Skip to content

Commit

Permalink
Add the option to add path-prefixes that should not be transformed (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte authored Jul 4, 2024
1 parent 5386551 commit 26d5837
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ It can be invoked as a standalone executable Jar-File. Java 17 is required.
```
Usage: jst [-hV] [--in-format=<inputFormat>] [--libraries-list=<librariesList>]
[--max-queue-depth=<maxQueueDepth>] [--out-format=<outputFormat>]
[--classpath=<addToClasspath>]... [--enable-parchment
--parchment-mappings=<mappingsPath> [--[no-]parchment-javadoc]
[--classpath=<addToClasspath>]... [--ignore-prefix=<ignoredPrefixes>]...
[--enable-parchment --parchment-mappings=<mappingsPath> [--[no-]parchment-javadoc]
[--parchment-conflict-prefix=<conflictPrefix>]] [--enable-accesstransformers
--access-transformer=<atFiles> [--access-transformer=<atFiles>]...
[--access-transformer-validation=<validation>]] INPUT OUTPUT
Expand All @@ -40,6 +40,9 @@ Usage: jst [-hV] [--in-format=<inputFormat>] [--libraries-list=<librariesList>]
--classpath=<addToClasspath>
Additional classpath entries to use. Is combined with --libraries-list.
-h, --help Show this help message and exit.
--ignore-prefix=<ignoredPrefixes>
Do not apply transformations to paths that start with any of these
prefixes.
--in-format=<inputFormat>
Specify the format of INPUT explicitly. AUTO (the default) performs
auto-detection. Other options are SINGLE_FILE for Java files, ARCHIVE
Expand Down
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 @@ -32,6 +32,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 apply transformations to paths that start with any of these prefixes.")
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 @@ -73,6 +76,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 @@ -18,6 +18,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 @@ -29,6 +30,8 @@ class SourceFileProcessor implements AutoCloseable {
private int maxQueueDepth = 50;
private final Logger logger;

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

public SourceFileProcessor(Logger logger) throws IOException {
this.logger = logger;
ijEnv = new IntelliJEnvironmentImpl(logger);
Expand Down Expand Up @@ -90,7 +93,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 @@ -101,6 +104,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 @@ -145,6 +157,11 @@ public void addLibrary(Path library) {
ClasspathSetup.addLibrary(logger, 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 @@ -61,8 +61,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 26d5837

Please sign in to comment.