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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package Torello.Java.Build;

import Torello.Java.ReadOnly.ReadOnlyList;
import Torello.Java.ReadOnly.ROHashMapBuilder;
import Torello.Java.ReadOnly.ROArrayListBuilder;

import Torello.Java.Additional.Ret4;
import Torello.Java.Additional.ByRef;

import Torello.Java.UnreachableError;

import Apache.CLI.Option;
import Apache.CLI.Options;
import Apache.CLI.OptionGroup;

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

import java.io.File;

// This generates an "OptionGroup" that contains all of the choices for the Main-Menu
// Exactly one of these options must be chosen by the user.  The user must provide exactly one
// option from the main OptionsGroup at the Command-Line (String[] argv)
//
// If more than one, or zero of the Main OptionsGroup are provided, class CLI's Constructor will
// throw an exception when processing the End-User-Provided String[] argv.
//
// The Main Options-Group Includes Options for:
//  * Each of the 8 Individual Build-Steps - individually, by themselves
//  * Complete-Build Composite-Steps.  These perform steps 1 through 7 (or 1 through 8)
//  * Partial/Developer-Build Composite-Steps.  These perform a subset of steps 1 - 8
//
// This class "GenerateOptions" simply creates/constructs the needed instances of the Apache-CLI
// classes, and inserts them into the Options and OptionsGroup classes.
// 
// This is done by this class' "generate(cloudSyncList)" method.
// 
// This class "generate(cloudSyncList)" method returns an instance of Class "OptionsRecord".
// OptionsRecord includes the necessary Apache-CLI Option Group instances, along with a few pieces
// of information that have been extracted along the way (within the "generate" method).

