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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 | 87 |
| 88 // Returns true if the pubspec file was modified. It might not be modified if | 88 // Returns true if the pubspec file was modified. It might not be modified if |
| 89 // there was a monolithic polymer transformer in the pubspec, or if the entry | 89 // there was a monolithic polymer transformer in the pubspec, or if the entry |
| 90 // point for some reason already existed in the pubspec. | 90 // point for some reason already existed in the pubspec. |
| 91 bool _createBoilerPlate(String entryPoint, String pubspecDir) { | 91 bool _createBoilerPlate(String entryPoint, String pubspecDir) { |
| 92 | 92 |
| 93 String html = ''' | 93 String html = ''' |
| 94 <!doctype html> | 94 <!doctype html> |
| 95 <html> | 95 <html> |
| 96 <head> | 96 <head> |
| 97 <script src="packages/web_components/dart_support.js"></script> | |
| 98 | |
| 99 <!-- link rel="import" href="path_to_html_import.html" --> | 97 <!-- link rel="import" href="path_to_html_import.html" --> |
| 100 </head> | 98 </head> |
| 101 <body> | 99 <body> |
| 102 <!-- HTML for body here --> | 100 <!-- HTML for body here --> |
| 103 <script type="application/dart">export 'package:polymer/init.dart';</script> | 101 <script type="application/dart">export 'package:polymer/init.dart';</script> |
| 104 </body> | 102 </body> |
| 105 </html> | 103 </html> |
| 106 '''; | 104 '''; |
| 107 | 105 |
| 108 new File(path.join(pubspecDir, entryPoint)).writeAsStringSync(html); | 106 new File(path.join(pubspecDir, entryPoint)).writeAsStringSync(html); |
| 109 | 107 |
| 110 var pubspecPath = path.join(pubspecDir, 'pubspec.yaml'); | 108 var pubspecPath = path.join(pubspecDir, 'pubspec.yaml'); |
| 111 var pubspecText = new File(pubspecPath).readAsStringSync(); | 109 var pubspecText = new File(pubspecPath).readAsStringSync(); |
| 112 var transformers = loadYaml(pubspecText)['transformers']; | 110 var transformers = loadYaml(pubspecText)['transformers']; |
| 113 var entryPoints; | 111 var entryPoints; |
| 114 | 112 |
| 115 var insertionPoint; | 113 var insertionPoint; |
| 116 var textToInsert = ''; | 114 var textToInsert = ''; |
| 117 | 115 |
| 118 if (transformers != null) { | 116 if (transformers != null) { |
| 119 // If there are transformers in the pubspec, look for the polymer | 117 // If there are transformers in the pubspec, look for the polymer |
| 120 // transformers, get the entry points, and delete the old entry points. | 118 // transformers, get the entry points, and delete the old entry points. |
| 121 var transformersSourceSpan = transformers.span; | 119 SourceSpan transformersSourceSpan = transformers.span; |
| 122 SourceSpan sourceSpan; | |
| 123 | 120 |
| 121 SourceSpan polymerTransformerSourceSpan; | |
| 122 SourceSpan entryPointsSourceSpan; | |
| 124 for (var e in transformers) { | 123 for (var e in transformers) { |
| 125 if (e != 'polymer' && (e is! YamlMap || e['polymer'] == null)) continue; | 124 if (e == 'polymer') { |
| 126 if (e == 'polymer' || !e['polymer'].containsKey('entry_points')) { | 125 // If they had an empty polymer transformer, just get rid of it (we will |
| 127 if (path.split(entryPoint)[0] != 'web') { | 126 // replace it with our own map style one). |
| 128 print('WARNING: Did not add entry_point $entryPoint to pubspec.yaml' | 127 pubspecText = pubspecText.replaceFirst( |
| 129 ' because of already-existing transformer|polymer section'); | 128 new RegExp(r'\n\s*-\spolymer\s*'), ''); |
| 129 entryPoints = [entryPoint]; | |
|
Siggi Cherem (dart-lang)
2014/11/12 19:46:36
we should save the pubspec span location of "- pol
jakemac
2014/11/12 21:49:10
Done.
| |
| 130 } else if (e is YamlMap && e['polymer'] != null) { | |
| 131 polymerTransformerSourceSpan = e['polymer'].span; | |
| 132 | |
| 133 var existing = e['polymer']['entry_points']; | |
| 134 if (existing == null && e['polymer'].containsKey('entry_points')) { | |
| 135 if (path.split(entryPoint)[0] != 'web') { | |
| 136 print('WARNING: Did not add entry_point $entryPoint to pubspec.yaml' | |
| 137 ' because of existing empty `entry_points` field in polymer' | |
| 138 ' transformer. This defaults to treating all files under `web/`' | |
| 139 ' as entry points, but you tried to add an entry point outside of' | |
| 140 ' the `web/` folder. You will need to hardcode all entry points' | |
|
Siggi Cherem (dart-lang)
2014/11/12 19:46:36
nit: replace "hardcode" with "explicitly list" (ha
jakemac
2014/11/12 21:49:10
Done.
| |
| 141 ' that you care about into your pubspec in order to include any' | |
| 142 ' outside of `web/`.'); | |
| 143 } | |
| 144 return false; | |
| 130 } | 145 } |
| 131 return false; | |
| 132 } else if (e['polymer'].keys.length > 1) { | |
| 133 // TODO(dgrove): handle the case where there are additional sections | |
| 134 // in the polymer transformer. | |
| 135 throw new UnimplementedError('Cannot handle non-entry_point entries ' | |
| 136 'for polymer transformer'); | |
| 137 } else { | |
| 138 var existing = e['polymer']['entry_points']; | |
| 139 entryPoints = (existing == null ? [] : | 146 entryPoints = (existing == null ? [] : |
| 140 (existing is String ? [existing] : existing.toList())); | 147 (existing is String ? [existing] : existing.toList())); |
| 141 | 148 |
| 142 if (entryPoints.contains(entryPoint)) return false; | 149 if (entryPoints.contains(entryPoint)) return false; |
| 143 entryPoints.add(entryPoint); | 150 entryPoints.add(entryPoint); |
| 144 | 151 |
| 145 sourceSpan = e.span; | 152 if (existing != null) { |
| 153 entryPointsSourceSpan = existing.span; | |
| 154 } | |
| 146 } | 155 } |
| 147 } | 156 } |
| 148 | 157 |
| 149 if (sourceSpan == null) { | 158 if (polymerTransformerSourceSpan == null) { |
| 150 // There were no polymer transformers. | |
| 151 insertionPoint = transformersSourceSpan.start.offset; | 159 insertionPoint = transformersSourceSpan.start.offset; |
| 152 textToInsert = '- '; | 160 textToInsert = '- polymer:\n entry_points:\n'; |
| 161 } else if (entryPointsSourceSpan == null) { | |
| 162 insertionPoint = polymerTransformerSourceSpan.start.offset; | |
| 163 textToInsert = ' entry_points:\n'; | |
| 153 } else { | 164 } else { |
| 154 insertionPoint = sourceSpan.start.offset; | 165 insertionPoint = entryPointsSourceSpan.start.offset; |
| 155 pubspecText = '${pubspecText.substring(0, insertionPoint)}' | 166 pubspecText = '${pubspecText.substring(0, insertionPoint)}' |
| 156 '${pubspecText.substring(sourceSpan.end.offset)}'; | 167 '${pubspecText.substring(entryPointsSourceSpan.end.offset)}'; |
| 157 } | 168 } |
| 158 } else { | 169 } else { |
| 159 // There were no transformers at all. | 170 // There were no transformers at all. |
| 160 insertionPoint = pubspecText.length; | 171 insertionPoint = pubspecText.length; |
| 161 var optionalNewline = pubspecText.endsWith('\n') ? '' : '\n'; | 172 var optionalNewline = pubspecText.endsWith('\n') ? '' : '\n'; |
| 162 textToInsert = '${optionalNewline}transformers:\n- '; | 173 textToInsert = ''' |
| 174 ${optionalNewline}transformers: | |
| 175 - polymer: | |
| 176 entry_points: | |
| 177 '''; | |
| 163 entryPoints = [entryPoint]; | 178 entryPoints = [entryPoint]; |
| 164 } | 179 } |
| 165 | 180 |
| 166 // TODO(dgrove): Once dartbug.com/20409 is addressed, use that here. | 181 // TODO(dgrove): Once dartbug.com/20409 is addressed, use that here. |
| 167 var entryPointsText = entryPoints.map((e) => ' - $e').join('\n'); | 182 var entryPointsText = entryPoints.map((e) => ' - $e').join('\n'); |
| 168 | 183 |
| 169 textToInsert = | 184 textToInsert += entryPointsText; |
| 170 '''${textToInsert}polymer: | |
| 171 entry_points: | |
| 172 $entryPointsText'''; | |
| 173 | |
| 174 | |
| 175 if (insertionPoint == pubspecText.length) { | 185 if (insertionPoint == pubspecText.length) { |
| 176 pubspecText = '${pubspecText}${textToInsert}'; | 186 pubspecText = '${pubspecText}${textToInsert}'; |
| 177 } else { | 187 } else { |
| 178 pubspecText = '${pubspecText.substring(0, insertionPoint)}' | 188 pubspecText = '${pubspecText.substring(0, insertionPoint)}' |
| 179 '${textToInsert}\n${pubspecText.substring(insertionPoint)}'; | 189 '${textToInsert}\n${pubspecText.substring(insertionPoint)}'; |
| 180 } | 190 } |
| 181 | 191 |
| 182 _writePubspec(pubspecPath, pubspecText); | 192 _writePubspec(pubspecPath, pubspecText); |
| 183 return true; | 193 return true; |
| 184 } | 194 } |
| 185 | 195 |
| 186 _writePubspec(String pubspecPath, String text) { | 196 _writePubspec(String pubspecPath, String text) { |
| 187 new File(pubspecPath).writeAsStringSync(text); | 197 new File(pubspecPath).writeAsStringSync(text); |
| 188 } | 198 } |
| OLD | NEW |