OLD | NEW |
1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 /// Command line tool to merge the SDK libraries and our patch files. | 6 /// Command line tool to merge the SDK libraries and our patch files. |
7 /// This is currently designed as an offline tool, but we could automate it. | 7 /// This is currently designed as an offline tool, but we could automate it. |
8 library dev_compiler.tool.patch_sdk; | 8 library dev_compiler.tool.patch_sdk; |
9 | 9 |
10 import 'dart:io'; | 10 import 'dart:io'; |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 | 181 |
182 /// Merge `@patch` declarations into `external` declarations. | 182 /// Merge `@patch` declarations into `external` declarations. |
183 class PatchApplier extends GeneralizingAstVisitor { | 183 class PatchApplier extends GeneralizingAstVisitor { |
184 final StringEditBuffer edits; | 184 final StringEditBuffer edits; |
185 final PatchFinder patch; | 185 final PatchFinder patch; |
186 | 186 |
187 bool _isLibrary = true; // until proven otherwise. | 187 bool _isLibrary = true; // until proven otherwise. |
188 | 188 |
189 PatchApplier(this.edits, this.patch); | 189 PatchApplier(this.edits, this.patch); |
190 | 190 |
191 @override visitCompilationUnit(CompilationUnit node) { | 191 @override |
| 192 visitCompilationUnit(CompilationUnit node) { |
192 super.visitCompilationUnit(node); | 193 super.visitCompilationUnit(node); |
193 if (_isLibrary) _mergeUnpatched(node); | 194 if (_isLibrary) _mergeUnpatched(node); |
194 } | 195 } |
195 | 196 |
196 void _merge(AstNode node, int pos) { | 197 void _merge(AstNode node, int pos) { |
197 var code = patch.contents.substring(node.offset, node.end); | 198 var code = patch.contents.substring(node.offset, node.end); |
198 edits.insert(pos, '\n' + code); | 199 edits.insert(pos, '\n' + code); |
199 } | 200 } |
200 | 201 |
201 /// Merges directives and declarations that are not `@patch` into the library. | 202 /// Merges directives and declarations that are not `@patch` into the library. |
(...skipping 15 matching lines...) Expand all Loading... |
217 _merge(d, partPos); | 218 _merge(d, partPos); |
218 } | 219 } |
219 | 220 |
220 // Merge declarations from the patch | 221 // Merge declarations from the patch |
221 int declPos = edits.original.length; | 222 int declPos = edits.original.length; |
222 for (var d in patch.mergeDeclarations) { | 223 for (var d in patch.mergeDeclarations) { |
223 _merge(d, declPos); | 224 _merge(d, declPos); |
224 } | 225 } |
225 } | 226 } |
226 | 227 |
227 @override visitPartOfDirective(PartOfDirective node) { | 228 @override |
| 229 visitPartOfDirective(PartOfDirective node) { |
228 _isLibrary = false; | 230 _isLibrary = false; |
229 } | 231 } |
230 | 232 |
231 @override visitFunctionDeclaration(FunctionDeclaration node) { | 233 @override |
| 234 visitFunctionDeclaration(FunctionDeclaration node) { |
232 _maybePatch(node); | 235 _maybePatch(node); |
233 } | 236 } |
234 | 237 |
235 /// Merge patches and extensions into the class | 238 /// Merge patches and extensions into the class |
236 @override visitClassDeclaration(ClassDeclaration node) { | 239 @override |
| 240 visitClassDeclaration(ClassDeclaration node) { |
237 node.members.forEach(_maybePatch); | 241 node.members.forEach(_maybePatch); |
238 | 242 |
239 var mergeMembers = patch.mergeMembers[_qualifiedName(node)]; | 243 var mergeMembers = patch.mergeMembers[_qualifiedName(node)]; |
240 if (mergeMembers == null) return; | 244 if (mergeMembers == null) return; |
241 | 245 |
242 // Merge members from the patch | 246 // Merge members from the patch |
243 var pos = node.members.last.end; | 247 var pos = node.members.last.end; |
244 for (var member in mergeMembers) { | 248 for (var member in mergeMembers) { |
245 var code = patch.contents.substring(member.offset, member.end); | 249 var code = patch.contents.substring(member.offset, member.end); |
246 edits.insert(pos, '\n\n ' + code); | 250 edits.insert(pos, '\n\n ' + code); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 final Map patches = <String, Declaration>{}; | 282 final Map patches = <String, Declaration>{}; |
279 final Map mergeMembers = <String, List<ClassMember>>{}; | 283 final Map mergeMembers = <String, List<ClassMember>>{}; |
280 final List mergeDeclarations = <CompilationUnitMember>[]; | 284 final List mergeDeclarations = <CompilationUnitMember>[]; |
281 | 285 |
282 PatchFinder.parseAndVisit(String contents) | 286 PatchFinder.parseAndVisit(String contents) |
283 : contents = contents, | 287 : contents = contents, |
284 unit = parseCompilationUnit(contents) { | 288 unit = parseCompilationUnit(contents) { |
285 visitCompilationUnit(unit); | 289 visitCompilationUnit(unit); |
286 } | 290 } |
287 | 291 |
288 @override visitCompilationUnitMember(CompilationUnitMember node) { | 292 @override |
| 293 visitCompilationUnitMember(CompilationUnitMember node) { |
289 mergeDeclarations.add(node); | 294 mergeDeclarations.add(node); |
290 } | 295 } |
291 | 296 |
292 @override visitClassDeclaration(ClassDeclaration node) { | 297 @override |
| 298 visitClassDeclaration(ClassDeclaration node) { |
293 if (_isPatch(node)) { | 299 if (_isPatch(node)) { |
294 var members = <ClassMember>[]; | 300 var members = <ClassMember>[]; |
295 for (var member in node.members) { | 301 for (var member in node.members) { |
296 if (_isPatch(member)) { | 302 if (_isPatch(member)) { |
297 patches[_qualifiedName(member)] = member; | 303 patches[_qualifiedName(member)] = member; |
298 } else { | 304 } else { |
299 members.add(member); | 305 members.add(member); |
300 } | 306 } |
301 } | 307 } |
302 if (members.isNotEmpty) { | 308 if (members.isNotEmpty) { |
303 mergeMembers[_qualifiedName(node)] = members; | 309 mergeMembers[_qualifiedName(node)] = members; |
304 } | 310 } |
305 } else { | 311 } else { |
306 mergeDeclarations.add(node); | 312 mergeDeclarations.add(node); |
307 } | 313 } |
308 } | 314 } |
309 | 315 |
310 @override visitFunctionDeclaration(FunctionDeclaration node) { | 316 @override |
| 317 visitFunctionDeclaration(FunctionDeclaration node) { |
311 if (_isPatch(node)) { | 318 if (_isPatch(node)) { |
312 patches[_qualifiedName(node)] = node; | 319 patches[_qualifiedName(node)] = node; |
313 } else { | 320 } else { |
314 mergeDeclarations.add(node); | 321 mergeDeclarations.add(node); |
315 } | 322 } |
316 } | 323 } |
317 | 324 |
318 @override visitFunctionBody(node) {} // skip method bodies | 325 @override |
| 326 visitFunctionBody(node) {} // skip method bodies |
319 } | 327 } |
320 | 328 |
321 String _qualifiedName(Declaration node) { | 329 String _qualifiedName(Declaration node) { |
322 var parent = node.parent; | 330 var parent = node.parent; |
323 var className = ''; | 331 var className = ''; |
324 if (parent is ClassDeclaration) { | 332 if (parent is ClassDeclaration) { |
325 className = parent.name.name + '.'; | 333 className = parent.name.name + '.'; |
326 } | 334 } |
327 var name = (node as dynamic).name; | 335 var name = (node as dynamic).name; |
328 return className + (name != null ? name.name : ''); | 336 return className + (name != null ? name.name : ''); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 if (diff != 0) return diff; | 432 if (diff != 0) return diff; |
425 return end - other.end; | 433 return end - other.end; |
426 } | 434 } |
427 } | 435 } |
428 | 436 |
429 List<SdkLibrary> _getSdkLibraries(String contents) { | 437 List<SdkLibrary> _getSdkLibraries(String contents) { |
430 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); | 438 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); |
431 parseCompilationUnit(contents).accept(libraryBuilder); | 439 parseCompilationUnit(contents).accept(libraryBuilder); |
432 return libraryBuilder.librariesMap.sdkLibraries; | 440 return libraryBuilder.librariesMap.sdkLibraries; |
433 } | 441 } |
OLD | NEW |