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 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 // This helps provide more meaningful error messages to users | 72 // This helps provide more meaningful error messages to users |
73 // (a missing file error, as opposed to an invalid URI error). | 73 // (a missing file error, as opposed to an invalid URI error). |
74 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); | 74 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); |
75 } | 75 } |
76 | 76 |
77 @override | 77 @override |
78 Uri restoreAbsolute(Source source) { | 78 Uri restoreAbsolute(Source source) { |
79 String sourcePath = source.fullName; | 79 String sourcePath = source.fullName; |
80 for (String pkgName in packageMap.keys) { | 80 for (String pkgName in packageMap.keys) { |
81 List<Folder> pkgFolders = packageMap[pkgName]; | 81 List<Folder> pkgFolders = packageMap[pkgName]; |
82 for (Folder pkgFolder in pkgFolders) { | 82 for (int i = 0; i < pkgFolders.length; i++) { |
83 Folder pkgFolder = pkgFolders[i]; | |
83 String pkgFolderPath = pkgFolder.path; | 84 String pkgFolderPath = pkgFolder.path; |
84 if (sourcePath.startsWith(pkgFolderPath)) { | 85 // TODO(paulberry): figure out the right thing to do for Windows. |
Paul Berry
2015/02/24 21:58:16
Note: AFAICT this function is broken for Windows b
| |
85 String relPath = sourcePath.substring(pkgFolderPath.length); | 86 if (sourcePath.startsWith(pkgFolderPath + '/')) { |
86 return Uri.parse('$PACKAGE_SCHEME:$pkgName$relPath'); | 87 String relPath = sourcePath.substring(pkgFolderPath.length + 1); |
88 if (_isReversibleTranslation(pkgFolders, i, relPath)) { | |
89 return Uri.parse('$PACKAGE_SCHEME:$pkgName/$relPath'); | |
90 } | |
87 } | 91 } |
88 } | 92 } |
89 } | 93 } |
90 return null; | 94 return null; |
91 } | 95 } |
92 | 96 |
93 /** | 97 /** |
98 * A translation from file path to package URI has just been found for | |
99 * using the [packageDirIndex]th element of [packageDirs], and appending the | |
100 * relative path [relPath]. Determine whether the translation is reversible; | |
101 * that is, whether translating the package URI pack to a file path will | |
102 * produce the file path we started with. | |
103 */ | |
104 bool _isReversibleTranslation(List<Folder> packageDirs, int packageDirIndex, | |
105 String relPath) { | |
106 // The translation is reversible provided there is no prior element of | |
107 // [packageDirs] containing a file matching [relPath]. | |
108 for (int i = 0; i < packageDirIndex; i++) { | |
109 Folder packageDir = packageDirs[i]; | |
110 if (packageDir.exists) { | |
111 Resource result = packageDir.getChild(relPath); | |
112 if (result is File && result.exists) { | |
113 return false; | |
114 } | |
115 } | |
116 } | |
117 return true; | |
118 } | |
119 | |
120 /** | |
94 * Returns `true` if [uri] is a `package` URI. | 121 * Returns `true` if [uri] is a `package` URI. |
95 */ | 122 */ |
96 static bool isPackageUri(Uri uri) { | 123 static bool isPackageUri(Uri uri) { |
97 return uri.scheme == PACKAGE_SCHEME; | 124 return uri.scheme == PACKAGE_SCHEME; |
98 } | 125 } |
99 } | 126 } |
OLD | NEW |