blob: 0a57b6bbac909ad8e4dd7795e76a482931ae250c [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;
18
Dave Borowitz76bbefd2014-03-11 16:57:45 -070019import com.google.common.collect.FluentIterable;
Dave Borowitz9de65952012-08-13 16:09:45 -070020import com.google.common.collect.ImmutableMap;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080021import com.google.common.hash.HashCode;
Dave Borowitz9de65952012-08-13 16:09:45 -070022import com.google.template.soy.SoyFileSet;
23import com.google.template.soy.tofu.SoyTofu;
24
25import 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 {
Dave Borowitz76bbefd2014-03-11 16:57:45 -070031 public DebugRenderer(String staticPrefix, Iterable<String> customTemplatesFilenames,
Chad Horohoe2a28d622012-11-12 11:56:59 -080032 final String soyTemplatesRoot, String siteTitle) {
Dave Borowitz9de65952012-08-13 16:09:45 -070033 super(
Dave Borowitz76bbefd2014-03-11 16:57:45 -070034 new FileUrlMapper(soyTemplatesRoot + File.separator),
Dave Borowitz9de65952012-08-13 16:09:45 -070035 ImmutableMap.<String, String> of(), staticPrefix,
Dave Borowitz76bbefd2014-03-11 16:57:45 -070036 FluentIterable.from(customTemplatesFilenames).transform(new FileUrlMapper()),
37 siteTitle);
Dave Borowitz9de65952012-08-13 16:09:45 -070038 }
39
40 @Override
Shawn Pearcea9b99a12015-02-10 15:35:11 -080041 public HashCode getTemplateHash(String soyFile) {
42 return computeTemplateHash(soyFile);
43 }
44
45 @Override
Dave Borowitz9de65952012-08-13 16:09:45 -070046 protected SoyTofu getTofu() {
Dave Borowitz11bbead2014-06-26 12:18:26 -070047 SoyFileSet.Builder builder = SoyFileSet.builder()
Dave Borowitz9de65952012-08-13 16:09:45 -070048 .setCompileTimeGlobals(globals);
Shawn Pearcea9b99a12015-02-10 15:35:11 -080049 for (URL template : templates.values()) {
Dave Borowitz9de65952012-08-13 16:09:45 -070050 try {
51 checkState(new File(template.toURI()).exists(), "Missing Soy template %s", template);
52 } catch (URISyntaxException e) {
53 throw new IllegalStateException(e);
54 }
55 builder.add(template);
56 }
57 return builder.build().compileToTofu();
58 }
59}