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.Tuple;
12  import io.vavr.collection.HashMap;
13  import io.vavr.collection.Map;
14  import io.vavr.collection.Seq;
15  import io.vavr.control.Option;
16  
17  import com.varmateo.yawg.api.YawgException;
18  import com.varmateo.yawg.logging.Log;
19  import com.varmateo.yawg.logging.LogFactory;
20  import com.varmateo.yawg.spi.PageBakeResult;
21  import com.varmateo.yawg.spi.PageBaker;
22  import com.varmateo.yawg.spi.PageContext;
23  
24  
25  /**
26   * 
27   */
28  /* default */ final class FileBaker {
29  
30  
31      private final Log _log;
32      private final Seq<PageBaker> _bakers;
33      private final PageBaker _defaultBaker;
34  
35      // Keys are baker names (aka baker types), values are the
36      // corresponding bakers. This map also includes the default baker.
37      private final Map<String, PageBaker> _allBakersByType;
38  
39  
40      /**
41       * @param log The logger that will be used for logging.
42       */
43      /* default */ FileBaker(
44              final Seq<PageBaker> bakers,
45              final PageBaker defaultBaker) {
46  
47          _log = LogFactory.createFor(FileBaker.class);
48          _bakers = bakers;
49          _defaultBaker = defaultBaker;
50          _allBakersByType = HashMap.ofEntries(bakers.map(b -> Tuple.of(b.shortName(), b)))
51                  .put(defaultBaker.shortName(), defaultBaker);
52      }
53  
54  
55      /**
56       * 
57       */
58      public void bakeFile(
59              final Path sourcePath,
60              final PageContext context,
61              final Path targetDir,
62              final DirBakeOptions dirBakeOptions)
63              throws YawgException {
64  
65          final PageBaker baker = findBaker(sourcePath, dirBakeOptions);
66          final long startTime = System.currentTimeMillis();
67  
68          final PageBakeResult result = baker.bake(sourcePath, context, targetDir);
69          if ( !result.isSuccess() ) {
70              throw result.failureCause();
71          }
72  
73          final long delay = System.currentTimeMillis() - startTime;
74          final Path sourceBasename = sourcePath.getFileName();
75          _log.debug(
76                  "    {1}: {0} ({2}ms)", sourceBasename, baker.shortName(), String.valueOf(delay));
77      }
78  
79  
80      /**
81       * @throws YawgException Thrown if the directory configuration
82       * specifies a baker type that is unknown.
83       */
84      private PageBaker findBaker(
85              final Path sourcePath,
86              final DirBakeOptions dirBakeOptions) {
87  
88          return dirBakeOptions.bakerTypes
89                  .flatMap(bakerTypes -> bakerTypes.bakerTypeFor(sourcePath))
90                  .map(bakerType -> findBakerWithType(bakerType))
91                  .getOrElse(() -> findBakerFromAll(sourcePath));
92      }
93  
94  
95      /**
96       *
97       */
98      private PageBaker findBakerWithType(final String bakerType) {
99  
100         final Option<PageBaker> baker = _allBakersByType.get(bakerType);
101 
102         if ( !baker.isDefined() ) {
103             throw FileBakerException.unknownBakerType(bakerType);
104         }
105 
106         return baker.get();
107     }
108 
109 
110     /**
111      *
112      */
113     private PageBaker findBakerFromAll(final Path sourcePath) {
114 
115         return _bakers
116                 .filter(candidate -> candidate.isBakeable(sourcePath))
117                 .headOption()
118                 .getOrElse(_defaultBaker);
119     }
120 
121 
122 }