1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package Torello.Java.Build;

import Torello.Java.ReadOnly.ReadOnlyList;

import Torello.Java.Shell;
import Torello.Java.GSUTIL;
import Torello.Java.OSResponse;
import Torello.Java.OSExtras;
import Torello.Java.StringParse;

import Torello.Java.Additional.BiAppendable;
import Torello.Java.Additional.AppendableSafe;

import static Torello.Java.C.BYELLOW;
import static Torello.Java.C.BGREEN;
import static Torello.Java.C.RESET;

import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.stream.Stream;

/**
 * This is the fifth Build-Stage, and it is part of the synchronization of a project with Google
 * Cloud Platform Stroage Buckets.  This class relies heavily on the GCP Shell-Command
 * {@code 'gsutil'} and it's Java implementation - {@link GSUTIL Torello.Java.GSUTIL}.
 * 
 * <BR /><BR />This class' synchronization-efforts entail copying the File-System's Java-Doc
 * Directory-Contents onto the (User-Specified) Google Cloud Server Storage-Bucket.
 * 
 * <EMBED CLASS='external-html' DATA-FILE-ID=S05_SYNC_JAVADOC>
 */
@Torello.JavaDoc.StaticFunctional
public class S05_SyncJavaDoc
{
    // Completely irrelevant, and the 'private' modifier keeps it off of JavaDoc
    private S05_SyncJavaDoc() { }

    private static final String FS = java.io.File.separator;


    // ********************************************************************************************
    // ********************************************************************************************
    // Copies entire 'javadoc/' directory-tree to GCS
    // ********************************************************************************************
    // ********************************************************************************************


