blob: 5e4c46623a52d50d8766ca125c938dce531ea25c [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles;
16
17import static com.google.common.base.Preconditions.checkState;
David Pursehouseb40361f2017-05-30 10:41:53 +090018import static java.util.stream.Collectors.toList;
Dave Borowitz9de65952012-08-13 16:09:45 -070019
Dave Borowitz9de65952012-08-13 16:09:45 -070020import com.google.common.collect.ImmutableMap;
David Pursehouseb40361f2017-05-30 10:41:53 +090021import com.google.common.collect.Streams;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080022import com.google.common.hash.HashCode;
Dave Borowitz9de65952012-08-13 16:09:45 -070023import com.google.template.soy.SoyFileSet;
David Pursehouse28726042019-06-27 09:09:30 +090024import com.google.template.soy.jbcsrc.api.SoySauce;
Dave Borowitz9de65952012-08-13 16:09:45 -070025import java.io.File;
26import java.net.URISyntaxException;
27import java.net.URL;
28
29/** Renderer that reloads Soy templates from the filesystem on every request. */
30public class DebugRenderer extends Renderer {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020031 public DebugRenderer(
32 String staticPrefix,
33 Iterable<String> customTemplatesFilenames,
34 final String soyTemplatesRoot,
35 String siteTitle) {
Dave Borowitz9de65952012-08-13 16:09:45 -070036 super(
Dave Borowitzde07eac2016-10-04 09:44:25 -040037 fileUrlMapper(soyTemplatesRoot + File.separator),
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020038 ImmutableMap.<String, String>of(),
39 staticPrefix,
David Pursehouseb40361f2017-05-30 10:41:53 +090040 Streams.stream(customTemplatesFilenames).map(fileUrlMapper()).collect(toList()),
Dave Borowitz76bbefd2014-03-11 16:57:45 -070041 siteTitle);
Dave Borowitz9de65952012-08-13 16:09:45 -070042 }
43
44 @Override
Shawn Pearcea9b99a12015-02-10 15:35:11 -080045 public HashCode getTemplateHash(String soyFile) {
46 return computeTemplateHash(soyFile);
47 }
48
49 @Override
David Pursehouse28726042019-06-27 09:09:30 +090050 protected SoySauce getSauce() {
Jesse Costello-Goodf65ff3b2021-05-17 13:18:09 -070051 SoyFileSet.Builder builder = SoyFileSet.builder();
Shawn Pearcea9b99a12015-02-10 15:35:11 -080052 for (URL template : templates.values()) {
Dave Borowitz9de65952012-08-13 16:09:45 -070053 try {
54 checkState(new File(template.toURI()).exists(), "Missing Soy template %s", template);
55 } catch (URISyntaxException e) {
56 throw new IllegalStateException(e);
57 }
Jesse Costello-Good6f62c5d2021-02-02 12:05:06 -080058 builder.add(template, toSoySrcPath(template));
Dave Borowitz9de65952012-08-13 16:09:45 -070059 }
David Pursehouse28726042019-06-27 09:09:30 +090060 return builder.build().compileTemplates();
Dave Borowitz9de65952012-08-13 16:09:45 -070061 }
62}