| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, 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 import 'dart:io'; |
| 6 |
| 7 // TODO(jmesserly): should be importing a compiler mirrors package. |
| 8 // This is currently generated by the shell script. |
| 9 import 'compile_mirrors.dart'; |
| 10 |
| 11 // TODO(jmesserly): pub is not creating a "packages" for us. |
| 12 import '../lib/src/html5_utils.dart' show htmlElementNames; |
| 13 |
| 14 const MY_NAME = 'build/gen_html_setters.sh'; |
| 15 const OUTPUT_NAME = 'lib/src/html5_setters.g.dart'; |
| 16 |
| 17 /** |
| 18 * Given a field name in `dart:html` gets the HTML attribute name. |
| 19 */ |
| 20 const fieldToAttribute = const { |
| 21 'acceptCharset': 'accept-charset', |
| 22 'defaultValue': 'default', |
| 23 // Yuck. http://dartbug.com/4550 |
| 24 'webkitdropzone': 'dropzone', |
| 25 'htmlFor': 'for', |
| 26 'httpEquiv': 'http-equiv', |
| 27 }; |
| 28 |
| 29 |
| 30 main() { |
| 31 print(''' |
| 32 This script uses compile time mirrors to get information from dart:html. |
| 33 The information is used to create "$OUTPUT_NAME". |
| 34 '''); |
| 35 |
| 36 var sdk = Platform.environment['DART_SDK']; |
| 37 if (sdk == null) { |
| 38 print('DART_SDK path must be set. Use gen_html_setters.sh.'); |
| 39 exit(1); |
| 40 } |
| 41 |
| 42 var htmlPath = new Path('dart:html'); |
| 43 var libPath = new Path(sdk).append('lib'); |
| 44 var pkgPath = new Path(sdk).append('pkg'); |
| 45 |
| 46 var mirrors = new Compilation.library([htmlPath], libPath, pkgPath).mirrors; |
| 47 var html = mirrors.libraries['html']; |
| 48 |
| 49 |
| 50 var extendsCode = new StringBuffer(); |
| 51 extendsCode.add('var htmlElementExtends = const {\n'); |
| 52 |
| 53 var code = new StringBuffer(); |
| 54 code.add('// This file is autogenerated by $MY_NAME. Do not edit.\n'); |
| 55 code.add('library html5_setters;\n'); |
| 56 |
| 57 var elemSet = new Set(); |
| 58 void addSuperInterfaces(ClassMirror cls) { |
| 59 var name = cls.displayName; |
| 60 if (name.endsWith('Element') && !elemSet.contains(name)) { |
| 61 elemSet.add(name); |
| 62 cls.superinterfaces.forEach(addSuperInterfaces); |
| 63 } |
| 64 } |
| 65 for (var name in htmlElementNames.values) { |
| 66 addSuperInterfaces(html.classes[name]); |
| 67 } |
| 68 addSuperInterfaces(html.classes['UnknownElement']); |
| 69 |
| 70 code.add('var htmlElementFields = const {\n'); |
| 71 var elements = new List.from(elemSet); |
| 72 elements.sort(); |
| 73 for (var element in elements) { |
| 74 var cls = html.classes[element]; |
| 75 var setters = []; |
| 76 |
| 77 var interfaces = cls.superinterfaces.filter( |
| 78 (i) => i.displayName.endsWith('Element')); |
| 79 |
| 80 if (interfaces.length > 1) { |
| 81 print('ERROR: $element has more than one Element superinterface:\n' |
| 82 ' $interfaces'); |
| 83 exit(2); |
| 84 } else if (interfaces.length > 0) { |
| 85 extendsCode.add(" '$element': '${interfaces[0].displayName}',\n"); |
| 86 } |
| 87 |
| 88 // TODO(jmesserly): using "cls.setters" does not seem to work |
| 89 for (var member in cls.members.values) { |
| 90 if (member.simpleName.startsWith(r'$')) continue; |
| 91 if (member is! VariableMirror) continue; |
| 92 if (member.isFinal || member.isConst) continue; |
| 93 setters.add(member.displayName); |
| 94 } |
| 95 |
| 96 if (setters.length == 0) continue; |
| 97 |
| 98 code.add(" '$element': const {\n"); |
| 99 for (var setter in setters) { |
| 100 var attr = fieldToAttribute[setter]; |
| 101 if (attr == null) attr = setter.toLowerCase(); |
| 102 code.add(" '$attr': '$setter',\n"); |
| 103 } |
| 104 code.add(' },\n'); |
| 105 } |
| 106 code.add('};\n\n'); |
| 107 extendsCode.add('};\n'); |
| 108 code.add(extendsCode.toString()); |
| 109 |
| 110 var outPath = new Path(new Options().script).directoryPath |
| 111 .append('..').append(OUTPUT_NAME); |
| 112 |
| 113 new File.fromPath(outPath) |
| 114 .openSync(FileMode.WRITE) |
| 115 ..writeStringSync(code.toString()) |
| 116 ..closeSync(); |
| 117 |
| 118 print('Wrote $outPath.'); |
| 119 } |
| OLD | NEW |