Class Utils


  • public final class Utils
    extends java.lang.Object
    This class provides utilities for the whole plugin.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copyFileTree​(java.nio.file.Path sourcePath, java.nio.file.Path targetPath)
      Copies a file at the given path, recursively if it is a directory.
      static void deleteFileTree​(java.nio.file.Path rootPath, boolean deleteRootEnabled)
      Deletes a file or a directory at the given path.
      static java.util.Optional<java.lang.String> getExtension​(java.lang.String fileName)
      Gets the extension of a file.
      static java.util.Optional<java.nio.file.Path> getNodeExecutablePath​(java.nio.file.Path nodeInstallDirectory, java.lang.String osName)
      Gets the path of the Node executable.
      static java.util.Optional<java.nio.file.Path> getNpmExecutablePath​(java.nio.file.Path nodeInstallDirectory, java.lang.String osName)
      Gets the path of the NPM executable.
      static java.lang.String getSystemJvmArch()
      Gets the current JVM architecture.
      static java.lang.String getSystemOsName()
      Gets the current O/S name.
      static java.util.Optional<java.nio.file.Path> getYarnExecutablePath​(java.nio.file.Path yarnInstallDirectory, java.lang.String osName)
      Gets the path of Yarn executable.
      static boolean is64BitsArch​(java.lang.String jreArch)
      Tells whether the given JRE architecture is a 64 bits one.
      static boolean isGzipExtension​(java.lang.String extension)
      Tells whether a file name extension is related to a GZIP file.
      static boolean isLinuxOs​(java.lang.String osName)
      Tells whether the given OS name matches a Linux OS.
      static boolean isMacOs​(java.lang.String osName)
      Tells whether the given OS name matches a Mac OS.
      static boolean isWindowsOs​(java.lang.String osName)
      Tells whether the given OS name matches a Windows OS.
      static void moveFileTree​(java.nio.file.Path sourcePath, java.nio.file.Path targetPath)
      Moves all files/directories from a source directory into a destination directory.
      static java.lang.String removeExtension​(java.lang.String filename)
      Removes the extension of a filename.
      static boolean setFileExecutable​(java.nio.file.Path path, java.lang.String osName)
      Marks the file as executable.
      static boolean setFileExecutable​(java.nio.file.Path path, java.util.Set<java.nio.file.attribute.PosixFilePermission> originalPermissions, java.lang.String osName)
      Marks the file as executable.
      static java.lang.String toHexadecimalString​(byte[] buffer)
      Converts a binary buffer into an hexadecimal string, with a lower case.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • deleteFileTree

        public static void deleteFileTree​(java.nio.file.Path rootPath,
                                          boolean deleteRootEnabled)
                                   throws java.io.IOException
        Deletes a file or a directory at the given path. If the root path is a directory, its content is deleted recursively before the directory itself.
        Parameters:
        rootPath - Root path.
        deleteRootEnabled - Whether the file at the root path itself must also be deleted.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • copyFileTree

        public static void copyFileTree​(java.nio.file.Path sourcePath,
                                        java.nio.file.Path targetPath)
                                 throws java.io.IOException
        Copies a file at the given path, recursively if it is a directory. File attributes and symlinks are preserved.
        Parameters:
        sourcePath - Source path.
        targetPath - Target Path.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • moveFileTree

        public static void moveFileTree​(java.nio.file.Path sourcePath,
                                        java.nio.file.Path targetPath)
                                 throws java.io.IOException
        Moves all files/directories from a source directory into a destination directory. The destination directory is created, and therefore must not exist before this method is called. All directories/files in the source path are copied first, preserving file attributes and symlinks. Finally the source path is deleted. Such method ensures any file tree can be moved from a volume to another.
        Parameters:
        sourcePath - Source path.
        targetPath - Target path.
        Throws:
        java.io.IOException - If an I/O error occurs.
        java.lang.IllegalArgumentException - If either the source directory or the destination directory is not an existing directory.
      • getExtension

        public static java.util.Optional<java.lang.String> getExtension​(java.lang.String fileName)
        Gets the extension of a file.
        Parameters:
        fileName - File name.
        Returns:
        Extension.
      • getNodeExecutablePath

        public static java.util.Optional<java.nio.file.Path> getNodeExecutablePath​(java.nio.file.Path nodeInstallDirectory,
                                                                                   java.lang.String osName)
        Gets the path of the Node executable.
        Parameters:
        nodeInstallDirectory - Node install directory.
        osName - O/S name.
        Returns:
        The path, may be null if it was not found.
        See Also:
        getSystemOsName()
      • getNpmExecutablePath

        public static java.util.Optional<java.nio.file.Path> getNpmExecutablePath​(java.nio.file.Path nodeInstallDirectory,
                                                                                  java.lang.String osName)
        Gets the path of the NPM executable.
        Parameters:
        nodeInstallDirectory - Node install directory.
        osName - O/S name.
        Returns:
        The path, may be null if it was not found.
        See Also:
        getSystemOsName()
      • getSystemJvmArch

        public static java.lang.String getSystemJvmArch()
        Gets the current JVM architecture.
        Returns:
        String describing the JVM architecture.
      • getSystemOsName

        public static java.lang.String getSystemOsName()
        Gets the current O/S name.
        Returns:
        String describing the O/S.
      • getYarnExecutablePath

        public static java.util.Optional<java.nio.file.Path> getYarnExecutablePath​(java.nio.file.Path yarnInstallDirectory,
                                                                                   java.lang.String osName)
        Gets the path of Yarn executable.
        Parameters:
        yarnInstallDirectory - Yarn install directory.
        osName - O/S name.
        Returns:
        The path, may be null if it was not found.
        See Also:
        getSystemOsName()
      • is64BitsArch

        public static boolean is64BitsArch​(java.lang.String jreArch)
        Tells whether the given JRE architecture is a 64 bits one.
        Parameters:
        jreArch - JRE architecture.
        Returns:
        true if the architecture is a 64 bits.
        See Also:
        getSystemJvmArch()
      • isGzipExtension

        public static boolean isGzipExtension​(java.lang.String extension)
        Tells whether a file name extension is related to a GZIP file.
        Parameters:
        extension - Extension
        Returns:
        true if the extension is related to a GZIP file.
      • isLinuxOs

        public static boolean isLinuxOs​(java.lang.String osName)
        Tells whether the given OS name matches a Linux OS.
        Parameters:
        osName - OS name.
        Returns:
        true if the OS name matches a Linux OS.
        See Also:
        getSystemOsName()
      • isMacOs

        public static boolean isMacOs​(java.lang.String osName)
        Tells whether the given OS name matches a Mac OS.
        Parameters:
        osName - OS name.
        Returns:
        true if the OS name matches a Mac OS.
        See Also:
        getSystemOsName()
      • isWindowsOs

        public static boolean isWindowsOs​(java.lang.String osName)
        Tells whether the given OS name matches a Windows OS.
        Parameters:
        osName - OS name.
        Returns:
        true if the OS name matches a Windows OS.
        See Also:
        getSystemOsName()
      • removeExtension

        public static java.lang.String removeExtension​(java.lang.String filename)
        Removes the extension of a filename. In case of a compressed TAR archive, the method removes the whole extension (e.g. '.tar.gz').
        Parameters:
        filename - Filename.
        Returns:
        The filename without the extension.
      • setFileExecutable

        public static boolean setFileExecutable​(java.nio.file.Path path,
                                                java.lang.String osName)
                                         throws java.io.IOException
        Marks the file as executable. This method does nothing under Windows.
        Parameters:
        path - Path.
        osName - OS name.
        Returns:
        true if the file permissions were touched, i.e. under a Non-Windows O/S, the file exists and has not the executable permission yet.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • setFileExecutable

        public static boolean setFileExecutable​(java.nio.file.Path path,
                                                java.util.Set<java.nio.file.attribute.PosixFilePermission> originalPermissions,
                                                java.lang.String osName)
                                         throws java.io.IOException
        Marks the file as executable. This method does nothing under Windows. This method allows to restore a file's permissions by providing the original permissions, in case they cannot be retrieved.
        Parameters:
        path - Path.
        originalPermissions - Original file permissions.
        osName - OS name.
        Returns:
        true if the file permissions were touched, i.e. under a Non-Windows O/S, the file exists and has not the executable permission yet.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • toHexadecimalString

        public static java.lang.String toHexadecimalString​(byte[] buffer)
        Converts a binary buffer into an hexadecimal string, with a lower case.
        Parameters:
        buffer - Buffer.
        Returns:
        Hexadecimal string.