Chromium Code Reviews| 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 16 matching lines...) Expand all Loading... | |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * Class to process Robolectric template (*.vm) files using Apache Velocity. | 29 * Class to process Robolectric template (*.vm) files using Apache Velocity. |
| 30 */ | 30 */ |
| 31 public final class ProcessRobolectricTemplate { | 31 public final class ProcessRobolectricTemplate { |
| 32 | 32 |
| 33 private ProcessRobolectricTemplate() { | 33 private ProcessRobolectricTemplate() { |
| 34 } | 34 } |
| 35 | 35 |
| 36 public static void main(String[] args) { | 36 public static void main(String[] args) { |
| 37 | |
| 37 final ProcessTemplateArgParser parser = ProcessTemplateArgParser.parse(a rgs); | 38 final ProcessTemplateArgParser parser = ProcessTemplateArgParser.parse(a rgs); |
| 38 | 39 |
| 39 Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); | 40 Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); |
| 40 Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, | 41 Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, |
| 41 parser.getBaseTemplateDir().toString()); | 42 parser.getBaseTemplateDir().toString()); |
| 42 Velocity.init(); | 43 Velocity.init(); |
| 43 | 44 |
| 44 final VelocityContext context = new VelocityContext(); | 45 final VelocityContext context = new VelocityContext(); |
| 45 final int api = parser.getApiLevel(); | 46 final int api = parser.getApiLevel(); |
| 46 context.put("api", api); | 47 context.put("api", api); |
| 47 if (api >= 21) { | 48 if (api >= 21) { |
| 48 context.put("ptrClass", "long"); | 49 context.put("ptrClass", "long"); |
| 49 context.put("ptrClassBoxed", "Long"); | 50 context.put("ptrClassBoxed", "Long"); |
| 50 } else { | 51 } else { |
| 51 context.put("ptrClass", "int"); | 52 context.put("ptrClass", "int"); |
| 52 context.put("ptrClassBoxed", "Integer"); | 53 context.put("ptrClassBoxed", "Integer"); |
| 53 } | 54 } |
| 54 | |
| 55 try { | 55 try { |
| 56 final PathMatcher templatePathMatcher = | 56 for(TemplateFileInfo templateFileInfo: parser.getTemplateFileInfoLis t()) { |
| 57 FileSystems.getDefault().getPathMatcher("glob:" + "**/*.vm") ; | 57 processTemplate(context, templateFileInfo.getTemplateFile(), |
| 58 Files.walkFileTree(parser.getBaseTemplateDir(), new SimpleFileVisito r<Path>() { | 58 templateFileInfo.getOutputFile(), api); |
|
mikecase (-- gone --)
2016/08/03 20:04:27
This walkFileTree line isnt working on the WebRTC
jbudorick
2016/08/03 22:24:03
Java version?
mikecase (-- gone --)
2016/08/03 22:29:55
Maybe. Also, they mentioned they set up some symli
| |
| 59 @Override | 59 } |
| 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) { | 60 } catch (IOException e) { |
| 69 System.err.println("Error processing template files for Robolectric! " + e.toString()); | 61 System.err.println("Error processing template files for Robolectric! " + e.toString()); |
| 70 } | 62 } |
| 71 } | 63 } |
| 72 | 64 |
| 73 private static void processTemplate(VelocityContext context, Path templateFi le, Path baseTemplateDir, Path outputDir, int api_level) throws IOException { | 65 private static void processTemplate(VelocityContext context, Path templateFi le, Path outputFile, int api_level) throws IOException { |
|
jbudorick
2016/08/03 22:24:03
line length
mikecase (-- gone --)
2016/08/03 22:29:55
Done
| |
| 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 |