001package Torello.Java.Build;
002
003import static Torello.Java.C.*;
004
005import Torello.Java.StringParse;
006import Torello.Java.OSResponse;
007import java.io.IOException;
008
009/**
010 * If you are writing a personalized instance of the {@code FunctionalInterface} named
011 * {@link UpgradeProcessor}, then there is some possibility that you might need to use these
012 * two helper functions.
013 */
014@Torello.JavaDoc.StaticFunctional
015public class Util
016{
017    // Completely irrelevant, and the 'private' modifier keeps it off of JavaDoc
018    private Util() { }
019
020    /**
021     * Simple Method for exiting with a consistent error message.
022     * 
023     * <BR /><BR /><B CLASS=JDDescLabel>Terminates the JVM</B>
024     * 
025     * <BR />This method invokes the Java-Command {@code 'System.exit'}
026     * 
027     * @param cmd The Command that was executed, leading to the error.
028     */
029    public static void ERROR_EXIT(final String cmd)
030    {
031        final String s =
032            BBLUE_BKGND + ' ' + RESET + " There was an error, and build is halting: " +
033            BRED + cmd + RESET;
034
035        final int len = s.length() - (BBLUE_BKGND + RESET + BRED + RESET).length();
036
037        System.err.println(
038            Printing.ERROR_EXIT_LINE + '\n' +
039
040            s + StringParse.nChars(' ', Printing.ERROR_EXIT_LEN - len - 1) +
041                BBLUE_BKGND + ' ' + RESET + '\n' +
042
043            Printing.ERROR_EXIT_LINE + '\n'
044        );
045
046        System.exit(0);
047    }
048
049    /**
050     * Checks a Command's Response for errors.  If Errors have occured, the response that was 
051     * returned is printed, and an {@code Error} is thrown.
052     * 
053     * @param osr The Command's Response
054     * @throws BuildError If an error has occured in executing an Operating System call.
055     */
056    public static void HALT_ON_ERROR(final OSResponse osr)
057    {
058        if (    (osr.response != 0)
059            // NOTE: You cannot do this check, because 'gsutil' literally writes all of its 
060            //       output to standard-error.  It's quite madening.
061            // ||  ((osr.stdErr != null) && (osr.stdErr.length() > 0))
062        )
063        {
064            System.out.println('\n' + osr.toString() + '\n');
065
066            throw new BuildError("OSResponse Had Errors, Look at StackTrace to See Where !");
067        }
068    }
069}