| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library analyzer.src.dart.sdk.patch; | 5 library analyzer.src.dart.sdk.patch; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/token.dart'; | 8 import 'package:analyzer/dart/ast/token.dart'; |
| 9 import 'package:analyzer/error/listener.dart'; | 9 import 'package:analyzer/error/listener.dart'; |
| 10 import 'package:analyzer/file_system/file_system.dart'; | 10 import 'package:analyzer/file_system/file_system.dart'; |
| 11 import 'package:analyzer/src/dart/scanner/reader.dart'; | 11 import 'package:analyzer/src/dart/scanner/reader.dart'; |
| 12 import 'package:analyzer/src/dart/scanner/scanner.dart'; | 12 import 'package:analyzer/src/dart/scanner/scanner.dart'; |
| 13 import 'package:analyzer/src/dart/sdk/sdk.dart'; | |
| 14 import 'package:analyzer/src/generated/parser.dart'; | 13 import 'package:analyzer/src/generated/parser.dart'; |
| 15 import 'package:analyzer/src/generated/sdk.dart'; | |
| 16 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:meta/meta.dart'; | 15 import 'package:meta/meta.dart'; |
| 18 import 'package:path/src/context.dart'; | |
| 19 | 16 |
| 20 /** | 17 /** |
| 21 * [SdkPatcher] applies patches to SDK [CompilationUnit]. | 18 * [SdkPatcher] applies patches to SDK [CompilationUnit]. |
| 22 */ | 19 */ |
| 23 class SdkPatcher { | 20 class SdkPatcher { |
| 24 bool _allowNewPublicNames; | 21 bool _allowNewPublicNames; |
| 25 String _baseDesc; | 22 String _baseDesc; |
| 26 String _patchDesc; | 23 String _patchDesc; |
| 27 CompilationUnit _patchUnit; | 24 CompilationUnit _patchUnit; |
| 28 | 25 |
| 29 /** | 26 /** |
| 30 * Patch the given [unit] of a SDK [source] with the patches defined in | 27 * Patch the given [unit] of a SDK [source] with the patches defined in |
| 31 * the [sdk] for the given [platform]. Throw [ArgumentError] if a patch | 28 * [allPatchPaths]. Throw [ArgumentError] if a patch |
| 32 * file cannot be read, or the contents violates rules for patch files. | 29 * file cannot be read, or the contents violates rules for patch files. |
| 33 */ | 30 */ |
| 34 void patch( | 31 void patch( |
| 35 FolderBasedDartSdk sdk, | 32 ResourceProvider resourceProvider, |
| 36 int platform, | 33 bool strongMode, |
| 34 Map<String, List<String>> allPatchPaths, |
| 37 AnalysisErrorListener errorListener, | 35 AnalysisErrorListener errorListener, |
| 38 Source source, | 36 Source source, |
| 39 CompilationUnit unit) { | 37 CompilationUnit unit) { |
| 40 // Process URI. | 38 // Process URI. |
| 41 String libraryUriStr; | 39 String libraryUriStr; |
| 42 bool isLibraryDefiningUnit; | 40 bool isLibraryDefiningUnit; |
| 43 { | 41 { |
| 44 Uri uri = source.uri; | 42 Uri uri = source.uri; |
| 45 if (uri.scheme != 'dart') { | 43 if (uri.scheme != 'dart') { |
| 46 throw new ArgumentError( | 44 throw new ArgumentError( |
| 47 'The URI of the unit to patch must have the "dart" scheme: $uri'); | 45 'The URI of the unit to patch must have the "dart" scheme: $uri'); |
| 48 } | 46 } |
| 49 List<String> uriSegments = uri.pathSegments; | 47 List<String> uriSegments = uri.pathSegments; |
| 50 String libraryName = uriSegments.first; | 48 String libraryName = uriSegments.first; |
| 51 libraryUriStr = 'dart:$libraryName'; | 49 libraryUriStr = 'dart:$libraryName'; |
| 52 isLibraryDefiningUnit = uriSegments.length == 1; | 50 isLibraryDefiningUnit = uriSegments.length == 1; |
| 53 _allowNewPublicNames = libraryName == '_internal'; | 51 _allowNewPublicNames = libraryName == '_internal'; |
| 54 } | 52 } |
| 55 // Prepare the patch files to apply. | 53 // Prepare the patch files to apply. |
| 56 List<String> patchPaths; | 54 List<String> patchPaths = allPatchPaths[libraryUriStr]; |
| 57 { | |
| 58 SdkLibrary sdkLibrary = sdk.getSdkLibrary(libraryUriStr); | |
| 59 if (sdkLibrary == null) { | |
| 60 throw new ArgumentError( | |
| 61 'The library $libraryUriStr is not defined in the SDK.'); | |
| 62 } | |
| 63 patchPaths = sdkLibrary.getPatches(platform); | |
| 64 } | |
| 65 | 55 |
| 66 bool strongMode = sdk.context.analysisOptions.strongMode; | |
| 67 Context pathContext = sdk.resourceProvider.pathContext; | |
| 68 for (String path in patchPaths) { | 56 for (String path in patchPaths) { |
| 69 String pathInLib = pathContext.joinAll(path.split('/')); | 57 File patchFile = resourceProvider.getFile(path); |
| 70 File patchFile = sdk.libraryDirectory.getChildAssumingFile(pathInLib); | |
| 71 if (!patchFile.exists) { | 58 if (!patchFile.exists) { |
| 72 throw new ArgumentError( | 59 throw new ArgumentError( |
| 73 'The patch file ${patchFile.path} for $source does not exist.'); | 60 'The patch file ${patchFile.path} for $source does not exist.'); |
| 74 } | 61 } |
| 75 Source patchSource = patchFile.createSource(); | 62 Source patchSource = patchFile.createSource(); |
| 76 CompilationUnit patchUnit = parse(patchSource, strongMode, errorListener); | 63 CompilationUnit patchUnit = parse(patchSource, strongMode, errorListener); |
| 77 | 64 |
| 78 // Prepare for reporting errors. | 65 // Prepare for reporting errors. |
| 79 _baseDesc = source.toString(); | 66 _baseDesc = source.toString(); |
| 80 _patchDesc = patchFile.path; | 67 _patchDesc = patchFile.path; |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 } | 356 } |
| 370 | 357 |
| 371 /** | 358 /** |
| 372 * Replace tokens of the [oldNode] with tokens of the [newNode]. | 359 * Replace tokens of the [oldNode] with tokens of the [newNode]. |
| 373 */ | 360 */ |
| 374 static void _replaceNodeTokens(AstNode oldNode, AstNode newNode) { | 361 static void _replaceNodeTokens(AstNode oldNode, AstNode newNode) { |
| 375 oldNode.beginToken.previous.setNext(newNode.beginToken); | 362 oldNode.beginToken.previous.setNext(newNode.beginToken); |
| 376 newNode.endToken.setNext(oldNode.endToken.next); | 363 newNode.endToken.setNext(oldNode.endToken.next); |
| 377 } | 364 } |
| 378 } | 365 } |
| OLD | NEW |