Skip to content

Commit 3611471

Browse files
committed
improved registration of input files
1 parent 118e16c commit 3611471

21 files changed

Lines changed: 173 additions & 87 deletions

jcp/src/main/java/com/igormaznitsa/jcp/JcpPreprocessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public Statistics execute() throws IOException {
228228

229229
final Collection<FileInfoContainer> filesToBePreprocessed =
230230
collectFilesToPreprocess(srcFolders, this.context.getExcludeFolders());
231-
this.context.getPreprocessedResources().addAll(filesToBePreprocessed);
231+
this.context.addAllPreprocessedResources(filesToBePreprocessed);
232232

233233
final List<PreprocessingState.ExcludeIfInfo> excludedIf =
234234
processGlobalDirectives(filesToBePreprocessed);

jcp/src/main/java/com/igormaznitsa/jcp/containers/FileInfoContainer.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
package com.igormaznitsa.jcp.containers;
2323

24+
import static java.util.Objects.requireNonNull;
25+
26+
2427
import com.igormaznitsa.jcp.context.PreprocessingState;
2528
import com.igormaznitsa.jcp.context.PreprocessorContext;
2629
import com.igormaznitsa.jcp.directives.AbstractDirectiveHandler;
@@ -35,7 +38,6 @@
3538
import java.util.Collection;
3639
import java.util.HashSet;
3740
import java.util.List;
38-
import java.util.Objects;
3941
import java.util.Set;
4042
import java.util.regex.Matcher;
4143
import java.util.regex.Pattern;
@@ -67,9 +69,13 @@ public class FileInfoContainer {
6769
*/
6870
private final boolean copyOnly;
6971
/**
70-
* List of file generated from the file.
72+
* Collection of files generated with the file.
7173
*/
7274
private final Collection<File> generatedResources = new HashSet<>();
75+
/**
76+
* Collection of files which took part during preprocessing of the file
77+
*/
78+
private final Collection<File> includedSources = new HashSet<>();
7379
/**
7480
* The flag shows that the file has been excluded from preprocessing and it will not be preprocessed and copied
7581
*/
@@ -85,8 +91,8 @@ public class FileInfoContainer {
8591

8692
public FileInfoContainer(final File srcFile, final String targetFileName,
8793
final boolean copyOnly) {
88-
Objects.requireNonNull(srcFile, "Source file is null");
89-
Objects.requireNonNull(targetFileName, "Target file name is null");
94+
requireNonNull(srcFile, "Source file is null");
95+
requireNonNull(targetFileName, "Target file name is null");
9096

9197
this.copyOnly = copyOnly;
9298
excludedFromPreprocessing = false;
@@ -123,12 +129,12 @@ private static String findTailRemover(final String str, final PreprocessorContex
123129
}
124130

125131
public void setTargetFolder(final String targetFolder) {
126-
this.targetFolder = Objects.requireNonNull(targetFolder, "Target folder must not be null");
132+
this.targetFolder = requireNonNull(targetFolder, "Target folder must not be null");
127133
}
128134

129135
public void setTargetName(final String targetName) {
130136
this.targetFileName =
131-
Objects.requireNonNull(targetFileName, "Target file name must not be null");
137+
requireNonNull(targetFileName, "Target file name must not be null");
132138
}
133139

134140

@@ -206,7 +212,7 @@ public List<PreprocessingState.ExcludeIfInfo> processGlobalDirectives(
206212
}
207213
}
208214
if (!preprocessingState.isIfStackEmpty()) {
209-
final TextFileDataContainer lastIf = Objects.requireNonNull(preprocessingState.peekIf());
215+
final TextFileDataContainer lastIf = requireNonNull(preprocessingState.peekIf());
210216
throw new PreprocessorException(
211217
"Unclosed " + AbstractDirectiveHandler.DIRECTIVE_PREFIX + "_if instruction detected",
212218
"", new FilePositionInfo[] {
@@ -336,8 +342,12 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
336342
context.clearLocalVariables();
337343
}
338344

339-
final PreprocessingState preprocessingState =
340-
state != null ? state : context.produceNewPreprocessingState(this, 1);
345+
final PreprocessingState preprocessingState;
346+
if (state == null) {
347+
preprocessingState = context.produceNewPreprocessingState(this, 1);
348+
} else {
349+
preprocessingState = state;
350+
}
341351

342352
String leftTrimmedString = null;
343353

@@ -402,7 +412,7 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
402412
AbstractDirectiveHandler.PREFIX_FOR_KEEPING_LINES_PROCESSED_DIRECTIVES +
403413
extractedDirective;
404414
final ResetablePrinter thePrinter =
405-
Objects.requireNonNull(preprocessingState.getPrinter());
415+
requireNonNull(preprocessingState.getPrinter());
406416
if (doPrintLn) {
407417
thePrinter.println(text, context.getEol());
408418
} else {
@@ -416,7 +426,7 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
416426
AbstractDirectiveHandler.PREFIX_FOR_KEEPING_LINES_PROCESSED_DIRECTIVES +
417427
extractedDirective;
418428
final ResetablePrinter thePrinter =
419-
Objects.requireNonNull(preprocessingState.getPrinter());
429+
requireNonNull(preprocessingState.getPrinter());
420430
if (doPrintLn) {
421431
thePrinter.println(text, context.getEol());
422432
} else {
@@ -429,7 +439,7 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
429439
}
430440
}
431441

432-
final ResetablePrinter thePrinter = Objects.requireNonNull(preprocessingState.getPrinter());
442+
final ResetablePrinter thePrinter = requireNonNull(preprocessingState.getPrinter());
433443
if (preprocessingState.isDirectiveCanBeProcessed() &&
434444
!preprocessingState.getPreprocessingFlags()
435445
.contains(PreprocessingFlag.TEXT_OUTPUT_DISABLED)) {
@@ -494,22 +504,22 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
494504

495505
if (!preprocessingState.isIfStackEmpty()) {
496506
final TextFileDataContainer lastIf =
497-
Objects.requireNonNull(preprocessingState.peekIf(), "'IF' stack is empty");
507+
requireNonNull(preprocessingState.peekIf(), "'IF' stack is empty");
498508
throw new PreprocessorException(
499509
"Unclosed " + AbstractDirectiveHandler.DIRECTIVE_PREFIX + "if instruction detected",
500510
"", new FilePositionInfo[] {
501511
new FilePositionInfo(lastIf.getFile(), lastIf.getNextStringIndex())}, null);
502512
}
503513
if (!preprocessingState.isWhileStackEmpty()) {
504514
final TextFileDataContainer lastWhile =
505-
Objects.requireNonNull(preprocessingState.peekWhile(), "'WHILE' stack is empty");
515+
requireNonNull(preprocessingState.peekWhile(), "'WHILE' stack is empty");
506516
throw new PreprocessorException(
507517
"Unclosed " + AbstractDirectiveHandler.DIRECTIVE_PREFIX + "while instruction detected",
508518
"", new FilePositionInfo[] {
509519
new FilePositionInfo(lastWhile.getFile(), lastWhile.getNextStringIndex())}, null);
510520
}
511521

512-
if (!context.isDryRun() && Objects.requireNonNull(lastTextFileDataContainer).isAutoFlush()) {
522+
if (!context.isDryRun() && requireNonNull(lastTextFileDataContainer).isAutoFlush()) {
513523
final File outFile = context.createDestinationFileForPath(makeTargetFilePathAsString());
514524

515525
final boolean wasSaved =

jcp/src/main/java/com/igormaznitsa/jcp/context/PreprocessingState.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package com.igormaznitsa.jcp.context;
2323

2424
import static com.igormaznitsa.jcp.utils.IOUtils.closeQuietly;
25+
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findFirstActiveFileContainer;
2526

2627

2728
import com.igormaznitsa.jcp.containers.FileInfoContainer;
@@ -218,17 +219,14 @@ public TextFileDataContainer openFile(final File file) throws IOException {
218219
return newContainer;
219220
}
220221

221-
222222
public TextFileDataContainer peekFile() {
223223
return includeStack.peek();
224224
}
225225

226-
227-
List<TextFileDataContainer> getCurrentIncludeStack() {
226+
public List<TextFileDataContainer> getCurrentIncludeStack() {
228227
return this.includeStack;
229228
}
230229

231-
232230
public FilePositionInfo[] makeIncludeStack() {
233231
if (this.fake) {
234232
return EMPTY_STACK;
@@ -484,8 +482,11 @@ public boolean saveBuffersToFile(final File outFile, final boolean keepComments)
484482
closeQuietly(writer);
485483
}
486484

487-
if (wasSaved && this.context.isKeepAttributes() && outFile.exists()) {
488-
PreprocessorUtils.copyFileAttributes(this.getRootFileInfo().getSourceFile(), outFile);
485+
if (wasSaved) {
486+
findFirstActiveFileContainer(context).ifPresent(t -> t.getGeneratedResources().add(outFile));
487+
if (this.context.isKeepAttributes() && outFile.exists()) {
488+
PreprocessorUtils.copyFileAttributes(this.getRootFileInfo().getSourceFile(), outFile);
489+
}
489490
}
490491

491492
return wasSaved;

jcp/src/main/java/com/igormaznitsa/jcp/context/PreprocessorContext.java

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import java.util.Set;
5353
import java.util.concurrent.atomic.AtomicReference;
5454
import java.util.stream.Collectors;
55-
import java.util.stream.Stream;
5655
import lombok.AccessLevel;
5756
import lombok.Data;
5857
import lombok.Getter;
@@ -86,8 +85,12 @@ public class PreprocessorContext {
8685
private final TextFileDataContainer currentInCloneSource;
8786
private final List<SourceFolder> sources = new ArrayList<>();
8887
private final File baseDir;
89-
private final Collection<File> activatedConfigFiles = new ArrayList<>();
90-
private final Collection<FileInfoContainer> preprocessedResources = new ArrayList<>();
88+
private final Collection<File> activatedConfigFiles;
89+
90+
@Setter(AccessLevel.NONE)
91+
@Getter(AccessLevel.NONE)
92+
private final Collection<FileInfoContainer> preprocessedResources;
93+
9194
@Setter(AccessLevel.NONE)
9295
@Getter(AccessLevel.NONE)
9396
private final AtomicReference<PreprocessingState> preprocessingState = new AtomicReference<>();
@@ -121,6 +124,8 @@ public class PreprocessorContext {
121124
* @param baseDir the base folder for process, it must not be null
122125
*/
123126
public PreprocessorContext(final File baseDir) {
127+
this.preprocessedResources = new ArrayList<>();
128+
this.activatedConfigFiles = new ArrayList<>();
124129
this.baseDir = Objects.requireNonNull(baseDir, "Base folder must not be null");
125130
this.preprocessingState
126131
.set(new PreprocessingState(this, this.sourceEncoding, this.targetEncoding));
@@ -139,8 +144,8 @@ public PreprocessorContext(final File baseDir) {
139144
public PreprocessorContext(final PreprocessorContext context) {
140145
Objects.requireNonNull(context, "Source context must not be null");
141146

142-
this.activatedConfigFiles.addAll(context.activatedConfigFiles);
143-
this.preprocessedResources.addAll(context.preprocessedResources);
147+
this.activatedConfigFiles = context.activatedConfigFiles;
148+
this.preprocessedResources = context.preprocessedResources;
144149

145150
this.baseDir = context.getBaseDir();
146151
this.verbose = context.isVerbose();
@@ -191,6 +196,22 @@ public PreprocessorContext(final PreprocessorContext context) {
191196
this.currentInCloneSource = context.getPreprocessingState().peekFile();
192197
}
193198

199+
public void addPreprocessedResource(final FileInfoContainer container) {
200+
if (container != null) {
201+
this.preprocessedResources.add(container);
202+
}
203+
}
204+
205+
public void addAllPreprocessedResources(final Collection<FileInfoContainer> containers) {
206+
if (containers != null) {
207+
this.preprocessedResources.addAll(containers);
208+
}
209+
}
210+
211+
public Set<FileInfoContainer> findPreprocessedResources() {
212+
return new HashSet<>(this.preprocessedResources);
213+
}
214+
194215
private static String makeStackView(
195216
final TextFileDataContainer cloneSource,
196217
final boolean cloned,
@@ -241,35 +262,31 @@ private static Charset decodeCharset(final String charsetName) {
241262
}
242263
}
243264

244-
public Collection<File> findAllInputFiles() {
245-
return Stream.concat(this.configFiles.stream(),
246-
this.preprocessedResources.stream().map(FileInfoContainer::getSourceFile)
247-
).collect(Collectors.toSet());
265+
public Set<File> findAllInputFiles() {
266+
final Set<File> result = new HashSet<>();
267+
result.addAll(this.configFiles);
268+
this.preprocessedResources.forEach(x -> {
269+
result.addAll(x.getIncludedSources());
270+
if (x.getSourceFile() != null &&
271+
!(x.getIncludedSources().isEmpty() && x.isExcludedFromPreprocessing())) {
272+
result.add(x.getSourceFile());
273+
}
274+
});
275+
return result;
248276
}
249277

250-
public Collection<File> findAllGeneratedFiles() {
278+
public Set<File> findAllGeneratedFiles() {
251279
return this.preprocessedResources.stream()
252280
.flatMap(x -> x.getGeneratedResources().stream())
253281
.collect(Collectors.toSet());
254282
}
255283

256-
public void notifyAboutFileInfoContainer(final FileInfoContainer fileInfoContainer) {
257-
if (fileInfoContainer != null) {
258-
final FileInfoContainer existing =
259-
this.findFileInfoContainer(fileInfoContainer.getSourceFile()).orElse(null);
260-
if (existing == null) {
261-
this.preprocessedResources.add(fileInfoContainer);
262-
} else {
263-
existing.getGeneratedResources().addAll(fileInfoContainer.getGeneratedResources());
264-
}
265-
}
266-
}
267-
268284
public Optional<FileInfoContainer> findFileInfoContainer(final File file) {
269285
if (file == null) {
270286
return Optional.empty();
271287
} else {
272-
return this.preprocessedResources.stream().filter(x -> file.equals(x.getSourceFile()))
288+
return this.preprocessedResources.stream()
289+
.filter(x -> file.equals(x.getSourceFile()))
273290
.findFirst();
274291
}
275292
}

jcp/src/main/java/com/igormaznitsa/jcp/directives/FlushDirectiveHandler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
package com.igormaznitsa.jcp.directives;
2323

24+
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findFirstActiveFileContainer;
25+
26+
2427
import com.igormaznitsa.jcp.context.PreprocessingState;
2528
import com.igormaznitsa.jcp.context.PreprocessorContext;
2629
import java.io.File;
@@ -63,6 +66,9 @@ public AfterDirectiveProcessingBehaviour execute(final String string,
6366
"Content was " + (saved ? "saved" : "not saved") + " into file '" + outFile + "'");
6467
}
6568

69+
findFirstActiveFileContainer(context)
70+
.ifPresent(f -> f.getGeneratedResources().add(outFile));
71+
6672
state.resetPrinters();
6773
} catch (IOException ex) {
6874
throw context.makeException("Can't flush text buffers", ex);

jcp/src/main/java/com/igormaznitsa/jcp/directives/IncludeDirectiveHandler.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
package com.igormaznitsa.jcp.directives;
2323

24+
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findFirstActiveFileContainer;
25+
26+
2427
import com.igormaznitsa.jcp.context.PreprocessingState;
2528
import com.igormaznitsa.jcp.context.PreprocessorContext;
2629
import com.igormaznitsa.jcp.expression.Expression;
@@ -63,11 +66,14 @@ public AfterDirectiveProcessingBehaviour execute(final String string,
6366
final String filePath = includingFilePath.toString();
6467

6568
try {
66-
final File theFile = context.findFileInSources(filePath);
69+
final File fileToInclude = context.findFileInSources(filePath);
6770
if (context.isVerbose()) {
68-
context.logForVerbose("Including file '" + theFile.getCanonicalPath() + '\'');
71+
context.logForVerbose("Including file '" + fileToInclude.getCanonicalPath() + '\'');
6972
}
70-
state.openFile(theFile);
73+
state.openFile(fileToInclude);
74+
75+
findFirstActiveFileContainer(context)
76+
.ifPresent(f -> f.getIncludedSources().add(fileToInclude));
7177
} catch (IOException ex) {
7278
throw context.makeException("Can't open file '" + filePath + '\'', ex);
7379
}

0 commit comments

Comments
 (0)