Skip to content

Commit 26d5837

Browse files
authored
Add the option to add path-prefixes that should not be transformed (#14)
1 parent 5386551 commit 26d5837

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ It can be invoked as a standalone executable Jar-File. Java 17 is required.
2929
```
3030
Usage: jst [-hV] [--in-format=<inputFormat>] [--libraries-list=<librariesList>]
3131
[--max-queue-depth=<maxQueueDepth>] [--out-format=<outputFormat>]
32-
[--classpath=<addToClasspath>]... [--enable-parchment
33-
--parchment-mappings=<mappingsPath> [--[no-]parchment-javadoc]
32+
[--classpath=<addToClasspath>]... [--ignore-prefix=<ignoredPrefixes>]...
33+
[--enable-parchment --parchment-mappings=<mappingsPath> [--[no-]parchment-javadoc]
3434
[--parchment-conflict-prefix=<conflictPrefix>]] [--enable-accesstransformers
3535
--access-transformer=<atFiles> [--access-transformer=<atFiles>]...
3636
[--access-transformer-validation=<validation>]] INPUT OUTPUT
@@ -40,6 +40,9 @@ Usage: jst [-hV] [--in-format=<inputFormat>] [--libraries-list=<librariesList>]
4040
--classpath=<addToClasspath>
4141
Additional classpath entries to use. Is combined with --libraries-list.
4242
-h, --help Show this help message and exit.
43+
--ignore-prefix=<ignoredPrefixes>
44+
Do not apply transformations to paths that start with any of these
45+
prefixes.
4346
--in-format=<inputFormat>
4447
Specify the format of INPUT explicitly. AUTO (the default) performs
4548
auto-detection. Other options are SINGLE_FILE for Java files, ARCHIVE

cli/src/main/java/net/neoforged/jst/cli/Main.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class Main implements Callable<Integer> {
3232
@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.")
3333
Path librariesList;
3434

35+
@CommandLine.Option(names = "--ignore-prefix", description = "Do not apply transformations to paths that start with any of these prefixes.")
36+
List<String> ignoredPrefixes = new ArrayList<>();
37+
3538
@CommandLine.Option(names = "--classpath", description = "Additional classpath entries to use. Is combined with --libraries-list.", converter = ClasspathConverter.class)
3639
List<Path> addToClasspath = new ArrayList<>();
3740

@@ -73,6 +76,9 @@ public Integer call() throws Exception {
7376
for (Path path : addToClasspath) {
7477
processor.addLibrary(path);
7578
}
79+
for (String ignoredPrefix : ignoredPrefixes) {
80+
processor.addIgnoredPrefix(ignoredPrefix);
81+
}
7682

7783
processor.setMaxQueueDepth(maxQueueDepth);
7884

cli/src/main/java/net/neoforged/jst/cli/SourceFileProcessor.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.nio.file.Path;
1919
import java.nio.file.attribute.FileTime;
2020
import java.time.Instant;
21+
import java.util.ArrayList;
2122
import java.util.List;
2223

2324
/**
@@ -29,6 +30,8 @@ class SourceFileProcessor implements AutoCloseable {
2930
private int maxQueueDepth = 50;
3031
private final Logger logger;
3132

33+
private final List<String> ignoredPrefixes = new ArrayList<>();
34+
3235
public SourceFileProcessor(Logger logger) throws IOException {
3336
this.logger = logger;
3437
ijEnv = new IntelliJEnvironmentImpl(logger);
@@ -90,7 +93,7 @@ private void processEntry(FileEntry entry, VirtualFile sourceRoot, List<SourceTr
9093
byte[] content = in.readAllBytes();
9194
var lastModified = entry.lastModified();
9295

93-
if (!transformers.isEmpty() && entry.hasExtension("java")) {
96+
if (!isIgnored(entry.relativePath()) && !transformers.isEmpty() && entry.hasExtension("java")) {
9497
var orgContent = content;
9598
content = transformSource(sourceRoot, entry.relativePath(), transformers, content);
9699
if (orgContent != content) {
@@ -101,6 +104,15 @@ private void processEntry(FileEntry entry, VirtualFile sourceRoot, List<SourceTr
101104
}
102105
}
103106

107+
private boolean isIgnored(String relativePath) {
108+
for (String ignoredPrefix : ignoredPrefixes) {
109+
if (relativePath.startsWith(ignoredPrefix)) {
110+
return true;
111+
}
112+
}
113+
return false;
114+
}
115+
104116
byte[] transformSource(VirtualFile contentRoot, String path, List<SourceTransformer> transformers, byte[] originalContentBytes) {
105117
// Instead of parsing the content we actually read from the file, we read the virtual file that is
106118
// visible to IntelliJ from adding the source jar. The reasoning is that IntelliJ will cache this internally
@@ -145,6 +157,11 @@ public void addLibrary(Path library) {
145157
ClasspathSetup.addLibrary(logger, library, ijEnv);
146158
}
147159

160+
public void addIgnoredPrefix(String ignoredPrefix) {
161+
System.out.println("Not transforming entries starting with " + ignoredPrefix);
162+
this.ignoredPrefixes.add(ignoredPrefix);
163+
}
164+
148165
@Override
149166
public void close() throws IOException {
150167
ijEnv.close();

parchment/src/main/java/net/neoforged/jst/parchment/GatherReplacementsVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ public void visitElement(@NotNull PsiElement element) {
6161
if (foundClass == null) {
6262
throw new IllegalStateException("Failed to find how class " + psiClass.getQualifiedName() + " was loaded while processing it");
6363
} else if (foundClass != psiClass) {
64-
throw new IllegalStateException("Class " + psiClass + " was loaded from two different sources: " +
65-
psiClass.getContainingFile() + " and " + foundClass.getContainingFile());
64+
throw new IllegalStateException("Class " + psiClass.getQualifiedName() + " was loaded from two different sources: " +
65+
psiClass.getContainingFile().getVirtualFile().getPath() + " and " +
66+
foundClass.getContainingFile().getVirtualFile().getPath());
6667
}
6768
}
6869

0 commit comments

Comments
 (0)