OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.testing.robolectric.template; | |
6 | |
7 import org.apache.velocity.Template; | |
8 import org.apache.velocity.VelocityContext; | |
9 import org.apache.velocity.app.Velocity; | |
10 import org.apache.velocity.exception.VelocityException; | |
11 import org.apache.velocity.runtime.RuntimeConstants; | |
12 | |
13 import java.io.File; | |
14 import java.io.FileOutputStream; | |
15 import java.io.IOException; | |
16 import java.io.StringWriter; | |
17 import java.nio.file.PathMatcher; | |
18 import java.nio.file.Path; | |
19 import java.nio.file.Paths; | |
20 import java.nio.file.FileSystems; | |
21 import java.nio.file.SimpleFileVisitor; | |
22 import java.nio.file.attribute.BasicFileAttributes; | |
23 import java.nio.file.FileVisitResult; | |
24 import java.nio.file.Files; | |
25 import java.util.ArrayList; | |
26 import java.util.List; | |
27 | |
28 /** | |
29 * Class to process Robolectric template (*.vm) files using Apache Velocity. | |
30 */ | |
31 public final class ProcessRobolectricTemplate { | |
32 | |
33 private ProcessRobolectricTemplate() { | |
34 } | |
35 | |
36 public static void main(String[] args) { | |
37 final ProcessTemplateArgParser parser = ProcessTemplateArgParser.parse(a
rgs); | |
38 | |
39 Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); | |
40 Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, | |
41 parser.getBaseTemplateDir().toString()); | |
42 Velocity.init(); | |
43 | |
44 final VelocityContext context = new VelocityContext(); | |
45 final int api = parser.getApiLevel(); | |
46 context.put("api", api); | |
47 if (api >= 21) { | |
48 context.put("ptrClass", "long"); | |
49 context.put("ptrClassBoxed", "Long"); | |
50 } else { | |
51 context.put("ptrClass", "int"); | |
52 context.put("ptrClassBoxed", "Integer"); | |
53 } | |
54 try { | |
55 for(TemplateFileInfo templateFileInfo: parser.getTemplateFileInfoLis
t()) { | |
56 processTemplate(context, templateFileInfo.getTemplateFile(), | |
57 templateFileInfo.getOutputFile(), api); | |
58 } | |
59 } catch (IOException e) { | |
60 System.err.println("Error processing template files for Robolectric!
" + e.toString()); | |
61 } | |
62 } | |
63 | |
64 private static void processTemplate(VelocityContext context, Path templateFi
le, | |
65 Path outputFile, int api_level) throws IOException { | |
66 final StringWriter stringWriter = new StringWriter(); | |
67 Template template = Velocity.getTemplate(templateFile.toString(), "UTF-8
"); | |
68 template.merge(context, stringWriter); | |
69 if (!Files.exists(outputFile.getParent())) { | |
70 Files.createDirectories(outputFile.getParent()); | |
71 } | |
72 Files.write(outputFile, stringWriter.toString().getBytes("UTF-8")); | |
73 } | |
74 } | |
OLD | NEW |