| 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 import 'dart:io'; | |
| 6 | |
| 7 import 'package:analyzer/src/generated/java_io.dart'; | |
| 8 import 'package:analyzer/src/generated/source.dart'; | |
| 9 import 'package:analyzer/src/generated/source_io.dart'; | |
| 10 import 'package:path/path.dart' show join; | |
| 11 | |
| 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 | |
| 14 /// packages can be found on one or more locations. | |
| 15 class MultiPackageResolver extends UriResolver { | |
| 16 final List<String> searchPaths; | |
| 17 MultiPackageResolver(this.searchPaths); | |
| 18 | |
| 19 @override | |
| 20 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | |
| 21 var candidates = _expandPath(uri); | |
| 22 if (candidates == null) return null; | |
| 23 | |
| 24 for (var path in candidates) { | |
| 25 var resolvedPath = _resolve(path); | |
| 26 if (resolvedPath != null) { | |
| 27 return new FileBasedSource( | |
| 28 new JavaFile(resolvedPath), actualUri != null ? actualUri : uri); | |
| 29 } | |
| 30 } | |
| 31 return null; | |
| 32 } | |
| 33 | |
| 34 /// Resolve [path] by looking at each prefix in [searchPaths] and returning | |
| 35 /// the first location where `prefix + path` exists. | |
| 36 String _resolve(String path) { | |
| 37 for (var prefix in searchPaths) { | |
| 38 var resolvedPath = join(prefix, path); | |
| 39 if (new File(resolvedPath).existsSync()) return resolvedPath; | |
| 40 } | |
| 41 return null; | |
| 42 } | |
| 43 | |
| 44 /// Expand `uri.path`, replacing dots in the package name with slashes. | |
| 45 List<String> _expandPath(Uri uri) { | |
| 46 if (uri.scheme != 'package') return null; | |
| 47 var path = uri.path; | |
| 48 var slashPos = path.indexOf('/'); | |
| 49 var packagePath = path.substring(0, slashPos).replaceAll(".", "/"); | |
| 50 var filePath = path.substring(slashPos + 1); | |
| 51 return ['$packagePath/lib/$filePath', '$packagePath/$filePath']; | |
| 52 } | |
| 53 } | |
| OLD | NEW |