Skip to content

Commit 8492caa

Browse files
committed
Add hidden prefixes
JSTs ignored prefixes are still pushed into the output file sink, which is not needed when the input and output sources are the same. The commit adds a stronger version of ignored prefixes, "hidden" prefixes, which are not processed or pushed to the output sink. This mainly aims to prevent potential write permission issues on read-only files during in-place transformation.
1 parent 81d66a5 commit 8492caa

File tree

8 files changed

+42
-2
lines changed

8 files changed

+42
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Usage: jst [-hV] [--in-format=<inputFormat>] [--libraries-list=<librariesList>]
7272
--classpath=<addToClasspath>
7373
Additional classpath entries to use. Is combined with --libraries-list.
7474
-h, --help Show this help message and exit.
75+
--hidden-prefix=<hiddenPrefixes>
76+
Do not process or emit paths that start with any of these prefixes.
7577
--ignore-prefix=<ignoredPrefixes>
7678
Do not apply transformations to paths that start with any of these
7779
prefixes.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public class Main implements Callable<Integer> {
3535
@CommandLine.Option(names = "--ignore-prefix", description = "Do not apply transformations to paths that start with any of these prefixes.")
3636
List<String> ignoredPrefixes = new ArrayList<>();
3737

38+
@CommandLine.Option(names = "--hidden-prefix", description = "Do not process or emit paths that start with any of these prefixes.")
39+
List<String> hiddenPrefixes = new ArrayList<>();
40+
3841
@CommandLine.Option(names = "--classpath", description = "Additional classpath entries to use. Is combined with --libraries-list.", converter = ClasspathConverter.class)
3942
List<Path> addToClasspath = new ArrayList<>();
4043

@@ -79,6 +82,10 @@ public Integer call() throws Exception {
7982
for (String ignoredPrefix : ignoredPrefixes) {
8083
processor.addIgnoredPrefix(ignoredPrefix);
8184
}
85+
for (String hiddenPrefix : hiddenPrefixes) {
86+
processor.addIgnoredPrefix(hiddenPrefix);
87+
processor.addHiddenPrefix(hiddenPrefix);
88+
}
8289

8390
processor.setMaxQueueDepth(maxQueueDepth);
8491

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class SourceFileProcessor implements AutoCloseable {
3434
private final Logger logger;
3535

3636
private final List<String> ignoredPrefixes = new ArrayList<>();
37+
private final List<String> hiddenPrefixes = new ArrayList<>();
3738

3839
public SourceFileProcessor(Logger logger) throws IOException {
3940
this.logger = logger;
@@ -114,13 +115,21 @@ private boolean processEntry(FileEntry entry, VirtualFile sourceRoot, List<Sourc
114115
lastModified = FileTime.from(Instant.now());
115116
}
116117
}
117-
sink.putFile(entry.relativePath(), lastModified, content);
118+
if (!isHidden(entry.relativePath())) sink.putFile(entry.relativePath(), lastModified, content);
118119
}
119120
return true;
120121
}
121122

122123
private boolean isIgnored(String relativePath) {
123-
for (String ignoredPrefix : ignoredPrefixes) {
124+
return isRelativePathIn(relativePath, this.ignoredPrefixes);
125+
}
126+
127+
private boolean isHidden(String relativePath) {
128+
return isRelativePathIn(relativePath, this.hiddenPrefixes);
129+
}
130+
131+
private boolean isRelativePathIn(String relativePath, List<String> prefixes) {
132+
for (String ignoredPrefix : prefixes) {
124133
if (relativePath.startsWith(ignoredPrefix)) {
125134
return true;
126135
}
@@ -187,6 +196,12 @@ public void addIgnoredPrefix(String ignoredPrefix) {
187196
this.ignoredPrefixes.add(ignoredPrefix);
188197
}
189198

199+
public void addHiddenPrefix(String hiddenPrefix) {
200+
System.out.println("Not reading entries starting with " + hiddenPrefix);
201+
this.ignoredPrefixes.add(hiddenPrefix);
202+
this.hiddenPrefixes.add(hiddenPrefix);
203+
}
204+
190205
@Override
191206
public void close() throws IOException {
192207
ijEnv.close();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public C1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public class C1 {
2+
C1() {}
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class C1 {
2+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package other;
2+
3+
final class C2f {
4+
5+
}

tests/src/test/java/net/neoforged/jst/tests/EmbeddedTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ void testIllegal() throws Exception {
292292
void testFolderClasspathEntries() throws Exception {
293293
runATTest("folder_classpath_entry", "--classpath=" + testDataRoot.resolve("accesstransformer/folder_classpath_entry/deps"));
294294
}
295+
296+
@Test
297+
void testHiddenPrefixes() throws Exception {
298+
runATTest("hidden_prefix", "--hidden-prefix=other");
299+
}
295300
}
296301

297302
@Nested

0 commit comments

Comments
 (0)