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 /// Implementations of [Packages] that may be used in either server or browser | |
6 /// based applications. For implementations that can only run in the browser, | |
7 /// see [package_config.packages_io_impl]. | |
8 library package_config.packages_impl; | |
9 | |
10 import "dart:collection" show UnmodifiableMapView; | |
11 import "../packages.dart"; | |
12 import "util.dart" show checkValidPackageUri; | |
13 | |
14 /// A [Packages] null-object. | |
15 class NoPackages implements Packages { | |
16 const NoPackages(); | |
17 | |
18 Uri resolve(Uri packageUri, {Uri notFound(Uri packageUri)}) { | |
19 String packageName = checkValidPackageUri(packageUri); | |
20 if (notFound != null) return notFound(packageUri); | |
21 throw new ArgumentError.value(packageUri, "packageUri", | |
22 'No package named "$packageName"'); | |
23 } | |
24 | |
25 Iterable<String> get packages => new Iterable<String>.generate(0); | |
26 | |
27 Map<String, Uri> asMap() => const<String,Uri>{}; | |
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 = _normalizePath(packageUri); | |
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 // TODO: inline to uri.normalizePath() when we move to 1.11 | |
55 static Uri _normalizePath(Uri uri) => new Uri().resolveUri(uri); | |
56 } | |
57 | |
58 /// A [Packages] implementation based on an existing map. | |
59 class MapPackages extends PackagesBase { | |
60 final Map<String, Uri> _mapping; | |
61 MapPackages(this._mapping); | |
62 | |
63 Uri getBase(String packageName) => _mapping[packageName]; | |
64 | |
65 Iterable<String> get packages => _mapping.keys; | |
66 | |
67 Map<String, Uri> asMap() => new UnmodifiableMapView<String, Uri>(_mapping); | |
68 } | |
69 | |
70 /// A [Packages] implementation based on a remote (e.g., HTTP) directory. | |
71 /// | |
72 /// There is no way to detect which packages exist short of trying to use | |
73 /// them. You can't necessarily check whether a directory exists, | |
74 /// except by checking for a know file in the directory. | |
75 class NonFilePackagesDirectoryPackages extends PackagesBase { | |
76 final Uri _packageBase; | |
77 NonFilePackagesDirectoryPackages(this._packageBase); | |
78 | |
79 Uri getBase(String packageName) => _packageBase.resolve("$packageName/"); | |
80 | |
81 Error _failListingPackages() { | |
82 return new UnsupportedError( | |
83 "Cannot list packages for a ${_packageBase.scheme}: " | |
84 "based package root"); | |
85 } | |
86 | |
87 Iterable<String> get packages { | |
88 throw _failListingPackages(); | |
89 } | |
90 | |
91 Map<String, Uri> asMap() { | |
92 throw _failListingPackages(); | |
93 } | |
94 } | |
OLD | NEW |