blob: 1de2f69ad671ad7c6a12e476e3cc1b5a5caed4b1 [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;
21import com.google.template.soy.SoyFileSet;
22import com.google.template.soy.tofu.SoyTofu;
23
24import java.io.File;
25import java.net.URISyntaxException;
26import java.net.URL;
27
28/** Renderer that reloads Soy templates from the filesystem on every request. */
29public class DebugRenderer extends Renderer {
Dave Borowitz76bbefd2014-03-11 16:57:45 -070030 public DebugRenderer(String staticPrefix, Iterable<String> customTemplatesFilenames,
Chad Horohoe2a28d622012-11-12 11:56:59 -080031 final String soyTemplatesRoot, String siteTitle) {
Dave Borowitz9de65952012-08-13 16:09:45 -070032 super(
Dave Borowitz76bbefd2014-03-11 16:57:45 -070033 new FileUrlMapper(soyTemplatesRoot + File.separator),
Dave Borowitz9de65952012-08-13 16:09:45 -070034 ImmutableMap.<String, String> of(), staticPrefix,
Dave Borowitz76bbefd2014-03-11 16:57:45 -070035 FluentIterable.from(customTemplatesFilenames).transform(new FileUrlMapper()),
36 siteTitle);
Dave Borowitz9de65952012-08-13 16:09:45 -070037 }
38
39 @Override
40 protected SoyTofu getTofu() {
Dave Borowitz11bbead2014-06-26 12:18:26 -070041 SoyFileSet.Builder builder = SoyFileSet.builder()
Dave Borowitz9de65952012-08-13 16:09:45 -070042 .setCompileTimeGlobals(globals);
43 for (URL template : templates) {
44 try {
45 checkState(new File(template.toURI()).exists(), "Missing Soy template %s", template);
46 } catch (URISyntaxException e) {
47 throw new IllegalStateException(e);
48 }
49 builder.add(template);
50 }
51 return builder.build().compileToTofu();
52 }
53}