| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * A class that keeps track of [Source]s used for installing packages. | |
| 7 */ | |
| 8 class SourceRegistry { | |
| 9 final Map<String, Source> _map; | |
| 10 Source _default; | |
| 11 | |
| 12 /** | |
| 13 * Creates a new registry with no packages registered. | |
| 14 */ | |
| 15 SourceRegistry() : _map = <Source>{}; | |
| 16 | |
| 17 /** | |
| 18 * Returns the default source, which is used when no source is specified. | |
| 19 */ | |
| 20 Source get defaultSource() => _default; | |
| 21 | |
| 22 /** | |
| 23 * Sets the default source. This takes a string, which must be the name of a | |
| 24 * registered source. | |
| 25 */ | |
| 26 void setDefault(String name) { | |
| 27 if (!_map.containsKey(name)) { | |
| 28 // TODO(nweiz): Real error-handling system | |
| 29 throw 'Default source $name is not in the registry'; | |
| 30 } | |
| 31 | |
| 32 _default = _map[name]; | |
| 33 } | |
| 34 | |
| 35 /** | |
| 36 * Registers a new source. This source may not have the same name as a source | |
| 37 * that's already been registered. | |
| 38 */ | |
| 39 void register(Source source) { | |
| 40 if (_map.containsKey(source.name)) { | |
| 41 // TODO(nweiz): Real error-handling system | |
| 42 throw 'Source registry already has a source named ${source.name}'; | |
| 43 } | |
| 44 | |
| 45 _map[source.name] = source; | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * Returns the source named [name]. Throws an error if no such source has been | |
| 50 * registered. If [name] is null, returns the default source. | |
| 51 */ | |
| 52 Source operator[](String name) { | |
| 53 if (name == null) { | |
| 54 if (defaultSource != null) return defaultSource; | |
| 55 // TODO(nweiz): Real error-handling system | |
| 56 throw 'No default source has been registered'; | |
| 57 } | |
| 58 if (_map.containsKey(name)) return _map[name]; | |
| 59 throw 'No source named $name is registered'; | |
| 60 } | |
| 61 } | |
| OLD | NEW |