View Javadoc
1   /**************************************************************************
2    *
3    * Copyright (c) 2016-2020 Yawg project contributors.
4    *
5    **************************************************************************/
6   
7   package com.varmateo.yawg.core;
8   
9   import java.nio.file.Path;
10  
11  import io.vavr.collection.List;
12  import io.vavr.collection.Seq;
13  import io.vavr.control.Option;
14  
15  import com.varmateo.yawg.spi.PageVars;
16  import com.varmateo.yawg.spi.PageVarsBuilder;
17  import com.varmateo.yawg.util.GlobMatcher;
18  
19  
20  /**
21   * Set of configuration parameters used for baking the files in a
22   * directory.
23   */
24  /* default */ final class DirBakeOptions {
25  
26  
27      /**
28       * Name of template to use when baking a directory and all its
29       * sub-directories.
30       */
31      public final Option<String> templateName;
32  
33      /**
34       * List of files to exclude when baking a directory and its
35       * sub-directories.
36       */
37      public final Option<GlobMatcher> filesToExclude;
38  
39      /**
40       * List of files to exclude when baking the current directory.
41       */
42      public final Option<GlobMatcher> filesToExcludeHere;
43  
44      /**
45       * Strict list of files to include when baking the current
46       * directory.
47       */
48      public final Option<GlobMatcher> filesToIncludeHere;
49  
50      /**
51       * Association of baker types to some lists of files.
52       */
53      public final Option<BakerMatcher> bakerTypes;
54  
55      /**
56       * Set of variables to be made available to the template that will
57       * also be propagated to child directories.
58       */
59      public final PageVars pageVars;
60  
61  
62      /**
63       * Set of variables made available to the template that will not
64       * be propagated to child directories.
65       */
66      public final PageVars pageVarsHere;
67  
68  
69      /**
70       *
71       */
72      public final TemplateNameMatcher templatesHere;
73  
74  
75      /**
76       * Collection of additional directories with content to be
77       * baked. The bake results from those directories will be placed
78       * in the same location as the bake results from the directory
79       * currently being baked.
80       */
81      public final Seq<Path> extraDirsHere;
82  
83  
84      /**
85       *
86       */
87      /* default */ DirBakeOptions(final Builder builder) {
88  
89          this.templateName = builder._templateName;
90          this.filesToExclude = builder._filesToExclude;
91          this.filesToExcludeHere = builder._filesToExcludeHere;
92          this.filesToIncludeHere = builder._filesToIncludeHere;
93          this.bakerTypes = builder._bakerTypes;
94          this.pageVars = builder._pageVarsBuilder.build();
95          this.pageVarsHere = builder._pageVarsHere;
96          this.templatesHere = builder._templatesHere;
97          this.extraDirsHere = builder._extraDirsHere;
98      }
99  
100 
101     /**
102      * Creates a new builder with no initializations.
103      *
104      * @return A newly created <code>Builder</code> instance.
105      */
106     public static Builder builder() {
107 
108         return new Builder();
109     }
110 
111 
112     /**
113      *
114      */
115     public static DirBakeOptions empty() {
116 
117         return DirBakeOptions.builder().build();
118     }
119 
120 
121     /**
122      * Creates a new container starting with the values from
123      * <code>that</code> and overriding it with the values we possess.
124      *
125      * @param that The source of default values.
126      *
127      * @return A newly created container initialized with our values,
128      * and using as default values the ones contained in
129      * <code>that</code>.
130      */
131     public DirBakeOptions#DirBakeOptions">DirBakeOptions mergeOnTopOf(final DirBakeOptions that) {
132 
133         final Builder builder = new Builder(that);
134 
135         this.templateName.forEach(builder::templateName);
136         this.filesToExclude.forEach(builder::addFilesToExclude);
137         this.filesToExcludeHere.forEach(builder::filesToExcludeHere);
138         this.filesToIncludeHere.forEach(builder::filesToIncludeHere);
139         this.bakerTypes.forEach(builder::addBakerTypes);
140         builder
141                 .addPageVars(this.pageVars)
142                 .pageVarsHere(this.pageVarsHere)
143                 .templatesHere(this.templatesHere)
144                 .extraDirsHere(this.extraDirsHere);
145 
146 
147         return builder.build();
148     }
149 
150 
151     /**
152      * A builder of <code>DirBakeOptions</code> instances.
153      */
154     public static final class Builder {
155 
156 
157         // These always start empty.
158         private Option<GlobMatcher> _filesToExcludeHere = Option.none();
159         private Option<GlobMatcher> _filesToIncludeHere = Option.none();
160         private PageVars _pageVarsHere = PageVars.empty();
161         private TemplateNameMatcher _templatesHere = TemplateNameMatcher.empty();
162 
163         // These adopt the values from externally provided
164         // initialization data.
165         private Option<String> _templateName;
166         private Option<GlobMatcher> _filesToExclude;
167         private Option<BakerMatcher> _bakerTypes;
168         private PageVarsBuilder _pageVarsBuilder;
169         private Seq<Path> _extraDirsHere;
170 
171 
172         /**
173          *
174          */
175         /* default */ Builder() {
176 
177             _templateName = Option.none();
178             _filesToExclude = Option.none();
179             _bakerTypes = Option.none();
180             _pageVarsBuilder = PageVarsBuilder.create();
181             _extraDirsHere = List.of();
182         }
183 
184 
185         /**
186          * Prepares a Builder for performing a merge.
187          */
188         /* default */ Builder(final DirBakeOptions defaults) {
189 
190             _templateName = defaults.templateName;
191             _filesToExclude = defaults.filesToExclude;
192             _bakerTypes = defaults.bakerTypes;
193             _pageVarsBuilder = PageVarsBuilder.create(defaults.pageVars);
194             _extraDirsHere = defaults.extraDirsHere;
195         }
196 
197 
198         /**
199          *
200          */
201         public Builder templateName(final String templateName) {
202 
203             _templateName = Option.of(templateName);
204 
205             return this;
206         }
207 
208 
209         /**
210          *
211          */
212         public Builder filesToExclude(final GlobMatcher fileNames) {
213 
214             _filesToExclude = Option.of(fileNames);
215 
216             return this;
217         }
218 
219 
220         /**
221          * @throws PatternSyntaxException If any of the given glob
222          * expressions are invalid.
223          */
224         public Builder filesToExclude(final String... fileNames) {
225 
226             final GlobMatcher patterns = GlobMatcher.create(fileNames);
227 
228             filesToExclude(patterns);
229 
230             return this;
231         }
232 
233 
234         /**
235          *
236          */
237         private Builder addFilesToExclude(final GlobMatcher fileNames) {
238 
239             _filesToExclude = _filesToExclude
240                     .map(x ->
241                          GlobMatcher.builder(x).addGlobMatcher(fileNames).build())
242                     .orElse(() -> Option.some(fileNames));
243 
244             return this;
245         }
246 
247 
248         /**
249          *
250          */
251         public Builder filesToExcludeHere(final GlobMatcher fileNames) {
252 
253             _filesToExcludeHere = Option.of(fileNames);
254 
255             return this;
256         }
257 
258 
259         /**
260          * @throws PatternSyntaxException If any of the given regular
261          * expressions are invalid.
262          */
263         public Builder filesToExcludeHere(final String... fileNames) {
264 
265             final GlobMatcher patterns = GlobMatcher.create(fileNames);
266 
267             return filesToExcludeHere(patterns);
268         }
269 
270 
271         /**
272          *
273          */
274         public Builder filesToIncludeHere(final GlobMatcher fileNames) {
275 
276             _filesToIncludeHere = Option.of(fileNames);
277 
278             return this;
279         }
280 
281 
282         /**
283          * @throws PatternSyntaxException If any of the given regular
284          * expressions are invalid.
285          */
286         public Builder filesToIncludeHere(final String... fileNames) {
287 
288             final GlobMatcher patterns = GlobMatcher.create(fileNames);
289 
290             return filesToIncludeHere(patterns);
291         }
292 
293 
294         /**
295          *
296          */
297         public Builder bakerTypes(final BakerMatcher bakerTypes) {
298 
299             _bakerTypes = Option.of(bakerTypes);
300 
301             return this;
302         }
303 
304 
305         /**
306          *
307          */
308         public Builder addBakerType(
309                 final String bakerType,
310                 final String... fileNames) {
311 
312             final BakerMatcher bakerTypes = BakerMatcher.builder()
313                     .addBakerType(bakerType, fileNames)
314                     .build();
315 
316             addBakerTypes(bakerTypes);
317 
318             return this;
319         }
320 
321 
322         /**
323          *
324          */
325         private Builder addBakerTypes(final BakerMatcher bakerTypes) {
326 
327             _bakerTypes = _bakerTypes
328                     .map(x ->
329                          BakerMatcher.builder(x).addBakerTypes(bakerTypes).build())
330                     .orElse(() -> Option.of(bakerTypes));
331 
332             return this;
333         }
334 
335 
336         /**
337          *
338          */
339         public  Builder pageVars(final PageVars pageVars) {
340 
341             _pageVarsBuilder = PageVarsBuilder.create(pageVars);
342 
343             return this;
344         }
345 
346 
347         /**
348          *
349          */
350         private Builder addPageVars(final PageVars pageVars) {
351 
352             _pageVarsBuilder.addPageVars(pageVars);
353 
354             return this;
355         }
356 
357 
358         /**
359          *
360          */
361         public Builder pageVarsHere(final PageVars pageVarsHere) {
362 
363             _pageVarsHere = pageVarsHere;
364 
365             return this;
366         }
367 
368 
369         /**
370          *
371          */
372         public Builder templatesHere(final TemplateNameMatcher templatesHere) {
373 
374             _templatesHere = templatesHere;
375 
376             return this;
377         }
378 
379 
380         /**
381          *
382          */
383         public Builder extraDirsHere(final Seq<Path> extraDirsHere) {
384 
385             _extraDirsHere = extraDirsHere;
386 
387             return this;
388         }
389 
390 
391         /**
392          *
393          */
394         public DirBakeOptions build() {
395 
396             return new DirBakeOptions(this);
397         }
398 
399 
400     }
401 
402 
403 }