| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import static com.google.common.base.Preconditions.checkState; |
| David Pursehouse | b40361f | 2017-05-30 10:41:53 +0900 | [diff] [blame] | 18 | import static java.util.stream.Collectors.toList; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 19 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
| David Pursehouse | b40361f | 2017-05-30 10:41:53 +0900 | [diff] [blame] | 21 | import com.google.common.collect.Streams; |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 22 | import com.google.common.hash.HashCode; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 23 | import com.google.template.soy.SoyFileSet; |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 24 | import com.google.template.soy.jbcsrc.api.SoySauce; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 25 | import java.io.File; |
| 26 | import java.net.URISyntaxException; |
| 27 | import java.net.URL; |
| 28 | |
| 29 | /** Renderer that reloads Soy templates from the filesystem on every request. */ |
| 30 | public class DebugRenderer extends Renderer { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 31 | public DebugRenderer( |
| 32 | String staticPrefix, |
| 33 | Iterable<String> customTemplatesFilenames, |
| 34 | final String soyTemplatesRoot, |
| 35 | String siteTitle) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 36 | super( |
| Dave Borowitz | de07eac | 2016-10-04 09:44:25 -0400 | [diff] [blame] | 37 | fileUrlMapper(soyTemplatesRoot + File.separator), |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 38 | ImmutableMap.<String, String>of(), |
| 39 | staticPrefix, |
| David Pursehouse | b40361f | 2017-05-30 10:41:53 +0900 | [diff] [blame] | 40 | Streams.stream(customTemplatesFilenames).map(fileUrlMapper()).collect(toList()), |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 41 | siteTitle); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Override |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 45 | public HashCode getTemplateHash(String soyFile) { |
| 46 | return computeTemplateHash(soyFile); |
| 47 | } |
| 48 | |
| 49 | @Override |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 50 | protected SoySauce getSauce() { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 51 | SoyFileSet.Builder builder = SoyFileSet.builder().setCompileTimeGlobals(globals); |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 52 | for (URL template : templates.values()) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 53 | try { |
| 54 | checkState(new File(template.toURI()).exists(), "Missing Soy template %s", template); |
| 55 | } catch (URISyntaxException e) { |
| 56 | throw new IllegalStateException(e); |
| 57 | } |
| 58 | builder.add(template); |
| 59 | } |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 60 | return builder.build().compileTemplates(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 61 | } |
| 62 | } |