Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Remove files on error

AuthorDaveJarvis <email>
Date2021-05-08 22:11:56 GMT-0700
Commite2da010466e89c5eba447c0d7210139724e47bcc
Parentfbd6210
src/main/java/com/keenwrite/typesetting/Typesetter.java
import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
+import static java.nio.file.Files.deleteIfExists;
import static java.nio.file.Files.newDirectoryStream;
import static java.util.concurrent.TimeUnit.*;
+import static org.apache.commons.io.FilenameUtils.removeExtension;
/**
mArgs.add( "mtx-context" );
mArgs.add( "--batchmode" );
+ mArgs.add( "--nonstopmode" );
mArgs.add( "--purgeall" );
mArgs.add( "--path='" + Path.of( themes.toString(), theme ) + "'" );
final var exit = process.exitValue();
process.destroy();
+
+ // If there was an error, the typesetter will leave behind log, pdf, and
+ // error files.
+ if( exit != 0 ) {
+ final var xmlName = mInput.getFileName().toString();
+ final var srcName = mOutput.getFileName().toString();
+ final var logName = newExtension( xmlName, ".log" );
+ final var errName = newExtension( xmlName, "-error.log" );
+ final var pdfName = newExtension( xmlName, ".pdf" );
+ final var badName = newExtension( srcName, ".log" );
+
+ deleteIfExists( badName );
+ deleteIfExists( logName );
+ deleteIfExists( errName );
+ deleteIfExists( pdfName );
+ }
// Exit value for a successful invocation of the typesetter. This value
// value is returned when creating the cache on the first run as well as
// creating PDFs on subsequent runs (after the cache has been created).
// Users don't care about exit codes, only whether the PDF was generated.
return exit == 0;
+ }
+
+ private Path newExtension( final String baseName, final String ext ) {
+ return mOutput.resolveSibling( removeExtension( baseName ) + ext );
}
/**
- * Answers whether the given directory is empty.
+ * Answers whether the given directory is empty. The typesetting software
+ * creates a non-empty directory by default. The return value from this
+ * method is a proxy to answering whether the typesetter has been run for
+ * the first time or not.
*
* @param path The directory to check for emptiness.
Delta27 lines added, 1 line removed, 26-line increase