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
package Torello.Java.Build;

import Torello.Java.StringParse;
import Torello.Java.FileNode;
import Torello.Java.RTC;

import Torello.Java.Additional.Counter;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import java.util.ArrayList;
import java.util.function.Predicate;

import Torello.Java.ReadOnly.ROArrayListBuilder;
import Torello.Java.ReadOnly.ROHashtableBuilder;
import Torello.Java.ReadOnly.ReadOnlyList;
import Torello.Java.ReadOnly.ReadOnlyArrayList;

import Torello.Java.StrCSV;

import static Torello.Java.C.BRED;
import static Torello.Java.C.RESET;

/**
 * Given a list of packges, collate a list of files to processed by the various
 * Build-Stages.
 */
@Torello.JavaDoc.StaticFunctional
public class Files
{
    // Completely irrelevant, and the 'private' modifier keeps it off of JavaDoc
    private Files() { }

    private static final String FS = File.separator;


    // ********************************************************************************************
    // ********************************************************************************************
    // File-Array Builder
    // ********************************************************************************************
    // ********************************************************************************************


    private static final int addMore
        (ROArrayListBuilder<String> b, String dir, boolean depth, FilenameFilter filter)
    {
        Counter c = new Counter(0);

        FileNode
            .createRoot         (dir)
            .loadTree           (depth ? -1 : 0, filter, null)
            .flattenJustFiles   (RTC.FULLPATH_VECTOR())
            .forEach            ((String srcFileName) -> { c.addOne(); b.add(srcFileName); });

        return c.get();
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Stage 01, Compile Files
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Retrieve the Stage 1 Files List of {@code '.java'} Files.  The master list that is returned
     * as a result of this method is the complete list of files sent to {@code 'javac'}, as command
     * line parameters.
     * 
     * @param packagesToCompile The list of packages whose files need to be compiled.  This list,
     * itself, was built by class {@link Packages} method
     * {@link Packages#packagesToCompile(Builder) builder} using all User-Provided Configurations,
     * and Command-Line Switch Choices.
     * 
     * @param logAndScreen Prints log information to both the log and the terminal-screen.
     * 
     * @return The list of files to be compiled by the Stage 1 Build-Class
     * {@link S01_JavaCompiler}.
     * 
     * @throws IOException Required due to the {@code Appendable} parameter {@code 'logAndScreen'}
     * @see Printing#printFilesCount(ReadOnlyList, ArrayList, Appendable)
     * @see BuildPackage#pkgRootDirectory
     * @see BuildPackage#hasSubPackages
     * @see BuildPackage#hasUpgradeFilesDir
     * @see BuildPackage#hasPackageSourceDir
     * @see BuildPackage#helperPackages
     */
    public static ReadOnlyList<String> filesToCompile(
            ReadOnlyList<BuildPackage>  packagesToCompile,
            Appendable                  logAndScreen
        )
        throws IOException
    {
        // This is much easier to understand.  It is actually smarter to just go to the class
        // "Files" and look what it is doing.  The loop that collects the files for a "Package"
        // uses the following BuildPackage boolean-fields, which are defined, as below, inside the
        // BuildPackage-Constructor:
        //
        // BuildPackage.hasSubPackages     = (flags & HAS_SUB_PACKAGES)    >= 1;
        // BuildPackage.pkgRootDirectory   = classPathLocation + fullName.replace(".", FS) + FS;
        //
        // this.helperPackages = (helperPackages == null) || (helperPackages.length == 0)
        //     ? EMPTY_LIST
        //     : ReadOnlyList.of(helperPackages);
        //
        // Just go to class "Files" to see how those "BuildPackage" boolean Configuration-Fields
        // pick files to insert into the "filesToCompile" list.

        // This keeps a complete list of all files that will need to be compiled
        ROArrayListBuilder<String> b = new ROArrayListBuilder<>();

        // This is used for printing to the screen only, it is discarded when this method
        // exits.

        final ArrayList<Integer> packageFilesCount = new ArrayList<>(packagesToCompile.size());

        for (final BuildPackage pkg : packagesToCompile)
        {
            int count = 0;

            count += addMore(b, pkg.pkgRootDirectory, pkg.hasSubPackages, FileNode.JAVA_FILES);

            if (pkg.hasUpgradeFilesDir) count += addMore
                (b, pkg.pkgRootDirectory + "upgrade-files" + FS, true, FileNode.JAVA_FILES);

            if (pkg.hasPackageSourceDir) count += addMore
                (b, pkg.pkgRootDirectory + "package-source" + FS, true, FileNode.JAVA_FILES);

            for (String helperPkgDirName : pkg.helperPackages) count += addMore
                (b, helperPkgDirName, false, FileNode.JAVA_FILES);

            packageFilesCount.add(Integer.valueOf(count));
        }

        Printing.printFilesCount(packagesToCompile, packageFilesCount, logAndScreen);

        return b.build();
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Stage 02, JavaDoc Files
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Retrieve the Stage 1 Files List of {@code '.java'} Files.  The master list that is returned
     * as a result of this method is the complete list of files sent to {@code 'javadoc'}, as
     * command line parameters.
     * 
     * @param packagesToJavaDoc The list of packages whose files need to be compiled.  This list,
     * itself, was built by class {@link Packages} method
     * {@link Packages#packagesToCompile(Builder) builder} using all User-Provided Configurations,
     * and Command-Line Switch Choices.
     * 
     * @param logAndScreen Prints log information to both the log and the terminal-screen.
     * 
     * @return The list of files to be compiled by the Stage 2 Build-Class
     * {@link S02_JavaDoc}.
     * 
     * @throws IOException Required due to the {@code Appendable} parameter {@code 'logAndScreen'}
     * @see Printing#printFilesCount(ReadOnlyList, ArrayList, Appendable)
     * @see BuildPackage#pkgRootDirectory
     * @see BuildPackage#hasPackageSourceDir
     */
    public static ReadOnlyList<String> filesToJavaDoc(
            ReadOnlyList<BuildPackage>  packagesToJavaDoc,
            Appendable                  logAndScreen
        )
        throws IOException
    {
        // This keeps a complete list of all files that will need to be compiled
        ROArrayListBuilder<String> b = new ROArrayListBuilder<>();

        // This is used for printing to the screen only, it is discarded when this method
        // exits.

        final ArrayList<Integer> packageFilesCount = new ArrayList<>(packagesToJavaDoc.size());

        for (final BuildPackage pkg : packagesToJavaDoc)
        {
            int count = 0;

            count += addMore(b, pkg.pkgRootDirectory, false, FileNode.JAVA_FILES);

            if (pkg.hasPackageSourceDir) count += addMore
                (b, pkg.pkgRootDirectory + "package-source" + FS, true, FileNode.JAVA_FILES);

            packageFilesCount.add(Integer.valueOf(count));
        }

        Printing.printFilesCount(packagesToJavaDoc, packageFilesCount, logAndScreen);

        return b.build();
    }
}