| 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 | 9 |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * Create a new [PackageMapUriResolver]. | 33 * Create a new [PackageMapUriResolver]. |
| 34 * | 34 * |
| 35 * [packageMap] is a table mapping package names to the paths of the | 35 * [packageMap] is a table mapping package names to the paths of the |
| 36 * directories containing the package | 36 * directories containing the package |
| 37 */ | 37 */ |
| 38 PackageMapUriResolver(this.resourceProvider, this.packageMap); | 38 PackageMapUriResolver(this.resourceProvider, this.packageMap); |
| 39 | 39 |
| 40 @override | 40 @override |
| 41 Source fromEncoding(UriKind kind, Uri uri) { | |
| 42 if (kind == UriKind.PACKAGE_URI) { | |
| 43 Resource resource = resourceProvider.getResource(uri.path); | |
| 44 if (resource is File) { | |
| 45 return resource.createSource(kind); | |
| 46 } | |
| 47 } | |
| 48 return null; | |
| 49 } | |
| 50 | |
| 51 @override | |
| 52 Source resolveAbsolute(Uri uri) { | 41 Source resolveAbsolute(Uri uri) { |
| 53 if (!isPackageUri(uri)) { | 42 if (!isPackageUri(uri)) { |
| 54 return null; | 43 return null; |
| 55 } | 44 } |
| 56 // Prepare path. | 45 // Prepare path. |
| 57 String path = uri.path; | 46 String path = uri.path; |
| 58 // Prepare path components. | 47 // Prepare path components. |
| 59 String pkgName; | 48 String pkgName; |
| 60 String relPath; | 49 String relPath; |
| 61 int index = path.indexOf('/'); | 50 int index = path.indexOf('/'); |
| 62 if (index == -1 || index == 0) { | 51 if (index == -1 || index == 0) { |
| 63 return null; | 52 return null; |
| 64 } else { | 53 } else { |
| 65 // <pkgName>/<relPath> | 54 // <pkgName>/<relPath> |
| 66 pkgName = path.substring(0, index); | 55 pkgName = path.substring(0, index); |
| 67 relPath = path.substring(index + 1); | 56 relPath = path.substring(index + 1); |
| 68 } | 57 } |
| 69 // Try to find an existing file. | 58 // Try to find an existing file. |
| 70 List<Folder> packageDirs = packageMap[pkgName]; | 59 List<Folder> packageDirs = packageMap[pkgName]; |
| 71 if (packageDirs != null) { | 60 if (packageDirs != null) { |
| 72 for (Folder packageDir in packageDirs) { | 61 for (Folder packageDir in packageDirs) { |
| 73 if (packageDir.exists) { | 62 if (packageDir.exists) { |
| 74 Resource result = packageDir.getChild(relPath); | 63 Resource result = packageDir.getChild(relPath); |
| 75 if (result is File && result.exists) { | 64 if (result is File && result.exists) { |
| 76 return result.createSource(UriKind.PACKAGE_URI); | 65 return result.createSource(uri); |
| 77 } | 66 } |
| 78 } | 67 } |
| 79 } | 68 } |
| 80 } | 69 } |
| 81 // Return a NonExistingSource instance. | 70 // Return a NonExistingSource instance. |
| 82 // This helps provide more meaningful error messages to users | 71 // This helps provide more meaningful error messages to users |
| 83 // (a missing file error, as opposed to an invalid URI error). | 72 // (a missing file error, as opposed to an invalid URI error). |
| 84 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); | 73 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); |
| 85 } | 74 } |
| 86 | 75 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 100 return null; | 89 return null; |
| 101 } | 90 } |
| 102 | 91 |
| 103 /** | 92 /** |
| 104 * Returns `true` if [uri] is a `package` URI. | 93 * Returns `true` if [uri] is a `package` URI. |
| 105 */ | 94 */ |
| 106 static bool isPackageUri(Uri uri) { | 95 static bool isPackageUri(Uri uri) { |
| 107 return uri.scheme == PACKAGE_SCHEME; | 96 return uri.scheme == PACKAGE_SCHEME; |
| 108 } | 97 } |
| 109 } | 98 } |
| OLD | NEW |