| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 source.package_map_resolver; | 5 library source.package_map_resolver; |
| 6 | 6 |
| 7 import 'package:analyzer/file_system/file_system.dart'; | 7 import 'package:analyzer/file_system/file_system.dart'; |
| 8 import 'package:analyzer/src/generated/source.dart'; | 8 import 'package:analyzer/src/generated/source.dart'; |
| 9 import 'package:analyzer/src/util/asserts.dart' as asserts; | 9 import 'package:analyzer/src/util/asserts.dart' as asserts; |
| 10 | 10 |
| 11 | |
| 12 /** | 11 /** |
| 13 * A [UriResolver] implementation for the `package:` scheme that uses a map of | 12 * A [UriResolver] implementation for the `package:` scheme that uses a map of |
| 14 * package names to their directories. | 13 * package names to their directories. |
| 15 */ | 14 */ |
| 16 class PackageMapUriResolver extends UriResolver { | 15 class PackageMapUriResolver extends UriResolver { |
| 17 /** | 16 /** |
| 18 * The name of the `package` scheme. | 17 * The name of the `package` scheme. |
| 19 */ | 18 */ |
| 20 static const String PACKAGE_SCHEME = "package"; | 19 static const String PACKAGE_SCHEME = "package"; |
| 21 | 20 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 Uri restoreAbsolute(Source source) { | 77 Uri restoreAbsolute(Source source) { |
| 79 String sourcePath = source.fullName; | 78 String sourcePath = source.fullName; |
| 80 Uri bestMatch; | 79 Uri bestMatch; |
| 81 int bestMatchLength = -1; | 80 int bestMatchLength = -1; |
| 82 for (String pkgName in packageMap.keys) { | 81 for (String pkgName in packageMap.keys) { |
| 83 List<Folder> pkgFolders = packageMap[pkgName]; | 82 List<Folder> pkgFolders = packageMap[pkgName]; |
| 84 for (int i = 0; i < pkgFolders.length; i++) { | 83 for (int i = 0; i < pkgFolders.length; i++) { |
| 85 Folder pkgFolder = pkgFolders[i]; | 84 Folder pkgFolder = pkgFolders[i]; |
| 86 String pkgFolderPath = pkgFolder.path; | 85 String pkgFolderPath = pkgFolder.path; |
| 87 // TODO(paulberry): figure out the right thing to do for Windows. | 86 // TODO(paulberry): figure out the right thing to do for Windows. |
| 88 if (pkgFolderPath.length > bestMatchLength && sourcePath.startsWith(pkgF
olderPath + '/')) { | 87 if (pkgFolderPath.length > bestMatchLength && |
| 88 sourcePath.startsWith(pkgFolderPath + '/')) { |
| 89 String relPath = sourcePath.substring(pkgFolderPath.length + 1); | 89 String relPath = sourcePath.substring(pkgFolderPath.length + 1); |
| 90 if (_isReversibleTranslation(pkgFolders, i, relPath)) { | 90 if (_isReversibleTranslation(pkgFolders, i, relPath)) { |
| 91 bestMatch = Uri.parse('$PACKAGE_SCHEME:$pkgName/$relPath'); | 91 bestMatch = Uri.parse('$PACKAGE_SCHEME:$pkgName/$relPath'); |
| 92 bestMatchLength = pkgFolderPath.length; | 92 bestMatchLength = pkgFolderPath.length; |
| 93 } | 93 } |
| 94 } | 94 } |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 return bestMatch; | 97 return bestMatch; |
| 98 } | 98 } |
| 99 | 99 |
| 100 /** | 100 /** |
| 101 * A translation from file path to package URI has just been found for | 101 * A translation from file path to package URI has just been found for |
| 102 * using the [packageDirIndex]th element of [packageDirs], and appending the | 102 * using the [packageDirIndex]th element of [packageDirs], and appending the |
| 103 * relative path [relPath]. Determine whether the translation is reversible; | 103 * relative path [relPath]. Determine whether the translation is reversible; |
| 104 * that is, whether translating the package URI pack to a file path will | 104 * that is, whether translating the package URI pack to a file path will |
| 105 * produce the file path we started with. | 105 * produce the file path we started with. |
| 106 */ | 106 */ |
| 107 bool _isReversibleTranslation(List<Folder> packageDirs, int packageDirIndex, | 107 bool _isReversibleTranslation( |
| 108 String relPath) { | 108 List<Folder> packageDirs, int packageDirIndex, String relPath) { |
| 109 // The translation is reversible provided there is no prior element of | 109 // The translation is reversible provided there is no prior element of |
| 110 // [packageDirs] containing a file matching [relPath]. | 110 // [packageDirs] containing a file matching [relPath]. |
| 111 for (int i = 0; i < packageDirIndex; i++) { | 111 for (int i = 0; i < packageDirIndex; i++) { |
| 112 Folder packageDir = packageDirs[i]; | 112 Folder packageDir = packageDirs[i]; |
| 113 if (packageDir.exists) { | 113 if (packageDir.exists) { |
| 114 Resource result = packageDir.getChild(relPath); | 114 Resource result = packageDir.getChild(relPath); |
| 115 if (result is File && result.exists) { | 115 if (result is File && result.exists) { |
| 116 return false; | 116 return false; |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 return true; | 120 return true; |
| 121 } | 121 } |
| 122 | 122 |
| 123 /** | 123 /** |
| 124 * Returns `true` if [uri] is a `package` URI. | 124 * Returns `true` if [uri] is a `package` URI. |
| 125 */ | 125 */ |
| 126 static bool isPackageUri(Uri uri) { | 126 static bool isPackageUri(Uri uri) { |
| 127 return uri.scheme == PACKAGE_SCHEME; | 127 return uri.scheme == PACKAGE_SCHEME; |
| 128 } | 128 } |
| 129 } | 129 } |
| OLD | NEW |