| 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 '../lib/file_system.dart'; | |
| 10 import '../lib/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.elapsedMilliseconds; | |
| 22 } | |
| 23 | |
| 24 String GREEN_COLOR = '\u001b[32m'; | |
| 25 String NO_COLOR = '\u001b[0m'; | |
| 26 | |
| 27 printStats(String phase, num elapsed, [String filename = '']) { | |
| 28 print('${phase} ${GREEN_COLOR}${filename}${NO_COLOR} in ${elapsed} msec.'); | |
| 29 } | |
| 30 | |
| 31 /** | |
| 32 * Run from the `utils/css` directory. | |
| 33 */ | |
| 34 void main(List<String> optionArgs) { | |
| 35 // argument 0 - sourcefile full path | |
| 36 // argument 1 - outputfile full path | |
| 37 String sourceFullFn = optionArgs[0]; | |
| 38 String outputFullFn = optionArgs[1]; | |
| 39 | |
| 40 String sourcePath; | |
| 41 String sourceFilename; | |
| 42 int idxBeforeFilename = sourceFullFn.lastIndexOf('/'); | |
| 43 if (idxBeforeFilename >= 0) { | |
| 44 sourcePath = sourceFullFn.substring(0, idxBeforeFilename + 1); | |
| 45 sourceFilename = sourceFullFn.substring(idxBeforeFilename + 1); | |
| 46 } | |
| 47 | |
| 48 String outPath; | |
| 49 String outFilename; | |
| 50 idxBeforeFilename = outputFullFn.lastIndexOf('/'); | |
| 51 if (idxBeforeFilename >= 0) { | |
| 52 outPath = outputFullFn.substring(0, idxBeforeFilename + 1); | |
| 53 outFilename = outputFullFn.substring(idxBeforeFilename + 1); | |
| 54 } | |
| 55 | |
| 56 if (sourceFilename.length == 0 || outFilename.length == 0) { | |
| 57 print("Unknown command:\r"); | |
| 58 print(" Format: sourcefile outputfile [--options]"); | |
| 59 print(" outputfile - template file filename.tmpl"); | |
| 60 print(" outputfile - generated dart source file filename.dart"); | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 // files = new NodeFileSystem(); | |
| 65 files = new VMFileSystem(); | |
| 66 | |
| 67 // TODO(terry): Cleanup options handling need common options between template | |
| 68 // and CSS parsers also cleanup above cruft. | |
| 69 | |
| 70 // TODO(terry): Pass on switches. | |
| 71 var args = []; | |
| 72 parseOptions(args, files); | |
| 73 | |
| 74 initHtmlWorld(false); | |
| 75 | |
| 76 if (!files.fileExists(sourceFullFn)) { | |
| 77 // Display colored error message if file is missing. | |
| 78 print(world.fatal("CSS source file missing - ${sourceFullFn}")); | |
| 79 } else { | |
| 80 | |
| 81 String source = files.readAll(sourceFullFn); | |
| 82 | |
| 83 List<Template> templates; | |
| 84 final parsedElapsed = time(() { | |
| 85 templates = templateParseAndValidate(source); | |
| 86 }); | |
| 87 | |
| 88 StringBuffer code = new StringBuffer(); | |
| 89 | |
| 90 num codegenElapsed; | |
| 91 if (world.errors == 0) { | |
| 92 // Generate the Dart class(es) for all template(s). | |
| 93 codegenElapsed = time(() { | |
| 94 code.write(Codegen.generate(templates, outFilename)); | |
| 95 }); | |
| 96 } | |
| 97 | |
| 98 printStats("Parsed", parsedElapsed, sourceFullFn); | |
| 99 printStats("Codegen", codegenElapsed, sourceFullFn); | |
| 100 | |
| 101 final outputElapsed = time(() { | |
| 102 files.writeString(outputFullFn, code.toString()); | |
| 103 }); | |
| 104 | |
| 105 printStats("Wrote file", codegenElapsed, outputFullFn); | |
| 106 } | |
| 107 } | |
| OLD | NEW |