|
54 | 54 | import com.igormaznitsa.jcp.context.CommentTextProcessor; |
55 | 55 | import com.igormaznitsa.jcp.context.PreprocessingState; |
56 | 56 | import com.igormaznitsa.jcp.context.PreprocessorContext; |
57 | | -import com.igormaznitsa.jcp.context.SpecialVariableProcessor; |
58 | 57 | import com.igormaznitsa.jcp.directives.ExcludeIfDirectiveHandler; |
59 | 58 | import com.igormaznitsa.jcp.exceptions.FilePositionInfo; |
60 | 59 | import com.igormaznitsa.jcp.exceptions.PreprocessorException; |
@@ -222,68 +221,41 @@ public PreprocessorContext getContext() { |
222 | 221 | return this.context; |
223 | 222 | } |
224 | 223 |
|
225 | | - |
226 | 224 | public Statistics execute() throws IOException { |
227 | | - this.context.getCommentTextProcessors().forEach(x -> x.onContextStarted(this.context)); |
228 | | - this.context.getMapVariableNameToSpecialVarProcessor() |
229 | | - .values().forEach(x -> x.onContextStarted(this.context)); |
230 | | - |
231 | 225 | final long timeStart = System.currentTimeMillis(); |
232 | | - Throwable throwable = null; |
233 | | - Statistics stat = null; |
234 | | - try { |
235 | | - this.context.getActivatedConfigFiles().addAll(processConfigFiles()); |
| 226 | + this.context.getActivatedConfigFiles().addAll(processConfigFiles()); |
236 | 227 |
|
237 | | - this.context.logInfo(String |
238 | | - .format("File extensions: %s excluded %s", this.context.getExtensions(), |
239 | | - this.context.getExcludeExtensions())); |
240 | | - final List<PreprocessorContext.SourceFolder> srcFolders = this.context.getSources(); |
241 | | - this.context.logDebug("Source folders: " + srcFolders); |
| 228 | + this.context.logInfo(String |
| 229 | + .format("File extensions: %s excluded %s", this.context.getExtensions(), |
| 230 | + this.context.getExcludeExtensions())); |
| 231 | + final List<PreprocessorContext.SourceFolder> srcFolders = this.context.getSources(); |
| 232 | + this.context.logDebug("Source folders: " + srcFolders); |
242 | 233 |
|
243 | | - if (srcFolders.isEmpty()) { |
244 | | - this.context.logWarning("Source folder list is empty!"); |
245 | | - } |
| 234 | + if (srcFolders.isEmpty()) { |
| 235 | + this.context.logWarning("Source folder list is empty!"); |
| 236 | + } |
246 | 237 |
|
247 | | - final Collection<FileInfoContainer> filesToBePreprocessed = |
248 | | - collectFilesToPreprocess(srcFolders, this.context.getExcludeFolders()); |
249 | | - this.context.addAllPreprocessedResources(filesToBePreprocessed); |
| 238 | + final Collection<FileInfoContainer> filesToBePreprocessed = |
| 239 | + collectFilesToPreprocess(srcFolders, this.context.getExcludeFolders()); |
| 240 | + this.context.addAllPreprocessedResources(filesToBePreprocessed); |
250 | 241 |
|
251 | | - final List<PreprocessingState.ExcludeIfInfo> excludedIf = |
252 | | - processGlobalDirectives(filesToBePreprocessed); |
| 242 | + final List<PreprocessingState.ExcludeIfInfo> excludedIf = |
| 243 | + processGlobalDirectives(filesToBePreprocessed); |
253 | 244 |
|
254 | | - processFileExclusion(excludedIf); |
255 | | - if (!this.context.isDryRun()) { |
256 | | - createTargetFolder(); |
257 | | - } else { |
258 | | - this.context.logInfo("Dry run mode is ON"); |
259 | | - } |
260 | | - stat = preprocessFiles(filesToBePreprocessed); |
261 | | - } catch (Throwable ex) { |
262 | | - throwable = ex; |
263 | | - if (ex instanceof IOException) { |
264 | | - throw (IOException) ex; |
265 | | - } |
266 | | - } finally { |
267 | | - try { |
268 | | - for (final CommentTextProcessor p : this.context.getCommentTextProcessors()) { |
269 | | - p.onContextStopped(this.context, throwable); |
270 | | - } |
271 | | - } finally { |
272 | | - for (final SpecialVariableProcessor p : this.context.getMapVariableNameToSpecialVarProcessor() |
273 | | - .values()) { |
274 | | - p.onContextStopped(this.context, throwable); |
275 | | - } |
276 | | - } |
277 | | - } |
278 | | - if (stat != null) { |
279 | | - final long elapsedTime = System.currentTimeMillis() - timeStart; |
280 | | - this.context.logInfo("-----------------------------------------------------------------"); |
281 | | - this.context.logInfo(String |
282 | | - .format("Preprocessed %d files, copied %d files, ignored %d files, elapsed time %d ms", |
283 | | - stat.getPreprocessed(), stat.getCopied(), stat.getExcluded(), elapsedTime)); |
| 245 | + processFileExclusion(excludedIf); |
| 246 | + if (!this.context.isDryRun()) { |
| 247 | + createTargetFolder(); |
| 248 | + } else { |
| 249 | + this.context.logInfo("Dry run mode is ON"); |
284 | 250 | } |
285 | | - return stat; |
| 251 | + final Statistics stat = this.preprocessFiles(filesToBePreprocessed, true); |
286 | 252 |
|
| 253 | + final long elapsedTime = System.currentTimeMillis() - timeStart; |
| 254 | + this.context.logInfo("-----------------------------------------------------------------"); |
| 255 | + this.context.logInfo(String |
| 256 | + .format("Preprocessed %d files, copied %d files, ignored %d files, elapsed time %d ms", |
| 257 | + stat.getPreprocessed(), stat.getCopied(), stat.getExcluded(), elapsedTime)); |
| 258 | + return stat; |
287 | 259 | } |
288 | 260 |
|
289 | 261 | private void processFileExclusion(final List<PreprocessingState.ExcludeIfInfo> foundExcludeIf) { |
@@ -353,54 +325,75 @@ private List<PreprocessingState.ExcludeIfInfo> processGlobalDirectives( |
353 | 325 | } |
354 | 326 |
|
355 | 327 |
|
356 | | - private Statistics preprocessFiles(final Collection<FileInfoContainer> files) throws IOException { |
| 328 | + private Statistics preprocessFiles(final Collection<FileInfoContainer> files, |
| 329 | + final boolean notifyProcessors) throws IOException { |
| 330 | + if (notifyProcessors) { |
| 331 | + context.fireNotificationStart(); |
| 332 | + } |
| 333 | + |
357 | 334 | int preprocessedCounter = 0; |
358 | 335 | int copiedCounter = 0; |
359 | 336 | int excludedCounter = 0; |
360 | 337 |
|
361 | | - for (final FileInfoContainer fileRef : files) { |
362 | | - if (fileRef.isExcludedFromPreprocessing()) { |
363 | | - excludedCounter++; |
364 | | - } else if (fileRef.isCopyOnly()) { |
365 | | - if (!context.isDryRun()) { |
366 | | - final File destinationFile = |
367 | | - this.context.createDestinationFileForPath(fileRef.makeTargetFilePathAsString()); |
368 | | - boolean doCopy = true; |
369 | | - |
370 | | - if (this.context.isDontOverwriteSameContent() && |
371 | | - PreprocessorUtils.isFileContentEquals(fileRef.getSourceFile(), destinationFile)) { |
372 | | - doCopy = false; |
373 | | - if (this.context.isVerbose()) { |
374 | | - this.context.logForVerbose(String |
375 | | - .format("Copy skipped because same content: %s -> {dst} %s", |
376 | | - PreprocessorUtils.getFilePath(fileRef.getSourceFile()), |
377 | | - fileRef.makeTargetFilePathAsString())); |
| 338 | + Throwable error = null; |
| 339 | + try { |
| 340 | + for (final FileInfoContainer fileRef : files) { |
| 341 | + if (fileRef.isExcludedFromPreprocessing()) { |
| 342 | + excludedCounter++; |
| 343 | + } else if (fileRef.isCopyOnly()) { |
| 344 | + if (!context.isDryRun()) { |
| 345 | + final File destinationFile = |
| 346 | + this.context.createDestinationFileForPath(fileRef.makeTargetFilePathAsString()); |
| 347 | + boolean doCopy = true; |
| 348 | + |
| 349 | + if (this.context.isDontOverwriteSameContent() && |
| 350 | + PreprocessorUtils.isFileContentEquals(fileRef.getSourceFile(), destinationFile)) { |
| 351 | + doCopy = false; |
| 352 | + if (this.context.isVerbose()) { |
| 353 | + this.context.logForVerbose(String |
| 354 | + .format("Copy skipped because same content: %s -> {dst} %s", |
| 355 | + PreprocessorUtils.getFilePath(fileRef.getSourceFile()), |
| 356 | + fileRef.makeTargetFilePathAsString())); |
| 357 | + } |
378 | 358 | } |
379 | | - } |
380 | 359 |
|
381 | | - if (doCopy) { |
382 | | - if (this.context.isVerbose()) { |
383 | | - this.context.logForVerbose(String.format("Copy file %s -> {dst} %s", |
384 | | - PreprocessorUtils.getFilePath(fileRef.getSourceFile()), |
385 | | - fileRef.makeTargetFilePathAsString())); |
| 360 | + if (doCopy) { |
| 361 | + if (this.context.isVerbose()) { |
| 362 | + this.context.logForVerbose(String.format("Copy file %s -> {dst} %s", |
| 363 | + PreprocessorUtils.getFilePath(fileRef.getSourceFile()), |
| 364 | + fileRef.makeTargetFilePathAsString())); |
| 365 | + } |
| 366 | + PreprocessorUtils.copyFile(fileRef.getSourceFile(), destinationFile, |
| 367 | + this.context.isKeepAttributes()); |
| 368 | + fileRef.getGeneratedResources().add(destinationFile); |
| 369 | + copiedCounter++; |
386 | 370 | } |
387 | | - PreprocessorUtils.copyFile(fileRef.getSourceFile(), destinationFile, |
388 | | - this.context.isKeepAttributes()); |
389 | | - fileRef.getGeneratedResources().add(destinationFile); |
390 | | - copiedCounter++; |
391 | 371 | } |
| 372 | + } else { |
| 373 | + final long startTime = System.currentTimeMillis(); |
| 374 | + fileRef.preprocessFileWithNotification(null, this.context, false); |
| 375 | + final long elapsedTime = System.currentTimeMillis() - startTime; |
| 376 | + if (this.context.isVerbose()) { |
| 377 | + this.context.logForVerbose(String |
| 378 | + .format("File preprocessing completed '%s', elapsed time %d ms", |
| 379 | + PreprocessorUtils.getFilePath(fileRef.getSourceFile()), elapsedTime)); |
| 380 | + } |
| 381 | + preprocessedCounter++; |
392 | 382 | } |
393 | | - } else { |
394 | | - final long startTime = System.currentTimeMillis(); |
395 | | - fileRef.preprocessFile(null, this.context); |
396 | | - final long elapsedTime = System.currentTimeMillis() - startTime; |
397 | | - if (this.context.isVerbose()) { |
398 | | - this.context.logForVerbose(String |
399 | | - .format("File preprocessing completed '%s', elapsed time %d ms", |
400 | | - PreprocessorUtils.getFilePath(fileRef.getSourceFile()), elapsedTime)); |
401 | | - } |
402 | | - preprocessedCounter++; |
403 | 383 | } |
| 384 | + } catch (Throwable err) { |
| 385 | + error = err; |
| 386 | + if (error instanceof IOException) { |
| 387 | + throw (IOException) error; |
| 388 | + } |
| 389 | + if (error instanceof RuntimeException) { |
| 390 | + throw (RuntimeException) error; |
| 391 | + } |
| 392 | + if (error instanceof Error) { |
| 393 | + throw (Error) error; |
| 394 | + } |
| 395 | + } finally { |
| 396 | + context.fireNotificationStop(error); |
404 | 397 | } |
405 | 398 |
|
406 | 399 | return new Statistics( |
|
0 commit comments