Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /// | 1 /// |
| 2 /// Script to create boilerplate for a Polymer element. | 2 /// Script to create boilerplate for a Polymer element. |
| 3 /// Produces new .html entry point for a polymer app and updates the | 3 /// Produces new .html entry point for a polymer app and updates the |
| 4 /// pubspec.yaml to reflect it. | 4 /// pubspec.yaml to reflect it. |
| 5 /// | 5 /// |
| 6 /// Run this script with pub run: | 6 /// Run this script with pub run: |
| 7 /// | 7 /// |
| 8 /// pub run polymer:new_entry <html_file> | 8 /// pub run polymer:new_entry <html_file> |
| 9 /// | 9 /// |
| 10 import 'dart:io'; | 10 import 'dart:io'; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 <script type="application/dart">export 'package:polymer/init.dart';</script> | 77 <script type="application/dart">export 'package:polymer/init.dart';</script> |
| 78 </body> | 78 </body> |
| 79 </html> | 79 </html> |
| 80 '''; | 80 '''; |
| 81 | 81 |
| 82 new File(path.join(pubspecDir, entryPoint)).writeAsStringSync(html); | 82 new File(path.join(pubspecDir, entryPoint)).writeAsStringSync(html); |
| 83 | 83 |
| 84 var pubspecPath = path.join(pubspecDir, 'pubspec.yaml'); | 84 var pubspecPath = path.join(pubspecDir, 'pubspec.yaml'); |
| 85 var pubspecText = new File(pubspecPath).readAsStringSync(); | 85 var pubspecText = new File(pubspecPath).readAsStringSync(); |
| 86 var transformers = loadYaml(pubspecText)['transformers']; | 86 var transformers = loadYaml(pubspecText)['transformers']; |
| 87 var entryPoints; | 87 var entryPoints = []; |
| 88 | 88 |
| 89 var insertionPoint; | 89 var insertionPoint; |
| 90 var textToInsert = ''; | 90 var textToInsert = ''; |
| 91 | 91 |
| 92 if (transformers != null) { | 92 if (transformers != null) { |
| 93 // If there are transformers in the pubspec, look for the polymer | 93 // If there are transformers in the pubspec, look for the polymer |
| 94 // transformers, get the entry points, and delete the old entry points. | 94 // transformers, get the entry points, and delete the old entry points. |
| 95 var transformersSourceSpan = transformers.span; | 95 var transformersSourceSpan = transformers.span; |
| 96 SourceSpan sourceSpan; | 96 SourceSpan sourceSpan; |
| 97 | 97 |
| 98 for (var e in transformers) { | 98 for (var e in transformers) { |
| 99 if (e != 'polymer' && (e is! YamlMap || e['polymer'] == null)) continue; | 99 if (e != 'polymer' && (e is! YamlMap || e['polymer'] == null)) continue; |
| 100 if (e == 'polymer' || !e['polymer'].containsKey('entry_points')) { | 100 if (e == 'polymer' || !e['polymer'].containsKey('entry_points')) { |
| 101 if (path.split(entryPoint)[0] != 'web') { | 101 if (path.split(entryPoint)[0] != 'web') { |
| 102 print('WARNING: Did not add entry_point $entryPoint to pubspec.yaml' | 102 print('WARNING: Did not add entry_point $entryPoint to pubspec.yaml' |
| 103 ' because of already-existing transformer|polymer section'); | 103 ' because of already-existing transformer|polymer section'); |
| 104 } | 104 } |
| 105 return false; | 105 return false; |
| 106 } else if (e['polymer'].keys.length > 1) { | 106 } else if (e['polymer'].keys.length > 1) { |
| 107 // TODO(dgrove): handle the case where there are additional sections | 107 // TODO(dgrove): handle the case where there are additional sections |
| 108 // in the polymer transformer. | 108 // in the polymer transformer. |
| 109 throw new UnimplementedError('Cannot handle non-entry_point entries ' | 109 throw new UnimplementedError('Cannot handle non-entry_point entries ' |
| 110 'for polymer transformer'); | 110 'for polymer transformer'); |
| 111 } else { | 111 } else { |
| 112 var existing = e['polymer']['entry_points']; | 112 var existing = e['polymer']['entry_points']; |
| 113 entryPoints = existing == null ? [] : | 113 entryPoints = (existing == null ? [] : |
| 114 (existing is String ? [existing] : existing.toList()); | 114 (existing is String ? [existing] : existing.toList())); |
| 115 | 115 |
| 116 if (entryPoints.contains(entryPoint)) return false; | 116 if (entryPoints.contains(entryPoint)) return false; |
| 117 entryPoints.add(entryPoint); | 117 entryPoints.add(entryPoint); |
| 118 | 118 |
| 119 sourceSpan = e.span; | 119 sourceSpan = e.span; |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 if (sourceSpan == null) { | 123 if (sourceSpan == null) { |
| 124 // There were no polymer transformers. | 124 // There were no polymer transformers. |
| 125 insertionPoint = transformersSourceSpan.start.offset; | 125 insertionPoint = transformersSourceSpan.start.offset; |
| 126 textToInsert = '- '; | 126 textToInsert = '- '; |
| 127 } else { | 127 } else { |
| 128 insertionPoint = sourceSpan.start.offset; | 128 insertionPoint = sourceSpan.start.offset; |
| 129 pubspecText = '${pubspecText.substring(0, insertionPoint)}' | 129 pubspecText = '${pubspecText.substring(0, insertionPoint)}' |
| 130 '${pubspecText.substring(sourceSpan.end.offset)}'; | 130 '${pubspecText.substring(sourceSpan.end.offset)}'; |
| 131 } | 131 } |
| 132 } else { | 132 } else { |
| 133 // There were no transformers at all. | 133 // There were no transformers at all. |
| 134 insertionPoint = pubspecText.length; | 134 insertionPoint = pubspecText.length; |
| 135 textToInsert = 'transformers:\n- '; | 135 textToInsert = 'transformers:\n- '; |
| 136 entryPoints.add(entryPoint); | |
|
Siggi Cherem (dart-lang)
2014/08/13 21:56:30
alternatively, you could remove the initialization
| |
| 136 } | 137 } |
| 137 | 138 |
| 138 // TODO(dgrove): Once dartbug.com/20409 is addressed, use that here. | 139 // TODO(dgrove): Once dartbug.com/20409 is addressed, use that here. |
| 139 var entryPointsText = entryPoints.map((e) => ' - $e').join('\n'); | 140 var entryPointsText = entryPoints.map((e) => ' - $e').join('\n'); |
| 140 | 141 |
| 141 textToInsert = | 142 textToInsert = |
| 142 '''${textToInsert}polymer: | 143 '''${textToInsert}polymer: |
| 143 entry_points: | 144 entry_points: |
| 144 $entryPointsText'''; | 145 $entryPointsText'''; |
| 145 | 146 |
| 146 | 147 |
| 147 if (insertionPoint == pubspecText.length) { | 148 if (insertionPoint == pubspecText.length) { |
| 148 pubspecText = '${pubspecText}${textToInsert}'; | 149 pubspecText = '${pubspecText}${textToInsert}'; |
| 149 } else { | 150 } else { |
| 150 pubspecText = '${pubspecText.substring(0, insertionPoint)}' | 151 pubspecText = '${pubspecText.substring(0, insertionPoint)}' |
| 151 '${textToInsert}\n${pubspecText.substring(insertionPoint)}'; | 152 '${textToInsert}\n${pubspecText.substring(insertionPoint)}'; |
| 152 } | 153 } |
| 153 | 154 |
| 154 _writePubspec(pubspecPath, pubspecText); | 155 _writePubspec(pubspecPath, pubspecText); |
| 155 return true; | 156 return true; |
| 156 } | 157 } |
| 157 | 158 |
| 158 _writePubspec(String pubspecPath, String text) { | 159 _writePubspec(String pubspecPath, String text) { |
| 159 new File(pubspecPath).writeAsStringSync(text); | 160 new File(pubspecPath).writeAsStringSync(text); |
| 160 } | 161 } |
| OLD | NEW |