    public static void sync(Builder builder) throws IOException
    {
        builder.timers.startStage05();
        Printing.startStep(5);

        OSResponse      osr     = null;
        StringBuilder   logOnly = new StringBuilder();

        final AppendableSafe logAndScreen = new AppendableSafe(
            new BiAppendable(System.out, logOnly),
            AppendableSafe.USE_APPENDABLE_ERROR
        );

        // Uses Shell-Contructor:
        // (outputAppendable, commandStrAppendable, standardOutput, errorOutput)

        // GSUTIL gsutil = new GSUTIL(logOnly, logAndScreen, null, null);
        builder.cloudSync.initStage05(logOnly, logAndScreen);
        Shell shell = new Shell(logOnly, logAndScreen, null, null);

        if (! builder.cli.SKIP_REMOVE_GCS_FILES)
        {
            // osr = gsutil.RM(builder.cli.GCS_DIR + "javadoc/" + "**");
            osr = builder.cloudSync.removeCloudJavaDocDir();
            Util.HALT_ON_ERROR(osr);
            Printing.PLS(logOnly, false);
        }

        // TRICKY !!! Do not use 'GCS_JAVADOC_DIR' here - because then it will be
        // 'javadoc/javadoc/!!!

        // osr = gsutil.CP(builder.LOCAL_JAVADOC_DIR, builder.cli.GCS_DIR, "-r");
        osr = builder.cloudSync.copyJavaDocDirToCloudDir();
        Util.HALT_ON_ERROR(osr);
        Printing.PLS(logOnly, false);

        osr = shell.RM(builder.LOCAL_JAVADOC_DIR, "-r");
        Util.HALT_ON_ERROR(osr);
        Printing.PLS(logOnly, false);

        if (builder.RUN_MAKE_PUBLIC)
        {
            Printing.PLS(logOnly, false);
            // osr = gsutil.MP(builder.cli.GCS_DIR + "javadoc/**");
            osr = builder.cloudSync.makePublicJavaDocDir();
            Util.HALT_ON_ERROR(osr);
        }

        builder.logs.write_S05_LOG(logOnly.toString());
        builder.cloudSync.endStage05();
        builder.timers.endStage05();
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Copies only selected packages of the Java-Doc Directory to GCS
    // ********************************************************************************************
    // ********************************************************************************************


    static void syncPart(Builder builder) throws IOException
    {
        builder.timers.startStage05();
        Printing.startStep(5);

        ReadOnlyList<BuildPackage> packageSyncList;

        if (builder.cli.userSpecifiedPackages != null)
        {
            packageSyncList = builder.cli.userSpecifiedPackages;

            for (BuildPackage bp : packageSyncList) if (! bp.mustDocument)
                throw new IllegalArgumentException("Not allowd to document: " + bp.fullName);
        }

        else packageSyncList = builder.packageList;

        OSResponse      osr     = null;
        StringBuilder   logOnly = new StringBuilder();

        final AppendableSafe logAndScreen = new AppendableSafe(
            new BiAppendable(System.out, logOnly),
            AppendableSafe.USE_APPENDABLE_ERROR
        );

        // Uses Shell-Contructor:
        // (outputAppendable, commandStrAppendable, standardOutput, errorOutput)

        Shell       shell   = new Shell(logOnly, logAndScreen, null, null);
        OSExtras    ose     = new OSExtras();

        // GSUTIL gsutil = new GSUTIL(logOnly, logAndScreen, null, null);
        builder.cloudSync.initStage05(logOnly, logAndScreen);

        Stream.Builder<String> aclCommandB = Stream.builder();
        Vector<String> copyDirs = new Vector<>(4);

        for (BuildPackage bp : packageSyncList)
        {
            final String PN_FS =
                builder.LOCAL_JAVADOC_DIR + bp.fullName.replace(".", FS) + FS;

            final String PN_GCS =
                builder.cli.GCS_DIR + "javadoc/" + bp.fullName.replace('.', '/') + '/';

            logAndScreen.append("\nCopying Package HTML: " + BYELLOW + bp.fullName + RESET + '\n');
            // osr = gsutil.CP(PN_FS + '*', PN_GCS);
            osr = builder.cloudSync.copySingleJDPackageToCloud(PN_FS, PN_GCS);

            Util.HALT_ON_ERROR(osr);

            // The GSUTIL.MP (Make-Public) Command is called once at the very end.
            aclCommandB.accept(PN_GCS + '*');

            // Empty out the previous calls stuff
            copyDirs.clear();

            if (new java.io.File(PN_FS + "doc-files" + FS).exists())
            {
                copyDirs.add("doc-files" + FS);
                aclCommandB.accept(PN_GCS + "doc-files" + FS + "**");
            }

            if (new java.io.File(PN_FS + "hilite-files" + FS).exists())
            {
                copyDirs.add("hilite-files" + FS);
                aclCommandB.accept(PN_GCS + "hilite-files" + FS + "**");
            }

            if (new java.io.File(PN_FS + "stylesheets" + FS).exists())
            {
                copyDirs.add("stylesheets" + FS);
                aclCommandB.accept(PN_GCS + "stylesheets" + FS + "**");
            }

            ose.currentWorkingDirectory = new File(PN_FS);
            // gsutil.osExtras = ose;

            logAndScreen.append(
                "\nOSExtras.currentWorkingDirectory was assigned:  " +
                BYELLOW + PN_FS + RESET + '\n'
            );

            // osr = gsutil.CP(copyDirs, PN_GCS, "-r");
            builder.cloudSync.copyOtherPackageDirsToCloudDir(ose, copyDirs, PN_GCS);
            Util.HALT_ON_ERROR(osr);
        }

        String[] makePublicArr = aclCommandB.build().toArray(String[]::new);

        // My Cute little hack for the day.  This will make Stage 8 a lot faster.
        // Well, this isn't considered a hack anymore, because it works pretty well.
        //
        // NOTE: Even if "Make-Public" isn't executed on a directory because that GCP Bucket
        //       directory has been assigned "Bucket-Level Permissions", it will stll be necessary
        //       to run the "Set Max Age" (Stage 8 Stuff), if the user has requested it!  Therefore
        //       we cannot just 'discard' the 'makePublicArr' when RUN_MAKE_PUBLIC is false.

        builder.stage8GCSDirs = makePublicArr;

        // REMEMBER: if this is ever invoked on a bucket that has bucket-level
        // permissions, instead of object level permissions, THIS WOULD CRASH.
        //
        // NOTE: the content of the 'aclCommandB' Stream.Builder are just wholly ignored and
        //       discarded (eventually), by the garbage-collector if the Storage-Bucket has been
        //       assigned Bucket-Level permissions instead of Object-Level Permissions.

        if (builder.RUN_MAKE_PUBLIC)
        {
            // Print these so they are legible
            logAndScreen.append(BGREEN + "\nMake Copied Files Public:\n\n" + RESET);
            for (String gcsDir : makePublicArr) logAndScreen.append('\t' + gcsDir + "\n");
            logAndScreen.append('\n');

            // Now do the actual "Make Public" Command
            // osr = gsutil.MP(makePublicArr);
            osr = builder.cloudSync.makePublicDirArr(makePublicArr);
            Util.HALT_ON_ERROR(osr);
        }

        logAndScreen.append(BGREEN + "\nClean Up:\n" + RESET);
        osr = shell.RM(builder.LOCAL_JAVADOC_DIR, "-r");
        Util.HALT_ON_ERROR(osr);

        builder.logs.write_S05_LOG(logOnly.toString());
        builder.cloudSync.endStage05();
        builder.timers.endStage05();
    }
}