| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library source.package_map_resolver; | |
| 6 | |
| 7 import 'dart:core' hide Resource; | |
| 8 | |
| 9 import 'package:analyzer/file_system/file_system.dart'; | |
| 10 import 'package:analyzer/src/generated/source.dart'; | |
| 11 import 'package:analyzer/src/util/asserts.dart' as asserts; | |
| 12 import 'package:path/path.dart' as pathos; | |
| 13 | |
| 14 /** | |
| 15 * A [UriResolver] implementation for the `package:` scheme that uses a map of | |
| 16 * package names to their directories. | |
| 17 */ | |
| 18 class PackageMapUriResolver extends UriResolver { | |
| 19 /** | |
| 20 * The name of the `package` scheme. | |
| 21 */ | |
| 22 static const String PACKAGE_SCHEME = "package"; | |
| 23 | |
| 24 /** | |
| 25 * A table mapping package names to the path of the directories containing | |
| 26 * the package. | |
| 27 */ | |
| 28 final Map<String, List<Folder>> packageMap; | |
| 29 | |
| 30 /** | |
| 31 * The [ResourceProvider] for this resolver. | |
| 32 */ | |
| 33 final ResourceProvider resourceProvider; | |
| 34 | |
| 35 /** | |
| 36 * Create a new [PackageMapUriResolver]. | |
| 37 * | |
| 38 * [packageMap] is a table mapping package names to the paths of the | |
| 39 * directories containing the package | |
| 40 */ | |
| 41 PackageMapUriResolver(this.resourceProvider, this.packageMap) { | |
| 42 asserts.notNull(resourceProvider); | |
| 43 asserts.notNull(packageMap); | |
| 44 } | |
| 45 | |
| 46 @override | |
| 47 Source resolveAbsolute(Uri uri) { | |
| 48 if (!isPackageUri(uri)) { | |
| 49 return null; | |
| 50 } | |
| 51 // Prepare path. | |
| 52 String path = uri.path; | |
| 53 // Prepare path components. | |
| 54 int index = path.indexOf('/'); | |
| 55 if (index == -1 || index == 0) { | |
| 56 return null; | |
| 57 } | |
| 58 // <pkgName>/<relPath> | |
| 59 String pkgName = path.substring(0, index); | |
| 60 String relPath = path.substring(index + 1); | |
| 61 // Try to find an existing file. | |
| 62 List<Folder> packageDirs = packageMap[pkgName]; | |
| 63 if (packageDirs != null) { | |
| 64 for (Folder packageDir in packageDirs) { | |
| 65 if (packageDir.exists) { | |
| 66 Resource result = packageDir.getChild(relPath); | |
| 67 if (result is File && result.exists) { | |
| 68 return result.createSource(uri); | |
| 69 } | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 // Return a NonExistingSource instance. | |
| 74 // This helps provide more meaningful error messages to users | |
| 75 // (a missing file error, as opposed to an invalid URI error). | |
| 76 String fullPath = packageDirs != null && packageDirs.isNotEmpty | |
| 77 ? packageDirs.first.canonicalizePath(relPath) | |
| 78 : relPath; | |
| 79 return new NonExistingSource(fullPath, uri, UriKind.PACKAGE_URI); | |
| 80 } | |
| 81 | |
| 82 @override | |
| 83 Uri restoreAbsolute(Source source) { | |
| 84 String sourcePath = source.fullName; | |
| 85 Uri bestMatch; | |
| 86 int bestMatchLength = -1; | |
| 87 pathos.Context pathContext = resourceProvider.pathContext; | |
| 88 for (String pkgName in packageMap.keys) { | |
| 89 List<Folder> pkgFolders = packageMap[pkgName]; | |
| 90 for (int i = 0; i < pkgFolders.length; i++) { | |
| 91 Folder pkgFolder = pkgFolders[i]; | |
| 92 String pkgFolderPath = pkgFolder.path; | |
| 93 if (pkgFolderPath.length > bestMatchLength && | |
| 94 sourcePath.startsWith(pkgFolderPath + pathContext.separator)) { | |
| 95 String relPath = sourcePath.substring(pkgFolderPath.length + 1); | |
| 96 if (_isReversibleTranslation(pkgFolders, i, relPath)) { | |
| 97 List<String> relPathComponents = pathContext.split(relPath); | |
| 98 String relUriPath = pathos.posix.joinAll(relPathComponents); | |
| 99 bestMatch = Uri.parse('$PACKAGE_SCHEME:$pkgName/$relUriPath'); | |
| 100 bestMatchLength = pkgFolderPath.length; | |
| 101 } | |
| 102 } | |
| 103 } | |
| 104 } | |
| 105 return bestMatch; | |
| 106 } | |
| 107 | |
| 108 /** | |
| 109 * A translation from file path to package URI has just been found for | |
| 110 * using the [packageDirIndex]th element of [packageDirs], and appending the | |
| 111 * relative path [relPath]. Determine whether the translation is reversible; | |
| 112 * that is, whether translating the package URI pack to a file path will | |
| 113 * produce the file path we started with. | |
| 114 */ | |
| 115 bool _isReversibleTranslation( | |
| 116 List<Folder> packageDirs, int packageDirIndex, String relPath) { | |
| 117 // The translation is reversible provided there is no prior element of | |
| 118 // [packageDirs] containing a file matching [relPath]. | |
| 119 for (int i = 0; i < packageDirIndex; i++) { | |
| 120 Folder packageDir = packageDirs[i]; | |
| 121 if (packageDir.exists) { | |
| 122 Resource result = packageDir.getChild(relPath); | |
| 123 if (result is File && result.exists) { | |
| 124 return false; | |
| 125 } | |
| 126 } | |
| 127 } | |
| 128 return true; | |
| 129 } | |
| 130 | |
| 131 /** | |
| 132 * Returns `true` if [uri] is a `package` URI. | |
| 133 */ | |
| 134 static bool isPackageUri(Uri uri) { | |
| 135 return uri.scheme == PACKAGE_SCHEME; | |
| 136 } | |
| 137 } | |
| OLD | NEW |