OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 pub.source_registry; | 5 library pub.source_registry; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package.dart'; | 9 import 'package.dart'; |
10 import 'source.dart'; | 10 import 'source.dart'; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 /// registered. | 58 /// registered. |
59 void register(Source source) { | 59 void register(Source source) { |
60 if (_sources.containsKey(source.name)) { | 60 if (_sources.containsKey(source.name)) { |
61 throw new StateError('Source registry already has a source named ' | 61 throw new StateError('Source registry already has a source named ' |
62 '${source.name}'); | 62 '${source.name}'); |
63 } | 63 } |
64 | 64 |
65 _sources[source.name] = source; | 65 _sources[source.name] = source; |
66 } | 66 } |
67 | 67 |
| 68 /// Loads the package identified by [id]. |
| 69 /// |
| 70 /// Throws an [ArgumentError] if [id] has an invalid source. |
| 71 Package load(PackageId id) { |
| 72 var source = this[id.source]; |
| 73 if (source == null) throw new ArgumentError("Unknown source ${id.source}."); |
| 74 |
| 75 var dir = source.getDirectory(id); |
| 76 return new Package.load(id.name, dir, this); |
| 77 } |
| 78 |
68 /// Returns the source named [name]. | 79 /// Returns the source named [name]. |
69 /// | 80 /// |
70 /// Returns an [UnknownSource] if no source with that name has been | 81 /// Returns an [UnknownSource] if no source with that name has been |
71 /// registered. If [name] is null, returns the default source. | 82 /// registered. If [name] is null, returns the default source. |
72 Source operator[](String name) { | 83 Source operator[](String name) { |
73 if (name == null) { | 84 if (name == null) { |
74 if (defaultSource != null) return defaultSource; | 85 if (defaultSource != null) return defaultSource; |
75 throw new StateError('No default source has been registered'); | 86 throw new StateError('No default source has been registered'); |
76 } | 87 } |
77 | 88 |
78 if (_sources.containsKey(name)) return _sources[name]; | 89 if (_sources.containsKey(name)) return _sources[name]; |
79 return new UnknownSource(name); | 90 return new UnknownSource(name); |
80 } | 91 } |
81 } | 92 } |
OLD | NEW |