From 219645e2c453d49d08b1d6de2e16ca43009f040f Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Wed, 1 Nov 2023 09:31:11 -0700 Subject: [PATCH] Added utilities class --- .../bld/extension/AbstractBootOperation.java | 52 ++----------- .../rife/bld/extension/BootJarOperation.java | 4 +- .../java/rife/bld/extension/BootUtils.java | 77 +++++++++++++++++++ .../rife/bld/extension/BootWarOperation.java | 6 +- .../bld/extension/BootJarOperationTest.java | 3 + 5 files changed, 90 insertions(+), 52 deletions(-) create mode 100644 src/main/java/rife/bld/extension/BootUtils.java diff --git a/src/main/java/rife/bld/extension/AbstractBootOperation.java b/src/main/java/rife/bld/extension/AbstractBootOperation.java index 1080f3b..122e99a 100644 --- a/src/main/java/rife/bld/extension/AbstractBootOperation.java +++ b/src/main/java/rife/bld/extension/AbstractBootOperation.java @@ -26,7 +26,6 @@ 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; @@ -54,47 +53,6 @@ public abstract class AbstractBootOperation> private String launcherClass_; private String mainClass_; - /** - * Deletes the given directory. - * - * @param directory the directory to delete - */ - public static void deleteDirectories(File... directory) throws FileUtilsErrorException { - for (var d : directory) { - if (d.exists()) { - FileUtils.deleteDirectory(d); - } - } - } - - /** - * Calculates 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("Unable to create: " + path.getAbsolutePath()); - } - } - /** * Retrieves the destination directory in which the archive will be created. * @@ -112,7 +70,7 @@ public abstract class AbstractBootOperation> */ public T destinationDirectory(File directory) throws IOException { destinationDirectory_ = directory; - mkDirs(destinationDirectory_); + BootUtils.mkDirs(destinationDirectory_); //noinspection unchecked return (T) this; } @@ -169,7 +127,7 @@ public abstract class AbstractBootOperation> */ protected void executeCopyInfClassesFiles(File stagingInfDirectory) throws IOException { var inf_classes_dir = new File(stagingInfDirectory, "classes"); - mkDirs(inf_classes_dir); + BootUtils.mkDirs(inf_classes_dir); for (var dir : sourceDirectories()) { if (dir.exists()) { @@ -179,7 +137,7 @@ public abstract class AbstractBootOperation> } } - deleteDirectories(new File(inf_classes_dir, "resources"), new File(inf_classes_dir, "templates")); + BootUtils.deleteDirectories(new File(inf_classes_dir, "resources"), new File(inf_classes_dir, "templates")); } /** @@ -189,7 +147,7 @@ public abstract class AbstractBootOperation> */ protected void executeCopyInfLibs(File stagingInfDirectory) throws IOException { var inf_lib_dir = new File(stagingInfDirectory, "lib"); - mkDirs(inf_lib_dir); + BootUtils.mkDirs(inf_lib_dir); for (var jar : infLibs_) { if (jar.exists()) { @@ -258,7 +216,7 @@ public abstract class AbstractBootOperation> */ protected void executeCreateManifest(File stagingDirectory) throws IOException { var meta_inf_dir = new File(stagingDirectory, "META-INF"); - mkDirs(meta_inf_dir); + BootUtils.mkDirs(meta_inf_dir); var manifest = new File(meta_inf_dir, "MANIFEST.MF").toPath(); diff --git a/src/main/java/rife/bld/extension/BootJarOperation.java b/src/main/java/rife/bld/extension/BootJarOperation.java index 40f3dd8..c39565d 100644 --- a/src/main/java/rife/bld/extension/BootJarOperation.java +++ b/src/main/java/rife/bld/extension/BootJarOperation.java @@ -54,7 +54,7 @@ public class BootJarOperation extends AbstractBootOperation { if (!silent() && LOGGER.isLoggable(Level.INFO)) { LOGGER.info(String.format("The executable JAR was created: %s (%s)", archive.getAbsolutePath(), - fileSize(archive))); + BootUtils.fileSize(archive))); } } finally { FileUtils.deleteDirectory(staging_dir); @@ -69,7 +69,7 @@ public class BootJarOperation extends AbstractBootOperation { */ protected File executeCreateBootInfDirectory(File stagingDirectory) throws IOException { var boot_inf = new File(stagingDirectory, "BOOT-INF"); - mkDirs(boot_inf); + BootUtils.mkDirs(boot_inf); return boot_inf; } diff --git a/src/main/java/rife/bld/extension/BootUtils.java b/src/main/java/rife/bld/extension/BootUtils.java new file mode 100644 index 0000000..c51f532 --- /dev/null +++ b/src/main/java/rife/bld/extension/BootUtils.java @@ -0,0 +1,77 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package rife.bld.extension; + +import rife.tools.FileUtils; +import rife.tools.exceptions.FileUtilsErrorException; + +import java.io.File; +import java.io.IOException; +import java.text.DecimalFormat; + +/** + * Collection of utility-type methods used by {@link AbstractBootOperation Spring Boot operations}. + * + * @author Erik C. Thauvin + * @since 1.0 + */ +public final class BootUtils { + private BootUtils() { + // no-op + } + + /** + * Deletes the given directory. + * + * @param directory the directory to delete + */ + public static void deleteDirectories(File... directory) throws FileUtilsErrorException { + for (var d : directory) { + if (d.exists()) { + FileUtils.deleteDirectory(d); + } + } + } + + /** + * Calculates 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("Unable to create: " + path.getAbsolutePath()); + } + } +} diff --git a/src/main/java/rife/bld/extension/BootWarOperation.java b/src/main/java/rife/bld/extension/BootWarOperation.java index 85d9ad6..c6b7883 100644 --- a/src/main/java/rife/bld/extension/BootWarOperation.java +++ b/src/main/java/rife/bld/extension/BootWarOperation.java @@ -58,7 +58,7 @@ public class BootWarOperation extends AbstractBootOperation { if (!silent() && LOGGER.isLoggable(Level.INFO)) { LOGGER.info(String.format("The executable WAR was created: %s (%s)", archive.getAbsolutePath(), - fileSize(archive))); + BootUtils.fileSize(archive))); } } finally { FileUtils.deleteDirectory(staging_dir); @@ -72,7 +72,7 @@ public class BootWarOperation extends AbstractBootOperation { */ protected void executeCopyWebInfProvidedLib(File stagingWebInfDirectory) throws IOException { var lib_provided_dir = new File(stagingWebInfDirectory, "lib-provided"); - mkDirs(lib_provided_dir); + BootUtils.mkDirs(lib_provided_dir); for (var jar : providedLibs_) { if (jar.exists()) { @@ -90,7 +90,7 @@ public class BootWarOperation extends AbstractBootOperation { */ protected File executeCreateWebInfDirectory(File stagingDirectory) throws IOException { var boot_inf = new File(stagingDirectory, "WEB-INF"); - mkDirs(boot_inf); + BootUtils.mkDirs(boot_inf); return boot_inf; } diff --git a/src/test/java/rife/bld/extension/BootJarOperationTest.java b/src/test/java/rife/bld/extension/BootJarOperationTest.java index 3662ade..c798c34 100644 --- a/src/test/java/rife/bld/extension/BootJarOperationTest.java +++ b/src/test/java/rife/bld/extension/BootJarOperationTest.java @@ -182,6 +182,7 @@ class BootJarOperationTest { "BOOT-INF/classes/rife/bld/extension/AbstractBootOperation.class\n" + "BOOT-INF/classes/rife/bld/extension/BootJarOperation.class\n" + "BOOT-INF/classes/rife/bld/extension/BootManifestAttribute.class\n" + + "BOOT-INF/classes/rife/bld/extension/BootUtils.class\n" + "BOOT-INF/classes/rife/bld/extension/BootWarOperation.class\n" + "BOOT-INF/lib/\n" + "BOOT-INF/lib/" + SPRING_BOOT + '\n' + @@ -216,6 +217,7 @@ class BootJarOperationTest { "BOOT-INF/classes/rife/bld/extension/AbstractBootOperation.class\n" + "BOOT-INF/classes/rife/bld/extension/BootJarOperation.class\n" + "BOOT-INF/classes/rife/bld/extension/BootManifestAttribute.class\n" + + "BOOT-INF/classes/rife/bld/extension/BootUtils.class\n" + "BOOT-INF/classes/rife/bld/extension/BootWarOperation.class\n" + "BOOT-INF/lib/\n" + "BOOT-INF/lib/" + BLD + '\n' + @@ -282,6 +284,7 @@ class BootJarOperationTest { "WEB-INF/classes/rife/bld/extension/AbstractBootOperation.class\n" + "WEB-INF/classes/rife/bld/extension/BootJarOperation.class\n" + "WEB-INF/classes/rife/bld/extension/BootManifestAttribute.class\n" + + "WEB-INF/classes/rife/bld/extension/BootUtils.class\n" + "WEB-INF/classes/rife/bld/extension/BootWarOperation.class\n" + "WEB-INF/lib/\n" + "WEB-INF/lib/" + BLD + '\n' +