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