Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(548)

Side by Side Diff: pkg/polymer/bin/new_entry.dart

Issue 683883004: added support for more types of transformer sections in new_entry script (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: move todo Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 var polymerRegex = new RegExp(r'\n\s*-\spolymer\s*');
129 ' because of already-existing transformer|polymer section'); 128 // Insert right after the newline.
129 insertionPoint = pubspecText.indexOf(polymerRegex) + 1;
130 pubspecText = pubspecText.replaceFirst(polymerRegex, '\n');
131 } else if (e is YamlMap && e['polymer'] != null) {
132 polymerTransformerSourceSpan = e['polymer'].span;
133
134 var existing = e['polymer']['entry_points'];
135 if (existing == null && e['polymer'].containsKey('entry_points')) {
136 if (path.split(entryPoint)[0] != 'web') {
137 print('WARNING: Did not add entry_point $entryPoint to pubspec.yaml'
138 ' because of existing empty `entry_points` field in polymer'
139 ' transformer. This defaults to treating all files under `web/`'
140 ' as entry points, but you tried to add an entry point outside of'
141 ' the `web/` folder. You will need to explicitly list all entry'
142 ' points that you care about into your pubspec in order to'
143 ' include any outside of `web/`.');
144 }
145 return false;
130 } 146 }
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 ? [] : 147 entryPoints = (existing == null ? [] :
140 (existing is String ? [existing] : existing.toList())); 148 (existing is String ? [existing] : existing.toList()));
141 149
142 if (entryPoints.contains(entryPoint)) return false; 150 if (entryPoints.contains(entryPoint)) return false;
143 entryPoints.add(entryPoint); 151 entryPoints.add(entryPoint);
144 152
145 sourceSpan = e.span; 153 if (existing != null) {
154 entryPointsSourceSpan = existing.span;
155 }
146 } 156 }
147 } 157 }
148 158
149 if (sourceSpan == null) { 159 if (polymerTransformerSourceSpan == null) {
150 // There were no polymer transformers. 160 if (insertionPoint == null) {
151 insertionPoint = transformersSourceSpan.start.offset; 161 insertionPoint = transformersSourceSpan.start.offset;
152 textToInsert = '- '; 162 }
163 textToInsert = '- polymer:\n entry_points:\n';
164 } else if (entryPointsSourceSpan == null) {
165 insertionPoint = polymerTransformerSourceSpan.start.offset;
166 textToInsert = ' entry_points:\n';
153 } else { 167 } else {
154 insertionPoint = sourceSpan.start.offset; 168 insertionPoint = entryPointsSourceSpan.start.offset;
155 pubspecText = '${pubspecText.substring(0, insertionPoint)}' 169 pubspecText = '${pubspecText.substring(0, insertionPoint)}'
156 '${pubspecText.substring(sourceSpan.end.offset)}'; 170 '${pubspecText.substring(entryPointsSourceSpan.end.offset)}';
157 } 171 }
158 } else { 172 } else {
159 // There were no transformers at all. 173 // There were no transformers at all.
160 insertionPoint = pubspecText.length; 174 insertionPoint = pubspecText.length;
161 var optionalNewline = pubspecText.endsWith('\n') ? '' : '\n'; 175 var optionalNewline = pubspecText.endsWith('\n') ? '' : '\n';
162 textToInsert = '${optionalNewline}transformers:\n- '; 176 textToInsert = '''
177 ${optionalNewline}transformers:
178 - polymer:
179 entry_points:
180 ''';
163 entryPoints = [entryPoint]; 181 entryPoints = [entryPoint];
164 } 182 }
165 183
184 if (entryPoints == null) entryPoints = [entryPoint];
166 // TODO(dgrove): Once dartbug.com/20409 is addressed, use that here. 185 // TODO(dgrove): Once dartbug.com/20409 is addressed, use that here.
167 var entryPointsText = entryPoints.map((e) => ' - $e').join('\n'); 186 var entryPointsText = entryPoints.map((e) => ' - $e').join('\n');
168 187
169 textToInsert = 188 textToInsert += entryPointsText;
170 '''${textToInsert}polymer:
171 entry_points:
172 $entryPointsText''';
173
174
175 if (insertionPoint == pubspecText.length) { 189 if (insertionPoint == pubspecText.length) {
176 pubspecText = '${pubspecText}${textToInsert}'; 190 pubspecText = '${pubspecText}${textToInsert}';
177 } else { 191 } else {
178 pubspecText = '${pubspecText.substring(0, insertionPoint)}' 192 pubspecText = '${pubspecText.substring(0, insertionPoint)}'
179 '${textToInsert}\n${pubspecText.substring(insertionPoint)}'; 193 '${textToInsert}\n${pubspecText.substring(insertionPoint)}';
180 } 194 }
181 195
182 _writePubspec(pubspecPath, pubspecText); 196 _writePubspec(pubspecPath, pubspecText);
183 return true; 197 return true;
184 } 198 }
185 199
186 _writePubspec(String pubspecPath, String text) { 200 _writePubspec(String pubspecPath, String text) {
187 new File(pubspecPath).writeAsStringSync(text); 201 new File(pubspecPath).writeAsStringSync(text);
188 } 202 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698