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