OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.testing.robolectric.template; | 5 package org.chromium.testing.robolectric.template; |
6 | 6 |
7 import org.apache.velocity.Template; | 7 import org.apache.velocity.Template; |
8 import org.apache.velocity.VelocityContext; | 8 import org.apache.velocity.VelocityContext; |
9 import org.apache.velocity.app.Velocity; | 9 import org.apache.velocity.app.Velocity; |
10 import org.apache.velocity.exception.VelocityException; | 10 import org.apache.velocity.exception.VelocityException; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 final VelocityContext context = new VelocityContext(); | 44 final VelocityContext context = new VelocityContext(); |
45 final int api = parser.getApiLevel(); | 45 final int api = parser.getApiLevel(); |
46 context.put("api", api); | 46 context.put("api", api); |
47 if (api >= 21) { | 47 if (api >= 21) { |
48 context.put("ptrClass", "long"); | 48 context.put("ptrClass", "long"); |
49 context.put("ptrClassBoxed", "Long"); | 49 context.put("ptrClassBoxed", "Long"); |
50 } else { | 50 } else { |
51 context.put("ptrClass", "int"); | 51 context.put("ptrClass", "int"); |
52 context.put("ptrClassBoxed", "Integer"); | 52 context.put("ptrClassBoxed", "Integer"); |
53 } | 53 } |
54 | |
55 try { | 54 try { |
56 final PathMatcher templatePathMatcher = | 55 for(TemplateFileInfo templateFileInfo: parser.getTemplateFileInfoLis
t()) { |
57 FileSystems.getDefault().getPathMatcher("glob:" + "**/*.vm")
; | 56 processTemplate(context, templateFileInfo.getTemplateFile(), |
58 Files.walkFileTree(parser.getBaseTemplateDir(), new SimpleFileVisito
r<Path>() { | 57 templateFileInfo.getOutputFile(), api); |
59 @Override | 58 } |
60 public FileVisitResult visitFile( | |
61 Path path, BasicFileAttributes attrs) throws IOException
{ | |
62 if (templatePathMatcher.matches(path)) { | |
63 processTemplate(context, path, parser.getBaseTemplateDir
(), parser.getOutputDir(), api); | |
64 } | |
65 return FileVisitResult.CONTINUE; | |
66 } | |
67 }); | |
68 } catch (IOException e) { | 59 } catch (IOException e) { |
69 System.err.println("Error processing template files for Robolectric!
" + e.toString()); | 60 System.err.println("Error processing template files for Robolectric!
" + e.toString()); |
70 } | 61 } |
71 } | 62 } |
72 | 63 |
73 private static void processTemplate(VelocityContext context, Path templateFi
le, Path baseTemplateDir, Path outputDir, int api_level) throws IOException { | 64 private static void processTemplate(VelocityContext context, Path templateFi
le, |
| 65 Path outputFile, int api_level) throws IOException { |
74 final StringWriter stringWriter = new StringWriter(); | 66 final StringWriter stringWriter = new StringWriter(); |
75 Template template = Velocity.getTemplate(baseTemplateDir.relativize(temp
lateFile).toString(), "UTF-8"); | 67 Template template = Velocity.getTemplate(templateFile.toString(), "UTF-8
"); |
76 template.merge(context, stringWriter); | 68 template.merge(context, stringWriter); |
77 | |
78 String templateFilename = templateFile.getFileName().toString(); | |
79 String processedFilename = "" + api_level + File.separator + templateFil
ename.replace(".vm", ""); | |
80 | |
81 String relativeOutputFile = templateFile.toString().replace(baseTemplate
Dir.toString(), "").replace(templateFilename, processedFilename); | |
82 | |
83 if (relativeOutputFile.startsWith("/")) { | |
84 relativeOutputFile = relativeOutputFile.substring(1); | |
85 } | |
86 Path outputFile = outputDir.resolve(relativeOutputFile); | |
87 if (!Files.exists(outputFile.getParent())) { | 69 if (!Files.exists(outputFile.getParent())) { |
88 Files.createDirectories(outputFile.getParent()); | 70 Files.createDirectories(outputFile.getParent()); |
89 } | 71 } |
90 Files.write(outputFile, stringWriter.toString().getBytes("UTF-8")); | 72 Files.write(outputFile, stringWriter.toString().getBytes("UTF-8")); |
91 } | 73 } |
92 } | 74 } |
OLD | NEW |