class Generate
{
    static
        Ret4<Options, OptionGroup, CollatedOptsRecord, PrintHelpRecord>
        generate(ReadOnlyList<CloudSync> cloudSyncList)
    {
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Main-Menu Top-Level OptionGroup - User must select precisely one of these options
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        final OptionGroup group = new OptionGroup();

        group.setRequired(true);

    
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Instantiate "Helper Lists" and a "Helper Map"
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // 
        // These three simple Data-Structures are used to help "interpret" the "User's User-Input"
        // The "Read-Only-Builder" Construct is used again here, since these variables are private
        // and never make it out of this method.  Only the ReadOnlyList's and ReadOnlyMap are
        // returned from this method (inside of the Record that it generates, the Builder's are
        // collected by the Garbage-Collector when the method exits, obviously)
        //
        // This method keeps a list of all menu options which would invoke "Stage 8".  This is a
        // little tricky because of how the class Builder actually handles the selected
        // "MENU_OPTION" that the User's User has chosen.  Stage-8 is the "extra step/stage" that
        // allows a user to request the "No Browser Cache" flag into all of the '.html' files -
        // so that the browser doesn't cache the javadoc files!  (This makes it easy to check &
        // update your java-code with-out having to constantly drill-down the browser menu to find
        // the "Clear Browser Cache" button inside of Google Chrome.  Besides, most people like
        // the crap Google saves inside of Chrome).
        // 
        // ========================================================================================
        // 
        // Remember, the "really difficult" part of this Build-Code is that the term "The User"
        // essentially refers to someone who is using Torello.Java.Build to build his program.
        // 
        // But (and, I guess, this is almost always the same person - although in a different
        // contenxt), the person invoking the Build-Code from the Command-Line is theoretically
        // "The User's Users".  It is difficult to think about all of the configurations that are
        // exported by Torello.Java.Build via the Classes: Config, JarInclude & Builder facilitate
        // running "a Build" from the Command-Line - AND THAT EXECUTING THAT BUILD FROM THE COMMAND
        // LINE IS "THEORETICALLY" THE ACTUAL USE-CASE, AND THE USER'S OF THE USER OF
        // Torello.Java.Build.
        // 
        // ========================================================================================
        // 
        // Long-Story-Short: This variable (directly below) maintains a list of all menu-options
        // that would require "Stage-8" to run.  This a little bit simpler than other versions of
        // the code.  Build-Code is nothing more than a giant collection of "if-then-else run-it"
        // statements that are configured by "Torello.Java.Build.Config" and "JarInclude".  
        //
        // Translating a User's "Options & Switches" from the Command-Line (String[] argv) passed
        // to the "public static void main" method is really just ugly code to look at.
        // 
        // Which Menu-Options "The User's User" has selected is difficult to explain, sometimes,
        // but means that the data inside the class Config Record is "The User", while the data
        // inside the class CLI are all of the data that is associated with the choice / selection
        // that "The User's User" has made at the command line (and passed to the 
        // "public static void main(String[] argv)" method)
        // 
        // AGAIN: These often are the exact same person, but acting in completely different
        //        contexts, and therefore have to be handled / "thought-of" / explained as such.
        // 
        // ========================================================================================
        // 
        // Knowing & identifying which Menu-Options offer the SMA Browser-Cache-Elimination
        // feature, and whether or not the user's user has actually selected one of those
        // Menu-Options at the Command-Line for that feature are two entirely different concepts.
        // 
        // This field helps get the data / information / configurations from the User-Provided 
        // Class "Torello.Java.Build.Config" (specifically, the information contained by the
        // User-Provided list: ReadOnlyList<CloudSync), and convert that information into a list
        // of ReadOnlyList<String> identifying exactly which menu options reuest the SMA feature.
        //
        // This resultant ReadOnlyList generated by the ROArrayListBuilder is used inside class
        // "Torello.Java.Build.CLI" which actually does the top-level processing of the
        // "String[] argv" data passed to the "public static void main(...)" method.

        ROArrayListBuilder<String> sma_IsRequested_List_B = new ROArrayListBuilder<>();


        // There are two different types of "Partial-Build" options offered by the CLI Main-Menu.
        // The first "Partial Build" Composite-Stage / Composite-Step Option executes Stages
        // 2 (javadoc), 3 (ipgrade), 5 (Cloud-Sync) and 8 (No-Browser-Cache / Set-Max-Age).
        // (NOTE: If the user hasn't configured SMA, then this PB-Variant only runs 2, 3 and 5)
        // 
        // The second "-pb" option simply runs Step 2 (javadoc) and Step 3 (Upgrade)
        // This Array-List-Builder generates / produces a "ReadOnlyList<String>" that identifies
        // the complete list of "Partial-Build" Options which do "2, 3, 5 and 8" (or just "2", "3"
        // and "5").  If a user passes a "-pbX" (where X is some number) option at the Command-Line
        // which isn't in this list, then it must be the one that does Steps / Stages "2" and "3"
        // only!
        // 
        // No More, No Less

        final ROArrayListBuilder<String> partialWithSync_List_B = new ROArrayListBuilder<>();


        // This is used to keep a list of all of the options that are added which allow a user to
        // specify "Package Nick-Names" that need to be build.
        //
        // This list that is being created / built right here is just a list of all of the
        // Switch-Options which would accept a list of Package-Nicknames.  This "ReadOnly List
        // Builder" is built, and then used inside class CLI, to check if the user has entered in
        // an option that allows for a Package-Nick-Name List.
        // 
        // A pretty simple concept, but a VERY LONG variable name.

        final ROArrayListBuilder<String> optionsWhichAcceptPkgNickNames_B =
            new ROArrayListBuilder<>();


        // This cute-little-thing right here is just a "Map" whose KEY is set to anyone of the
        // several "-pb" or "-cb" options which are printed to the Man-Page / Main-Menu.  When
        // the User's User selects one of these Menu-Options, this Map will produce / return the
        // "CloudSync" Instance-Object that is associated with that Menu-Option.
        // 
        // If the User's User types "-pb2" at the Command-Line (Partial-Build #2), and the User
        // himself has associated "-pb2" with a particular Google-Cloud Storage-Bucket, then the
        // Cloud-Sync instance for that Google-Cloud Storage-Bucket will be retrieved / returned
        // from the class CLI (which processes the User's User's Input) using exactly this map.

        final ROHashMapBuilder<String, CloudSync> cloudSync_SwitchMapper_B =
            new ROHashMapBuilder<>();


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // CLI Menu-Options: "-1" to "-8" (also known as: "Primary Build-Step Options")
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // 
        // This wonderful little method fills up "group" (of type Apache.CLI.OptionGroup) with all
        // of the Menu-Options for the 8 Primary Build-Sages.  No more, no less.  Well, it also
        // generates the Man-Page / Help-Menu Text for those 8 Stages, and returns that as a String
        // which is then saved to "helpMenu_BuildStageSteps_Str"
        //
        // Method-Inputs: "group" (an instance of "Apache.CLI.OptionGroup"), into which the 8
        //                Primary Build-Stages / Build-Steps "Apache.CLI.Option" instances are
        //                inserted.
        // 
        //                "sma_IsRequested_List_B" - currently, the text-string "-8" is inserted
        //                into this read-only list-builder, since that is the only stage/step that
        //                has anything to do with preventing the Chrome Browser-Cache from caching
        //                Java-Doc HTML-Pages.
        // 
        // Method-Outputs: Notice that the above 2 references are actually instance that receive
        //                 outputs from this method.
        // 
        //                 **ALSO** This method returns a java.lang.String that contains the 
        //                 Command-Line Main-Menu Text for those 8 Build-Stages / Build-Steps.

        final String mainMenu_PrimaryOptions_Str = GeneratePrimaryOpts.generate
            (group, optionsWhichAcceptPkgNickNames_B, sma_IsRequested_List_B);


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Composite-Build-Step Menu-Option Name for the one that does Steps #1 to #4
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // 
        // This can be computed without actually creating this whole "ByRef" retrieval mechanism,
        // however if that ever were to change, this ensures there will be less typing in the
        // future.
        // 
        // The User will **ALWAYS** be provided a Menu-Option for running Steps #1 to #4.  If the
        // user has provided one single CloudSync instance to his class 'Config', then this 
        // Menu-Option will be a Partial-Build ("-pbX" option).  The current implementation pegs
        // this option to always being "-pb1"
        // 
        // If the user has provided a build 'Config' instance that has ZERO CloudSync instances,
        // then (in the current implementation), this will be assigned "-cb1", and it will also
        // be the only "Composite-Stage Complete-Build" Menu-Option provided.
        // 
        // The value assigned to this field inside this wrapper is computed and assigned inside of
        // class "GenerateCompositeOpts"

        final ByRef<String> compositeStep_RunStage2to4_OptionName = new ByRef<>();


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // CLI Menu-Options: "-pb" (Partial-Build) and "-cb" (Complete-Build) Composite-Options
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // 
        // Generates the Help-Menu / Main-Page for the "-pb" and "-cb" Command Line Menu-Options
        // 
        // This also generates the Text-String for the Man-Page / Main-Menu.
        //
        // Finally, this also returns a "cbCount" (Complete-Build Composite-Step Menu-Option Count)
        //
        // And a "pbCount" ==> A count of the number of Partial-Build Composite-Step Menu-Options.
        // 
        // Ret4 Instance:
        //      Ret4.a: The value of "cbCount" - which is a count on the number of "Complete Build"
        //              Menu-Options provided by the Composite-Build Steps of this Main-Menu.
        // 
        //      Ret4.b: The value of "pbCount" - a count of the number of "Partial Build" Composite
        //              Menu-Options of this Main-Menu.
        // 
        //      Ret4.c: A value saved to the java.lang.String
        //              "mainMenu_CompositeSteps_CompleteBuildOpts_Str".  This
        //              String simply contains the Command-Line Man-Page / Main-Menu-Text portion
        //              that has all of the "Composite-Step Complete-Build Build Options"
        //
        //              Specifically the "-cb" options for this CLI-Menu.
        // 
        //              Note that this String is used by class "PrintHelpRecord" in the case that
        //              the User's User has entered erroneous switch-info at the Command-Line, and
        //              needs the Man-Page / Help-Menu shoved in his face.
        //
        //      Ret4.d: A value saved to the java.lang.String
        //              "mainMenu_CompositeSteps_PartialBuildOpts_Str".
        //              Contains the Command-Line Man-Page for "Composite-Step Partial-Build Opts.
    
        final Ret4<Integer, Integer, String, String> r4 = GenerateCompositeOpts.generate(
            // Apache-CLI "OptionGroup" Instance
            group,

            // Builds a Map of "switch-as-str" ==> CloudSync-Reference
            cloudSync_SwitchMapper_B,

            // Builds list of all Main-Menu Options which accept a list of Package-Nick Names
            optionsWhichAcceptPkgNickNames_B,

            // Builds a List of "Set-Max-Age Step" Option-Names (These are String's like "-8")
            sma_IsRequested_List_B,


            // Builds a (Read-Only) List of Sync-To-Cloud Partial-Build Option-Names
            // (String's like "-pb2")

            partialWithSync_List_B,

    
            // Contains the Composite-Step Build-Stage that runs steps #1 to #4.  Note that this 
            // might be a "-pbX" switch, if the user has provided at least one instance of the
            // class 'CloudSync' inside of his 'Config' instance.
            // 
            // If the user is running a No-Cloud-Sync Build, then this will be a "-cbX" option.

            compositeStep_RunStage2to4_OptionName,


            // The User-Provided List of Cloud Synchronization Storage Buckets
            cloudSyncList
        );


        // HERE IS THE DATA GENERATED BY THE ABOVE METHOD!
        // The Ret4 Data is put into separate Variable-Names (to make naming easier).
        //
        // IT IS TRUE, THIS COULD BE CHANGED TO A JDK-21 "Record"... but I think that would be more
        // difficult to read not less, since the Record-Definition would have to be at the top of
        // this class, rather than right here, directly below the invokation!

        final int cbCount = r4.a;
        final int pbCount = r4.b;

        // The Complete-Build Menu-Text as a String. 
        final String mainMenu_CompositeSteps_CompleteBuildOpts_Str = (cbCount > 0)
            ? r4.c
            : "";

        // The Partial-Build Menu-Text as a String.
        final String mainMenu_CompositeSteps_PartialBuildOpts_Str = (pbCount > 0)
            ? r4.d
            : "";

        // This is a sanity-check for me (the developer), the above little ?-thingy is not supposed
        // to actually ever go-off.  Earlier it was planned to allow the user to provide ZERO
        // Cloud-Sync Options, and that perhaps either the Partial-Build or Complete-Build
        // Composite-Step Sub-Menu's might be empty.
        // 
        // I have seen the light and realize that neither of these would ever be empty, because
        // the option to do steps #2 & #3 will always be valuable, and always be a "Partial-Build"
        // (Meaing the Partial-Build Composite-Step Sub-Menu would **NEVER** be empty).  As for the
        // Complete-Build Composite-Step Sub-Menu, well steps #1 to #4 would be always be an option
        // (no matter what!) - even if the user provided ZERO CloudSync isntances.
        // 
        // So, in the end, those little "Ternary Operators" are just goig to sit there (as is this
        // long-winded comment) so that I may remember exactly what Composite-Step Menu-Options are
        // always available - NO MATTER WHAT THE USER HAS CONFIGURED INSIDE OF HIS 'Config' Class
        // instance / Object-Reference.
        // 
        // ALSO: These exception/error throws are going to sit right here, as well, in case I ever
        //       screw something up.  The "extra-overhead cost" of executing these two lines seems
        //       like absolutely nothing, they are executed only once, they are not inside of some
        //       kind of loop.  UnreachableError is so much better than "assert", by the way.
        // 
        // UnreachableError: Better than 'assert'.  I want this case to crash immediately, and 
        //                   unconditionally.  Developers are the only ones who are going to use
        //                   this Build-Tool, anyway!  Other developers can deal with errors and
        //                   exception throws just fine.

        if (cbCount == 0) throw new UnreachableError();
        if (pbCount == 0) throw new UnreachableError();


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // CLI Option for requesting to run the "Links Checker"
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        // CLI Option for requesting to run the "Links Checker"
        group.addOption(
            Option
                .builder    ("LC")
                .required   (false)
                .desc       ("Links Checker Build")
                .longOpt    ("linksCheck")
                .hasArg     (false)
                .build()
        );

        // The "Menu Text" for this Main-Menu-Option is just 2 lines, and actually created inside
        // the class "PrintHelpRecord"


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Instantiate Apache.CLI.Options
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        final Options options = new Options();

        // Add the main / top-level Command-Line Arguments
        options.addOptionGroup(group);


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Add the "Extra Options" ("-srg", "-txl", "-jcp", etc...)
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // 
        // Adds the Auxillary-Options to the Apache Main-Menu "OptionsGroup" instance.
        // These Options instance are not useful outside of their insertion into 'options'. 
        // This 'generate' method just returns those options, converted into a Menu-Text String
        // so that they may be easily printed to the CLI/Terminal if the user screws up and needs
        // to read the Main-Menu / Help-Screen thing.

        String nonMainMenu_AuxiliaryOptions_Str = GenerateAuxiliaryOpts.generate(options);


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Generate the Records, and Return them (and exit this method)
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        PrintHelpRecord phr = new PrintHelpRecord(
            cbCount,                                        // int
            pbCount,                                        // int
            mainMenu_PrimaryOptions_Str,                    // String
            mainMenu_CompositeSteps_CompleteBuildOpts_Str,  // String
            mainMenu_CompositeSteps_PartialBuildOpts_Str,   // String
            nonMainMenu_AuxiliaryOptions_Str                // String
        );

        CollatedOptsRecord cor = new CollatedOptsRecord(
            optionsWhichAcceptPkgNickNames_B.build(),   // ReadOnlyList<String>
            cloudSync_SwitchMapper_B.build(),           // ReadOnlyMap<String, CloudSync>
            sma_IsRequested_List_B.build(),             // ReadOnlyList<String>
            partialWithSync_List_B.build(),             // ReadOnlyList<String>
            compositeStep_RunStage2to4_OptionName.f     // String
        );

        // Apache.CLI.Options
        // Apache.CLI.OptionGroup
        // Torello.Java.Build.CollatedOptsRecord
        // Torello.Java.Build.PrintHelpRecord

        return new Ret4<>(options, group, cor, phr);
    }
}