OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #library('templatetool'); |
| 6 |
| 7 #import('dart:io'); |
| 8 #import('template.dart'); |
| 9 #import('../../frog/file_system.dart'); |
| 10 #import('../../frog/file_system_vm.dart'); |
| 11 |
| 12 |
| 13 FileSystem files; |
| 14 |
| 15 /** Invokes [callback] and returns how long it took to execute in ms. */ |
| 16 num time(callback()) { |
| 17 final watch = new Stopwatch(); |
| 18 watch.start(); |
| 19 callback(); |
| 20 watch.stop(); |
| 21 return watch.elapsedInMs(); |
| 22 } |
| 23 |
| 24 // Color constants used for generating messages. |
| 25 String _GREEN_COLOR = '\u001b[32m'; |
| 26 String _NO_COLOR = '\u001b[0m'; |
| 27 |
| 28 printStats(String phase, num elapsed, [String filename = '']) { |
| 29 print('${phase}${_GREEN_COLOR} ${filename}${_NO_COLOR} in ${elapsed} msec.'); |
| 30 } |
| 31 |
| 32 /** |
| 33 * Run from the `utils/css` directory. |
| 34 */ |
| 35 void main() { |
| 36 // argument 0 - sourcefile full path |
| 37 // argument 1 - outputfile full path |
| 38 var optionArgs = new Options().arguments; |
| 39 |
| 40 String sourceFullFn = optionArgs[0]; |
| 41 String outputFullFn = optionArgs[1]; |
| 42 |
| 43 String sourcePath; |
| 44 String sourceFilename; |
| 45 int idxBeforeFilename = sourceFullFn.lastIndexOf('/'); |
| 46 if (idxBeforeFilename >= 0) { |
| 47 sourcePath = sourceFullFn.substring(0, idxBeforeFilename + 1); |
| 48 sourceFilename = sourceFullFn.substring(idxBeforeFilename + 1); |
| 49 } |
| 50 |
| 51 String outPath; |
| 52 String outFilename; |
| 53 idxBeforeFilename = outputFullFn.lastIndexOf('/'); |
| 54 if (idxBeforeFilename >= 0) { |
| 55 outPath = outputFullFn.substring(0, idxBeforeFilename + 1); |
| 56 outFilename = outputFullFn.substring(idxBeforeFilename + 1); |
| 57 } |
| 58 |
| 59 if (sourceFilename.length == 0 || outFilename.length == 0) { |
| 60 print("Unknown command:\r"); |
| 61 print(" Format: sourcefile outputfile [--options]"); |
| 62 print(" outputfile - template file filename.tmpl"); |
| 63 print(" outputfile - generated dart source file filename.dart"); |
| 64 return; |
| 65 } |
| 66 |
| 67 // files = new NodeFileSystem(); |
| 68 files = new VMFileSystem(); |
| 69 |
| 70 // TODO(terry): Cleanup options handling need common options between template |
| 71 // and CSS parsers also cleanup above cruft. |
| 72 |
| 73 // TODO(terry): Pass on switches. |
| 74 var args = []; |
| 75 parseOptions(args, files); |
| 76 |
| 77 initHtmlWorld(false); |
| 78 |
| 79 if (!files.fileExists(sourceFullFn)) { |
| 80 // Display colored error message if file is missing. |
| 81 print(world.fatal("CSS source file missing - ${sourceFullFn}")); |
| 82 } else { |
| 83 |
| 84 String source = files.readAll(sourceFullFn); |
| 85 |
| 86 List<Template> templates; |
| 87 final parsedElapsed = time(() { |
| 88 templates = templateParseAndValidate(source); |
| 89 }); |
| 90 |
| 91 StringBuffer code = new StringBuffer(); |
| 92 |
| 93 num codegenElapsed; |
| 94 if (world.errors == 0) { |
| 95 // Generate the Dart class(es) for all template(s). |
| 96 codegenElapsed = time(() { |
| 97 code.add(Codegen.generate(templates, outFilename)); |
| 98 }); |
| 99 } |
| 100 |
| 101 printStats("Parsed", parsedElapsed, sourceFullFn); |
| 102 printStats("Codegen", codegenElapsed, sourceFullFn); |
| 103 |
| 104 final outputElapsed = time(() { |
| 105 files.writeString(outputFullFn, code.toString()); |
| 106 }); |
| 107 |
| 108 printStats("Wrote file", codegenElapsed, outputFullFn); |
| 109 } |
| 110 } |
OLD | NEW |