Class Utils


  • public class Utils
    extends Object
    • Constructor Detail

      • Utils

        public Utils()
    • Method Detail

      • getConfiguredAndroidHome

        public static String getConfiguredAndroidHome()
        Retrieves the configured Android SDK root directory.
        Returns:
        The configured Android SDK root, if any. May include un-expanded variables.
      • getEnvironment

        public static EnvVars getEnvironment​(AbstractBuild<?,​?> build,
                                             BuildListener listener)
        Gets a combined set of environment variables for the current computer and build.
        Parameters:
        build - The build for which we should retrieve environment variables.
        listener - The listener used to get the environment variables.
        Returns:
        Environment variables for the current computer, with the build variables taking precedence.
      • getAndroidSdk

        public static AndroidSdk getAndroidSdk​(Launcher launcher,
                                               String androidSdkRootPreferred,
                                               String androidSdkHome)
        Tries to validate the given Android SDK root directory.
        Parameters:
        launcher - The launcher for the remote node.
        androidSdkRootPreferred - The preferred SDK root directory. Normally the (variable-expanded) SDK root directory specified in the job/system configuration.
        androidSdkHome - The SDK home directory, i.e. the workspace directory.
        Returns:
        AndroidSdk object representing the properties of the installed SDK, or null if no valid directory is found.
      • getAndroidSdk

        public static AndroidSdk getAndroidSdk​(Launcher launcher,
                                               Node node,
                                               EnvVars envVars,
                                               String androidSdkRootPreferred,
                                               String androidSdkHome)
        Tries to validate the given Android SDK root directory; otherwise tries to locate a copy of the SDK by checking for the auto-install directory and for common environment variables.
        Parameters:
        launcher - The launcher for the remote node.
        node - Current node
        envVars - Environment variables for the build.
        androidSdkRootPreferred - The preferred SDK root directory. Normally the (variable-expanded) SDK root directory specified in the job/system configuration.
        androidSdkHome - The SDK home directory, i.e. the workspace directory.
        Returns:
        AndroidSdk object representing the properties of the installed SDK, or null if no valid directory is found.
      • getAndroidSdk

        public static AndroidSdk getAndroidSdk​(Launcher launcher,
                                               Node node,
                                               EnvVars envVars,
                                               boolean checkPreferredOnly,
                                               String androidSdkRootPreferred,
                                               String androidSdkHome)
        Tries to validate the given Android SDK root directory; otherwise tries to locate a copy of the SDK by checking for the auto-install directory and for common environment variables.
        Parameters:
        launcher - The launcher for the remote node.
        node - Current node
        envVars - Environment variables for the build.
        checkPreferredOnly - just check preferred directory, do not try to determine other SDK roots by evaluating auto-install directory or common environment variables.
        androidSdkRootPreferred - The preferred SDK root directory. Normally the (variable-expanded) SDK root directory specified in the job/system configuration.
        androidSdkHome - The SDK home directory, i.e. the workspace directory.
        Returns:
        AndroidSdk object representing the properties of the installed SDK, or null if no valid directory is found.
      • areAllSubdirectoriesExistant

        public static boolean areAllSubdirectoriesExistant​(File root,
                                                           String[] subDirectories)
        Check if a root directory contains all the given subDirectories. If a single subDirectory does not exist, false is returned.
        Parameters:
        root - the root-directory which needs to hold the subDirectories
        subDirectories - the names of the subDirectories to check for existence
        Returns:
        true if all subDirectories exist or empty, false otherwise
      • areAllFilesExistantInDir

        public static boolean areAllFilesExistantInDir​(File root,
                                                       String[] relativeFilePaths)
        Check if a root directory contains all the given files. If a single file is not found false is returned.
        Parameters:
        root - the root-directory which needs to hold the subDirectories
        relativeFilePaths - the names of the files to check for existence
        Returns:
        true if all files exist or empty, false otherwise
      • validateAndroidHome

        public static ValidationResult validateAndroidHome​(File sdkRoot,
                                                           boolean allowLegacy,
                                                           boolean fromWebConfig)
        Validates whether the given directory looks like a valid Android SDK directory.
        Parameters:
        sdkRoot - The directory to validate.
        allowLegacy - Whether the legacy SDK Tools layout is considered valid
        fromWebConfig - Whether we are being called from the web config and should be more lax.
        Returns:
        Whether the SDK looks valid or not (or a warning if the SDK install is incomplete).
      • getAndroidSdkHomeDirectory

        public static File getAndroidSdkHomeDirectory​(String androidSdkHome)
        Locates the Android SDK home directory using the same scheme as the Android SDK does.
        Parameters:
        androidSdkHome -
        Returns:
        A File representing the directory in which the ".android" subdirectory should go.
      • getHomeDirectory

        public static File getHomeDirectory()
        Locates the current user's home directory using the same scheme as the Android SDK does.
        Returns:
        A File representing the home directory.
      • getSdkInstallDirectory

        public static final FilePath getSdkInstallDirectory​(Node node)
        Retrieves the path at which the Android SDK should be installed on the current node.
        Parameters:
        node -
        Returns:
        Path within the tools folder where the SDK should live.
      • getEnvironmentVarsFromEmulatorArgs

        public static EnvVars getEnvironmentVarsFromEmulatorArgs​(String commandLineOptions)
        Parse the given command-line and return the appropriate environment variables if known options are found. Currently this method is only used to workaround Android Emulator Bug 64356053, where the '-no-audio', '-noaudio', '-audio none' option does not work for the qemu2-emulator. If one of the options is found the environment variable 'QEMU_AUDIO_DRV=none' is set.
        Parameters:
        commandLineOptions - CLI-parameters to parse
        Returns:
        EnvVars (Map) of additional environment variables based on given commandLine, empty if no recognized option was found or parameters is null
      • getToolCommand

        public static ArgumentListBuilder getToolCommand​(AndroidSdk androidSdk,
                                                         boolean isUnix,
                                                         SdkCliCommand sdkCmd)
        Generates a ready-to-use ArgumentListBuilder for one of the Android SDK tools.
        Parameters:
        androidSdk - The Android SDK to use.
        isUnix - Whether the system where this command should run is sane.
        sdkCmd - The Android tool and any extra arguments for the command to run.
        Returns:
        Arguments including the full path to the SDK and any extra Windows stuff required.
      • runAndroidTool

        public static void runAndroidTool​(Launcher launcher,
                                          OutputStream stdout,
                                          OutputStream stderr,
                                          AndroidSdk androidSdk,
                                          SdkCliCommand sdkCmd,
                                          FilePath workingDirectory)
                                   throws IOException,
                                          InterruptedException
        Runs an Android tool on the remote build node and waits for completion before returning.
        Parameters:
        launcher - The launcher for the remote node.
        stdout - The stream to which standard output should be redirected.
        stderr - The stream to which standard error should be redirected.
        androidSdk - The Android SDK to use.
        sdkCmd - The Android tool and any extra arguments for the command to run.
        workingDirectory - The directory to run the tool from, or null if irrelevant
        Throws:
        IOException - If execution of the tool fails.
        InterruptedException - If execution of the tool is interrupted.
      • expandVariables

        public static String expandVariables​(AbstractBuild<?,​?> build,
                                             BuildListener listener,
                                             String token)
        Expands the variable in the given string to its value in the environment variables available to this build. The Jenkins-specific build variables for this build are then substituted.
        Parameters:
        build - The build from which to get the build-specific and environment variables.
        listener - The listener used to get the environment variables.
        token - The token which may or may not contain variables in the format ${foo}.
        Returns:
        The given token, with applicable variable expansions done.
      • expandVariables

        public static String expandVariables​(EnvVars envVars,
                                             Map<String,​String> buildVars,
                                             String token)
        Expands the variable in the given string to its value in the variables available to this build. The Jenkins-specific build variables take precedence over environment variables.
        Parameters:
        envVars - Map of the environment variables.
        buildVars - Map of the build-specific variables.
        token - The token which may or may not contain variables in the format ${foo}.
        Returns:
        The given token, with applicable variable expansions done.
      • killProcess

        public static boolean killProcess​(Proc process,
                                          int timeoutMs)
        Attempts to kill the given process, timing-out after timeoutMs.
        Parameters:
        process - The process to kill.
        timeoutMs - How long to wait for before cancelling the attempt to kill the process.
        Returns:
        true if the process was killed successfully.
      • sendEmulatorCommand

        public static boolean sendEmulatorCommand​(Launcher launcher,
                                                  PrintStream logger,
                                                  int port,
                                                  String command,
                                                  int timeoutMs)
        Sends a user command to the running emulator via its telnet interface.
        Execution will be cancelled if it takes longer than timeoutMs.
        Parameters:
        logger - The build logger.
        launcher - The launcher for the remote node.
        port - The emulator's telnet port.
        command - The command to execute on the emulator's telnet interface.
        timeoutMs - How long to wait (in ms) for the command to complete before cancelling it.
        Returns:
        Whether sending the command succeeded.
      • getRelativePath

        public static String getRelativePath​(String from,
                                             String to)
        Determines the relative path required to get from one path to another.
        Parameters:
        from - Path to go from.
        to - Path to reach.
        Returns:
        The relative path between the two, or null for invalid input.
      • getRelativePathDistance

        public static int getRelativePathDistance​(String from,
                                                  String to)
        Determines the number of steps required to get between two paths.

        e.g. To get from "/foo/bar/baz" to "/foo/blah" requires making three steps:

        • "/foo/bar"
        • "/foo"
        • "/foo/blah"
        Parameters:
        from - Path to go from.
        to - Path to reach.
        Returns:
        The relative distance between the two, or -1 for invalid input.
      • normalizePathSeparators

        public static String normalizePathSeparators​(String path)
        Reduce multi-slash and multi-backslash to single characters, but keeping double backslash in the beginning to keep UNC paths.
        Parameters:
        path - the path to normalize
        Returns:
        normalized path without double slash/backslash
      • isVersionOlderThan

        public static boolean isVersionOlderThan​(String strVersion,
                                                 String strVersionToCompare)
        Checks whether the version number string represented by the first parameter is older then the version number string represented by the second parameter. For comparison the utility class VersionNumber is used.
        Parameters:
        strVersion - the version number to check if older then strVersionToCompare
        strVersionToCompare - the version number where strVersion is compared to
        Returns:
        true if VersionNumber representation of strVersion is older then VersionNumber representation of strVersionToCompare
      • getPatternWithHighestSuffixedVersionNumberInMultiLineInput

        public static String getPatternWithHighestSuffixedVersionNumberInMultiLineInput​(String multiLine,
                                                                                        String pattern)
        Looks up the input for the given pattern with an attached version number. The pattern with the highest version found is returned. The delimiter between pattern and version may be ';' or '-'. No RC or beta versions are supported, those versions with an additional suffix will be ignored
        Parameters:
        multiLine - multi-line input string to look up pattern + version
        pattern - the pattern to look for
        Returns:
        The pattern found with the highest version number, or null if pattern is not found
      • equalsVersion

        public static boolean equalsVersion​(String strVersionA,
                                            String strVersionB,
                                            int partsToCompare)
        Compares one given string representing a version number ("[:digit:]+(\.[:digit:]+)*") to another one and checks for equality. Additionally the number of parts to compare can be given, this allows comparing only eg: the major and minor numbers. The version number "1.0.0" would match "1.0.1" if partsToCompare would be 2.
        Parameters:
        strVersionA - version number to compare against strVersionB
        strVersionB - version number to compare against strVersionA
        partsToCompare - if > 0 then the number of parts for that version number are compared, if <= 0 the complete version number is compared
        Returns:
        true if the versions number (or if requested parts of the version numbers) are identical, false otherwise
      • isProcessAlive

        public static boolean isProcessAlive​(Process process)
        Checks if java.lang.Process is still alive. Native isAlive method exists since Java 8 API.
        Parameters:
        process - Process to check
        Returns:
        true if process is alive, false if process has exited