From 677994bb67ad995fea0ffcad5b9c78afaec458c2 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Mon, 30 Oct 2023 21:36:09 -0700 Subject: [PATCH] Added logging of archive file size --- .../bld/extension/AbstractBootOperation.java | 43 +++++++++++++------ .../rife/bld/extension/BootJarOperation.java | 3 +- .../rife/bld/extension/BootWarOperation.java | 3 +- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main/java/rife/bld/extension/AbstractBootOperation.java b/src/main/java/rife/bld/extension/AbstractBootOperation.java index d486f4c..df4bb43 100644 --- a/src/main/java/rife/bld/extension/AbstractBootOperation.java +++ b/src/main/java/rife/bld/extension/AbstractBootOperation.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.nio.file.Files; +import java.text.DecimalFormat; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collection; @@ -57,7 +58,7 @@ public abstract class AbstractBootOperation> * * @param directory the directory to delete */ - public void deleteDirectories(File... directory) throws FileUtilsErrorException { + public static void deleteDirectories(File... directory) throws FileUtilsErrorException { for (var d : directory) { if (d.exists()) { FileUtils.deleteDirectory(d); @@ -65,6 +66,34 @@ public abstract class AbstractBootOperation> } } + /** + * Return the given file size in bytes, kilobytes, megabytes, gigabytes or terabytes. + * + * @param file the file + * @return the file size in B, KB, MB, GB, or TB. + */ + public static String fileSize(File file) { + var size = file.length(); + if (size <= 0) { + return "0 B"; + } + var units = new String[]{"B", "KB", "MB", "GB", "TB"}; + var digitGroups = (int) (Math.log10(size) / Math.log10(1024)); + return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + + ' ' + units[digitGroups]; + } + + /** + * Makes a directory for the given path, including any necessary but nonexistent parent directories. + * + * @param path the directory path + */ + public static void mkDirs(File path) throws IOException { + if (!path.exists() && !path.mkdirs()) { + throw new IOException("ERROR: unable to create: " + path.getAbsolutePath()); + } + } + /** * Retrieves the destination directory in which the JAR will be created. * @@ -376,17 +405,6 @@ public abstract class AbstractBootOperation> return (T) this; } - /** - * Makes a directory for the given path, including any necessary but nonexistent parent directories. - * - * @param path the directory path - */ - protected void mkDirs(File path) throws IOException { - if (!path.exists() && !path.mkdirs()) { - throw new IOException("ERROR: unable to create: " + path.getAbsolutePath()); - } - } - /** * Provides source directories that will be used for the jar archive creation. * @@ -414,6 +432,7 @@ public abstract class AbstractBootOperation> * * @return {@code true} or an {@link IllegalArgumentException} */ + @SuppressWarnings("SameReturnValue") protected boolean verifyExecute() throws IllegalArgumentException { if (mainClass() == null) { throw new IllegalArgumentException("ERROR: project mainClass required."); diff --git a/src/main/java/rife/bld/extension/BootJarOperation.java b/src/main/java/rife/bld/extension/BootJarOperation.java index ae5aed7..72d6361 100644 --- a/src/main/java/rife/bld/extension/BootJarOperation.java +++ b/src/main/java/rife/bld/extension/BootJarOperation.java @@ -53,7 +53,8 @@ public class BootJarOperation extends AbstractBootOperation { var archive = executeCreateArchive(staging_dir); if (!silent() && LOGGER.isLoggable(Level.INFO)) { - LOGGER.info("The executable JAR was created: " + archive.getAbsolutePath()); + LOGGER.info(String.format("The executable JAR was created: %s (%s)", archive.getAbsolutePath(), + fileSize(archive))); } } finally { FileUtils.deleteDirectory(staging_dir); diff --git a/src/main/java/rife/bld/extension/BootWarOperation.java b/src/main/java/rife/bld/extension/BootWarOperation.java index 1a73de0..2b05d0f 100644 --- a/src/main/java/rife/bld/extension/BootWarOperation.java +++ b/src/main/java/rife/bld/extension/BootWarOperation.java @@ -57,7 +57,8 @@ public class BootWarOperation extends AbstractBootOperation { var archive = executeCreateArchive(staging_dir); if (!silent() && LOGGER.isLoggable(Level.INFO)) { - LOGGER.info("The executable WAR was created: " + archive.getAbsolutePath()); + LOGGER.info(String.format("The executable WAR was created: %s (%s)", archive.getAbsolutePath(), + fileSize(archive))); } } finally { FileUtils.deleteDirectory(staging_dir);