Chromium Code Reviews| 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 import 'dart:io'; | 5 import 'dart:io'; |
| 6 | 6 |
| 7 import 'package:analyzer/src/generated/java_io.dart'; | 7 import 'package:analyzer/src/generated/java_io.dart'; |
| 8 import 'package:analyzer/src/generated/source.dart'; | 8 import 'package:analyzer/src/generated/source.dart'; |
| 9 import 'package:analyzer/src/generated/source_io.dart'; | 9 import 'package:analyzer/src/generated/source_io.dart'; |
| 10 import 'package:path/path.dart' show join; | 10 import 'package:path/path.dart' as path; |
| 11 | 11 |
| 12 /// A package resolver that supports a non-standard package layout, where | 12 /// A package resolver that supports a non-standard package layout, where |
| 13 /// packages with dotted names are expanded to a hierarchy of directories, and | 13 /// packages with dotted names are expanded to a hierarchy of directories, and |
| 14 /// packages can be found on one or more locations. | 14 /// packages can be found on one or more locations. |
| 15 class MultiPackageResolver extends UriResolver { | 15 class MultiPackageResolver extends UriResolver { |
| 16 final List<String> searchPaths; | 16 final List<String> searchPaths; |
| 17 MultiPackageResolver(this.searchPaths); | 17 MultiPackageResolver(this.searchPaths); |
| 18 | 18 |
| 19 @override | 19 @override |
| 20 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | 20 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 21 var candidates = _expandPath(uri); | 21 var candidates = _expandPath(uri); |
| 22 if (candidates == null) return null; | 22 if (candidates == null) return null; |
| 23 | 23 |
| 24 for (var path in candidates) { | 24 for (var path in candidates) { |
| 25 var resolvedPath = _resolve(path); | 25 var resolvedPath = _resolve(path); |
| 26 if (resolvedPath != null) { | 26 if (resolvedPath != null) { |
| 27 return new FileBasedSource( | 27 return new FileBasedSource( |
| 28 new JavaFile(resolvedPath), actualUri != null ? actualUri : uri); | 28 new JavaFile(resolvedPath), actualUri != null ? actualUri : uri); |
| 29 } | 29 } |
| 30 } | 30 } |
| 31 return null; | 31 return null; |
| 32 } | 32 } |
| 33 | 33 |
| 34 /// Resolve [path] by looking at each prefix in [searchPaths] and returning | 34 @override |
| 35 /// the first location where `prefix + path` exists. | 35 Uri restoreAbsolute(Source source) { |
|
Paul Berry
2016/04/27 19:19:57
FYI, technically this breaks the interface contrac
| |
| 36 String _resolve(String path) { | 36 var uri = source.uri; |
| 37 if (uri.scheme == 'package' || uri.scheme == 'dart') { | |
| 38 return uri; | |
| 39 } | |
| 40 if (uri.scheme == '' || uri.scheme == 'file') { | |
| 41 var filePath = path.absolute(uri.path); | |
|
Paul Berry
2016/04/27 19:19:57
This won't work on Windows, since uri.path uses '/
| |
| 42 for (var searchPath in searchPaths) { | |
| 43 if (path.isWithin(searchPath, filePath)) { | |
| 44 var relativePath = path.relative(filePath, from: searchPath); | |
| 45 var segments = path.split(relativePath); | |
| 46 var libIndex = segments.lastIndexOf('lib'); | |
|
vsm
2016/04/27 18:24:35
This is not yet tested and seems brittle...
| |
| 47 var packageName = segments.sublist(0, libIndex).join('.'); | |
| 48 var packageRelativePath = segments.sublist(libIndex + 1).join('/'); | |
| 49 return Uri.parse('package:$packageName/$packageRelativePath'); | |
| 50 } | |
| 51 } | |
| 52 } | |
| 53 return null; | |
| 54 } | |
| 55 | |
| 56 /// Resolve [packagePath] by looking at each prefix in [searchPaths] and retur ning | |
|
Jennifer Messerly
2016/04/27 18:30:05
long line
| |
| 57 /// the first location where `prefix + packagePath` exists. | |
| 58 String _resolve(String packagePath) { | |
| 37 for (var prefix in searchPaths) { | 59 for (var prefix in searchPaths) { |
| 38 var resolvedPath = join(prefix, path); | 60 var resolvedPath = path.join(prefix, packagePath); |
| 39 if (new File(resolvedPath).existsSync()) return resolvedPath; | 61 if (new File(resolvedPath).existsSync()) return resolvedPath; |
| 40 } | 62 } |
| 41 return null; | 63 return null; |
| 42 } | 64 } |
| 43 | 65 |
| 44 /// Expand `uri.path`, replacing dots in the package name with slashes. | 66 /// Expand `uri.path`, replacing dots in the package name with slashes. |
| 45 List<String> _expandPath(Uri uri) { | 67 List<String> _expandPath(Uri uri) { |
| 46 if (uri.scheme != 'package') return null; | 68 if (uri.scheme != 'package') return null; |
| 47 var path = uri.path; | 69 var path = uri.path; |
| 48 var slashPos = path.indexOf('/'); | 70 var slashPos = path.indexOf('/'); |
| 49 var packagePath = path.substring(0, slashPos).replaceAll(".", "/"); | 71 var packagePath = path.substring(0, slashPos).replaceAll(".", "/"); |
| 50 var filePath = path.substring(slashPos + 1); | 72 var filePath = path.substring(slashPos + 1); |
| 51 return ['$packagePath/lib/$filePath', '$packagePath/$filePath']; | 73 return ['$packagePath/lib/$filePath', '$packagePath/$filePath']; |
| 52 } | 74 } |
| 53 } | 75 } |
| OLD | NEW |