001package Torello.Java.Build;
002
003import static Torello.Java.C.*;
004
005import Torello.Java.StringParse;
006import Torello.Java.ReadOnly.ReadOnlyList;
007
008import java.io.IOException;
009import java.util.ArrayList;
010
011/**
012 * A few minor printing utilities.
013 * 
014 * <BR />
015 * <EMBED CLASS='external-html' DATA-FILE-ID=INTERNAL_CLASS_NOTE>
016 */
017@Torello.JavaDoc.StaticFunctional
018public class Printing
019{
020    // Completely irrelevant, and the 'private' modifier keeps it off of JavaDoc
021    private Printing() { }
022
023    static final int LEN = 80;
024
025    private static final String BLANKS = StringParse.nChars(' ', LEN);
026
027    // Used for the dataRecLogSB
028    static final String DATA_REC_LOG_LINES =
029        BRED + StringParse.nChars('=', LEN) + RESET + '\n' +
030        BRED + StringParse.nChars('=', LEN) + RESET + '\n';
031
032    // startStep(int)
033    static final String BEGIN_STAGE_LINE = BRED_BKGND + BLANKS + RESET;
034
035    // Logs
036    static final String LOG_LINE = BCYAN_BKGND + BLANKS + RESET;
037
038    // Timers
039    static final String TIMER_SUMMARY_LINE = BPURPLE_BKGND + BLANKS + RESET;
040
041    // Util.ERROR_EXIT
042    static final int ERROR_EXIT_LEN = LEN + 10;
043
044    static final String ERROR_EXIT_LINE =
045        BBLUE_BKGND + StringParse.nChars(' ', ERROR_EXIT_LEN) + RESET;
046
047    static final String TAR_JAR_DIVIDER =
048        "\n\n" +
049        StringParse.nChars('*', LEN) + '\n' + 
050        StringParse.nChars('*', LEN) + "\n\n";
051
052
053    // These are printed by Timers.PRINT_STEP_TIME(int, boolean) and by Util.startStep(int)
054    static final ReadOnlyList<String> STAGES = ReadOnlyList.of(
055    
056        /* Stage 00 */ "", // Array[0] ==> Not Used
057        /* Stage 01 */ "Run javac",
058        /* Stage 02 */ "Run javadoc",
059        /* Stage 03 */ "Torello.JavaDoc.Upgrade",
060        /* Stage 04 */ "Build TAR & JAR Files",
061        /* Stage 05 */ "Sync javadoc/ Files to GCS",
062        /* Stage 06 */ "Sync TAR & JAR Files to GCS",
063        /* Stage 07 */ "Sync Log Files to GCS",
064        /* Stage 08 */ "Set Cache-Control Max-Age"        
065    );
066
067    private static final String MESSAGE_BEGINNING =
068        '\n' + Printing.BEGIN_STAGE_LINE + "\n" + BRED_BKGND + " " + RESET + " Stage 0";
069
070    private static final String MESSAGE_ENDING =
071        BRED_BKGND + " " + RESET + "\n" + Printing.BEGIN_STAGE_LINE + '\n';
072
073    static void startStep(final int stageNumber)
074    {
075        if ((stageNumber < 1) || (stageNumber > 8)) throw new IllegalArgumentException
076            ("An Illegal Step Number has been passed to method 'startStep(int)'");
077
078        System.out.println(
079            MESSAGE_BEGINNING +
080
081            stageNumber + ": " + Printing.STAGES.get(stageNumber) +
082
083            // "# Stage 0X: ".length() ==> 12
084            // NOTE: The extra '-1' is so that the closing '#' is on the line-end, not after it!
085
086            StringParse.nChars
087                (' ', Printing.LEN - Printing.STAGES.get(stageNumber).length() - 12 - 1) +
088
089            MESSAGE_ENDING
090        );
091    }
092
093    /**
094     * Used elsewhere.  Must be public until further notice.  This will hopefully one day be
095     * relegated to the annals of "Package-Private".
096     */
097    public static void PLS(final Appendable a, final boolean unixColors) throws IOException
098    {
099        a.append(
100            "\n\n" +
101            (unixColors ? BCYAN : "") +
102            "*********************************************************************************\n" +
103            "*********************************************************************************\n" +
104            (unixColors ? RESET : "") +
105            "\n\n"
106        );
107    }
108
109    /**
110     * Prints out a list of file-counts to the screen and to the log.
111     * 
112     * @param packages a list of packages (as instances of class {@link BuildPackage}).  This list
113     * is a parallel data-set to {@code 'filesCount'}.  Each element of {@code 'packages'}
114     * identifies exactly one package within the current build-processes.  In the exact same list
115     * location in {@code 'filesCount'} there is an integer that specifies how many relevant files
116     * are contained in the package at any particular list index.
117     * 
118     * @param filesCount A parallel list to {@code 'packages'}, this parameter contains the file
119     * count for each package listed.
120     */
121    static void printFilesCount(
122            final ReadOnlyList<BuildPackage>    packages,
123            final ArrayList<Integer>            filesCount,
124            final Appendable                    logAndScreen,
125            final boolean                       javaOrClass
126        )
127        throws IOException
128    {
129        double  log10   = -1;
130        double  max     = 0;
131
132        for (Integer c : filesCount)
133            if ((log10 = Math.log10(c)) > max)
134                max = log10;
135
136        final int spacing = 2 + ((int) max);
137
138        int total = 0;
139
140        final String fileType = javaOrClass
141            ? "'.java' Files: "
142            : "'.class' Files: ";
143
144        for (int i=0; i < packages.size(); i++)
145        {
146            final int count = filesCount.get(i);
147
148            total += count;
149
150            logAndScreen.append(
151                BRED + StringParse.rightSpacePad("" + count, spacing) + RESET +
152                    fileType +
153                    packages.get(i).fullName + '\n'
154            );
155        }
156
157        logAndScreen.append("\nTotal " + fileType + BRED + total + RESET + "\n\n");
158    }
159}