001package Torello.Java.Build;
002
003import Torello.JavaDoc.Upgrade;
004import Torello.JavaDoc.Stats;
005import Torello.JavaDoc.Messager.AssertFail;
006
007import Torello.Java.FileRW;
008
009import static Torello.Java.C.BRED;
010import static Torello.Java.C.BRED_BKGND;
011import static Torello.Java.C.BYELLOW;
012import static Torello.Java.C.RESET;
013
014import java.io.File;
015import java.io.IOException;
016
017/**
018 * This is the third Build-Stage, and it runs the Java-Doc {@link Upgrade Upgrader-Tool}
019 * 
020 * <EMBED CLASS=external-html DATA-FILE-ID=STAGE_PRIVATE_NOTE>
021 * <EMBED CLASS='external-html' DATA-FILE-ID=S03_UPGRADE>
022 */
023@Torello.JavaDoc.StaticFunctional
024public class S03_Upgrade
025{
026    // Completely irrelevant, and the 'private' modifier keeps it off of JavaDoc
027    private S03_Upgrade() { }
028
029    public static void upgrade(final BuilderRecord brec) throws IOException
030    {
031        brec.timers.startStage03();
032
033        Printing.startStep(3);
034
035        StringBuilder sb = new StringBuilder();
036
037
038        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
039        // User-Provided Pre-Upgrader Script
040        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
041
042        if (brec.preUpgraderScript != null) TRY_CATCH(
043            () -> brec.preUpgraderScript.accept(brec, sb),
044            "User-Provided Pre-Upgrader Script has Failed"
045        );
046
047
048        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
049        // Main JDU
050        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
051
052        sb.append(
053            '\n' +
054            "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n" +
055            "Invoking Upgrader.upgrade().  Log Content Will not be available here.\n" +
056            "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n" +
057            '\n'
058        );
059
060        Stats result = brec.upgrader.upgrade(); 
061
062        if (result == null)
063        {
064            System.err.println(BRED + "THERE WERE ERRORS, EXITING." + RESET);
065            System.exit(1);
066        }
067
068        if (brec.FAVICON != null)
069
070            try 
071                { FileRW.copyFile(brec.FAVICON, brec.LOCAL_JAVADOC_DIR, false); }
072
073            catch (IOException ioe)
074            {
075                System.err.println(
076                    "Unable to copy Favicon File: [" + brec.FAVICON + "] to javadoc output " +
077                        "directory:\n" +
078                    BYELLOW + brec.LOCAL_JAVADOC_DIR + RESET + '\n' +
079                    "Skipping Favicon..."
080                );
081            }
082
083
084        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
085        // User-Provided Post-Upgrader Script
086        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
087
088        if (brec.postUpgraderScript != null) TRY_CATCH(
089            () -> brec.postUpgraderScript.accept(brec, sb),
090            "User-Provided Post-Upgrader Script has Failed"
091        );
092
093
094        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
095        // Write Logs & Exit
096        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
097
098        brec.logs.write_S03_LOGS(sb.toString());
099        brec.timers.endStage03();
100    }
101
102    @FunctionalInterface
103    private static interface ThrowingRunnable
104    { void run() throws IOException; }
105
106    private static void TRY_CATCH(final ThrowingRunnable r, final String msg)
107        throws IOException
108    {
109        try 
110            { r.run(); }
111
112        catch (AssertFail af)
113        { 
114            System.err.println(
115                BRED_BKGND + ' ' + msg + ' ' + RESET + '\n' +
116                "Exiting...\n"
117            );
118
119            System.exit(-1);
120        }
121    }
122}