OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 library package_config.packages_impl; |
| 6 |
| 7 import "dart:collection" show UnmodifiableMapView; |
| 8 import "dart:io" show Directory; |
| 9 import "package:path/path.dart" as path; |
| 10 import "../packages.dart"; |
| 11 import "util.dart" show checkValidPackageUri; |
| 12 |
| 13 /// A [Packages] null-object. |
| 14 class NoPackages implements Packages { |
| 15 const NoPackages(); |
| 16 |
| 17 Uri resolve(Uri packageUri, {Uri notFound(Uri packageUri)}) { |
| 18 String packageName = checkValidPackageUri(packageUri); |
| 19 if (notFound != null) return notFound(packageUri); |
| 20 throw new ArgumentError.value(packageUri, "packageUri", |
| 21 'No package named "$packageName"'); |
| 22 } |
| 23 |
| 24 Iterable<String> get packages => new Iterable<String>.empty(); |
| 25 |
| 26 Map<String, Uri> asMap() => const<String,Uri>{}; |
| 27 } |
| 28 |
| 29 |
| 30 /// Base class for [Packages] implementations. |
| 31 /// |
| 32 /// This class implements the [resolve] method in terms of a private |
| 33 /// member |
| 34 abstract class _PackagesBase implements Packages { |
| 35 Uri resolve(Uri packageUri, {Uri notFound(Uri packageUri)}) { |
| 36 packageUri = packageUri.normalizePath(); |
| 37 String packageName = checkValidPackageUri(packageUri); |
| 38 Uri packageBase = _getBase(packageName); |
| 39 if (packageBase == null) { |
| 40 if (notFound != null) return notFound(packageUri); |
| 41 throw new ArgumentError.value(packageUri, "packageUri", |
| 42 'No package named "$packageName"'); |
| 43 } |
| 44 String packagePath = packageUri.path.substring(packageName.length + 1); |
| 45 return packageBase.resolve(packagePath); |
| 46 } |
| 47 |
| 48 /// Find a base location for a package name. |
| 49 /// |
| 50 /// Returns `null` if no package exists with that name, and that can be |
| 51 /// determined. |
| 52 Uri _getBase(String packageName); |
| 53 } |
| 54 |
| 55 /// A [Packages] implementation based on an existing map. |
| 56 class MapPackages extends _PackagesBase { |
| 57 final Map<String, Uri> _mapping; |
| 58 MapPackages(this._mapping); |
| 59 |
| 60 Uri _getBase(String packageName) => _mapping[packageName]; |
| 61 |
| 62 Iterable<String> get packages => _mapping.keys; |
| 63 |
| 64 Map<String, Uri> asMap() => new UnmodifiableMapView<String, Uri>(_mapping); |
| 65 } |
| 66 |
| 67 /// A [Packages] implementation based on a local directory. |
| 68 class FilePackagesDirectoryPackages extends _PackagesBase { |
| 69 final Directory _packageDir; |
| 70 FilePackagesDirectoryPackages(this._packageDir); |
| 71 |
| 72 Uri _getBase(String packageName) => |
| 73 new Uri.directory(path.join(packageName,'')); |
| 74 |
| 75 Iterable<String> _listPackageNames() { |
| 76 return _packageDir.listSync() |
| 77 .where((e) => e is Directory) |
| 78 .map((e) => path.basename(e.path)); |
| 79 } |
| 80 |
| 81 Iterable<String> get packages { |
| 82 return _listPackageNames(); |
| 83 } |
| 84 |
| 85 Map<String, Uri> asMap() { |
| 86 var result = <String, Uri>{}; |
| 87 for (var packageName in _listPackageNames()) { |
| 88 result[packageName] = _getBase(packageName); |
| 89 } |
| 90 return new UnmodifiableMapView<String, Uri>(result); |
| 91 } |
| 92 } |
| 93 |
| 94 /// A [Packages] implementation based on a remote (e.g., HTTP) directory. |
| 95 /// |
| 96 /// There is no way to detect which packages exist short of trying to use |
| 97 /// them. You can't necessarily check whether a directory exists, |
| 98 /// except by checking for a know file in the directory. |
| 99 class NonFilePackagesDirectoryPackages extends _PackagesBase { |
| 100 final Uri _packageBase; |
| 101 NonFilePackagesDirectoryPackages(this._packageBase); |
| 102 |
| 103 Uri _getBase(String packageName) => _packageBase.resolve("$packageName/"); |
| 104 |
| 105 Error _failListingPackages() { |
| 106 return new UnsupportedError( |
| 107 "Cannot list packages for a ${_packageBase.scheme}: " |
| 108 "based package root"); |
| 109 } |
| 110 |
| 111 Iterable<String> get packages { |
| 112 throw _failListingPackages(); |
| 113 } |
| 114 |
| 115 Map<String, Uri> asMap() { |
| 116 throw _failListingPackages(); |
| 117 } |
| 118 } |
OLD | NEW |