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