| 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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 152 |
| 153 bool _isLibrary = true; // until proven otherwise. | 153 bool _isLibrary = true; // until proven otherwise. |
| 154 | 154 |
| 155 PatchApplier(this.edits, this.patch); | 155 PatchApplier(this.edits, this.patch); |
| 156 | 156 |
| 157 @override visitCompilationUnit(CompilationUnit node) { | 157 @override visitCompilationUnit(CompilationUnit node) { |
| 158 super.visitCompilationUnit(node); | 158 super.visitCompilationUnit(node); |
| 159 if (_isLibrary) _mergeUnpatched(node); | 159 if (_isLibrary) _mergeUnpatched(node); |
| 160 } | 160 } |
| 161 | 161 |
| 162 void _merge(AstNode node, int pos) { |
| 163 var code = patch.contents.substring(node.offset, node.end); |
| 164 edits.insert(pos, '\n' + code); |
| 165 } |
| 166 |
| 162 /// Merges directives and declarations that are not `@patch` into the library. | 167 /// Merges directives and declarations that are not `@patch` into the library. |
| 163 void _mergeUnpatched(CompilationUnit unit) { | 168 void _mergeUnpatched(CompilationUnit unit) { |
| 164 // Merge directives from the patch | 169 |
| 170 // Merge imports from the patch |
| 165 // TODO(jmesserly): remove duplicate imports | 171 // TODO(jmesserly): remove duplicate imports |
| 166 var directivePos = unit.directives.last.end; | 172 |
| 167 for (var directive in patch.unit.directives) { | 173 // To patch a library, we must have a library directive |
| 168 var code = patch.contents.substring(directive.offset, directive.end); | 174 var libDir = unit.directives.first as LibraryDirective; |
| 169 edits.insert(directivePos, '\n' + code); | 175 int importPos = unit.directives.lastWhere((d) => d is ImportDirective, |
| 176 orElse: () => libDir).end; |
| 177 for (var d in patch.unit.directives.where((d) => d is ImportDirective)) { |
| 178 _merge(d, importPos); |
| 179 } |
| 180 |
| 181 int partPos = unit.directives.last.end; |
| 182 for (var d in patch.unit.directives.where((d) => d is PartDirective)) { |
| 183 _merge(d, partPos); |
| 170 } | 184 } |
| 171 | 185 |
| 172 // Merge declarations from the patch | 186 // Merge declarations from the patch |
| 173 var declarationPos = edits.original.length; | 187 int declPos = edits.original.length; |
| 174 for (var declaration in patch.mergeDeclarations) { | 188 for (var d in patch.mergeDeclarations) { |
| 175 var code = patch.contents.substring(declaration.offset, declaration.end); | 189 _merge(d, declPos); |
| 176 edits.insert(declarationPos, '\n' + code); | |
| 177 } | 190 } |
| 178 } | 191 } |
| 179 | 192 |
| 180 @override visitPartOfDirective(PartOfDirective node) { | 193 @override visitPartOfDirective(PartOfDirective node) { |
| 181 _isLibrary = false; | 194 _isLibrary = false; |
| 182 } | 195 } |
| 183 | 196 |
| 184 @override visitFunctionDeclaration(FunctionDeclaration node) { | 197 @override visitFunctionDeclaration(FunctionDeclaration node) { |
| 185 _maybePatch(node); | 198 _maybePatch(node); |
| 186 } | 199 } |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 if (diff != 0) return diff; | 390 if (diff != 0) return diff; |
| 378 return end - other.end; | 391 return end - other.end; |
| 379 } | 392 } |
| 380 } | 393 } |
| 381 | 394 |
| 382 List<SdkLibrary> _getSdkLibraries(String contents) { | 395 List<SdkLibrary> _getSdkLibraries(String contents) { |
| 383 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); | 396 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); |
| 384 parseCompilationUnit(contents).accept(libraryBuilder); | 397 parseCompilationUnit(contents).accept(libraryBuilder); |
| 385 return libraryBuilder.librariesMap.sdkLibraries; | 398 return libraryBuilder.librariesMap.sdkLibraries; |
| 386 } | 399 } |
| OLD | NEW |