| 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 ddc.tool.patch_sdk; | 8 library ddc.tool.patch_sdk; |
| 9 | 9 |
| 10 import 'dart:io'; | 10 import 'dart:io'; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 /// writing the dart:* libraries, and not a tool given to Dart developers, it | 101 /// writing the dart:* libraries, and not a tool given to Dart developers, it |
| 102 /// seems like a non-ideal situation. Instead we keep the preprocessing simple. | 102 /// seems like a non-ideal situation. Instead we keep the preprocessing simple. |
| 103 List<String> _patchLibrary(List<String> partsContents, String patchContents) { | 103 List<String> _patchLibrary(List<String> partsContents, String patchContents) { |
| 104 var results = <StringEditBuffer>[]; | 104 var results = <StringEditBuffer>[]; |
| 105 | 105 |
| 106 // Parse the patch first. We'll need to extract bits of this as we go through | 106 // Parse the patch first. We'll need to extract bits of this as we go through |
| 107 // the other files. | 107 // the other files. |
| 108 var patchFinder = new PatchFinder.parseAndVisit(patchContents); | 108 var patchFinder = new PatchFinder.parseAndVisit(patchContents); |
| 109 | 109 |
| 110 // Merge `external` declarations with the corresponding `@patch` code. | 110 // Merge `external` declarations with the corresponding `@patch` code. |
| 111 bool first = true; | |
| 112 for (var partContent in partsContents) { | 111 for (var partContent in partsContents) { |
| 113 var partEdits = new StringEditBuffer(partContent); | 112 var partEdits = new StringEditBuffer(partContent); |
| 114 var partUnit = parseCompilationUnit(partContent); | 113 var partUnit = parseCompilationUnit(partContent); |
| 115 partUnit.accept(new PatchApplier(partEdits, patchFinder)); | 114 partUnit.accept(new PatchApplier(partEdits, patchFinder)); |
| 116 results.add(partEdits); | 115 results.add(partEdits); |
| 117 } | 116 } |
| 118 return new List<String>.from(results.map((e) => e.toString())); | 117 return new List<String>.from(results.map((e) => e.toString())); |
| 119 } | 118 } |
| 120 | 119 |
| 121 /// Merge `@patch` declarations into `external` declarations. | 120 /// Merge `@patch` declarations into `external` declarations. |
| 122 class PatchApplier extends GeneralizingAstVisitor { | 121 class PatchApplier extends GeneralizingAstVisitor { |
| 123 final StringEditBuffer edits; | 122 final StringEditBuffer edits; |
| 124 final PatchFinder patch; | 123 final PatchFinder patch; |
| 125 | 124 |
| 126 bool _isLibrary = true; // until proven otherwise. | 125 bool _isLibrary = true; // until proven otherwise. |
| 127 | 126 |
| 128 PatchApplier(this.edits, this.patch); | 127 PatchApplier(this.edits, this.patch); |
| 129 | 128 |
| 130 @override visitCompilationUnit(CompilationUnit node) { | 129 @override visitCompilationUnit(CompilationUnit node) { |
| 131 super.visitCompilationUnit(node); | 130 super.visitCompilationUnit(node); |
| 132 if (_isLibrary) _mergeUnpatched(node); | 131 if (_isLibrary) _mergeUnpatched(node); |
| 133 } | 132 } |
| 134 | 133 |
| 135 /// Merges directives and declarations that are not `@patch` into the library. | 134 /// Merges directives and declarations that are not `@patch` into the library. |
| 136 void _mergeUnpatched(CompilationUnit unit) { | 135 void _mergeUnpatched(CompilationUnit unit) { |
| 137 // Merge directives from the patch | 136 // Merge directives from the patch |
| 138 // TODO(jmesserly): remove duplicate imports | 137 // TODO(jmesserly): remove duplicate imports |
| 139 var directivePos = unit.directives.last.end; | 138 var directivePos = unit.directives.last.end; |
| 140 for (var directive in patch.unit.directives) { | 139 for (var directive in patch.unit.directives) { |
| 141 var uri = directive.uri.stringValue; | |
| 142 var code = patch.contents.substring(directive.offset, directive.end); | 140 var code = patch.contents.substring(directive.offset, directive.end); |
| 143 edits.insert(directivePos, '\n' + code); | 141 edits.insert(directivePos, '\n' + code); |
| 144 } | 142 } |
| 145 | 143 |
| 146 // Merge declarations from the patch | 144 // Merge declarations from the patch |
| 147 var declarationPos = edits.original.length; | 145 var declarationPos = edits.original.length; |
| 148 for (var declaration in patch.mergeDeclarations) { | 146 for (var declaration in patch.mergeDeclarations) { |
| 149 var code = patch.contents.substring(declaration.offset, declaration.end); | 147 var code = patch.contents.substring(declaration.offset, declaration.end); |
| 150 edits.insert(declarationPos, '\n' + code); | 148 edits.insert(declarationPos, '\n' + code); |
| 151 } | 149 } |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 int get length => end - begin; | 344 int get length => end - begin; |
| 347 | 345 |
| 348 String toString() => '(Edit @ $begin,$end: "$replace")'; | 346 String toString() => '(Edit @ $begin,$end: "$replace")'; |
| 349 | 347 |
| 350 int compareTo(_StringEdit other) { | 348 int compareTo(_StringEdit other) { |
| 351 int diff = begin - other.begin; | 349 int diff = begin - other.begin; |
| 352 if (diff != 0) return diff; | 350 if (diff != 0) return diff; |
| 353 return end - other.end; | 351 return end - other.end; |
| 354 } | 352 } |
| 355 } | 353 } |
| OLD | NEW |