Chromium Code Reviews| Index: utils/pub/source_registry.dart |
| diff --git a/utils/pub/source_registry.dart b/utils/pub/source_registry.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..85518b949fb0a6b107bb0de89c8ec01938f5e558 |
| --- /dev/null |
| +++ b/utils/pub/source_registry.dart |
| @@ -0,0 +1,61 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/** |
| + * A class that keeps track of [Source]s used for installing packages. |
| + */ |
| +class SourceRegistry { |
| + final Map<String, Source> _map; |
| + Source _default; |
| + |
| + /** |
| + * Creates a new registry with no packages registered. |
| + */ |
| + SourceRegistry() : _map = new Map<String, Source>(); |
|
Bob Nystrom
2012/05/08 17:01:49
<Source>{}
nweiz
2012/05/09 00:21:04
Done.
|
| + |
| + /** |
| + * Returns the default source, which is used when no source is specified. |
| + */ |
| + Source get defaultSource() => _default; |
| + |
| + /** |
| + * Sets the default source. This takes a string, which must be the name of a |
| + * registered source. |
| + */ |
| + void setDefault(String name) { |
| + if (!_map.containsKey(name)) { |
| + // TODO(nweiz): Real error-handling system |
|
Bob Nystrom
2012/05/08 17:01:49
Now may be as good a time as any to do the right e
nweiz
2012/05/09 00:21:04
I'm going to do this in a separate CL, since it's
|
| + throw 'Default source $name is not in the registry'; |
| + } |
| + |
| + _default = _map[name]; |
| + } |
| + |
| + /** |
| + * Registers a new source. This source may not have the same name as a source |
| + * that's already been registered. |
| + */ |
| + void register(Source source) { |
| + if (_map.containsKey(source.name)) { |
| + // TODO(nweiz): Real error-handling system |
| + throw 'Source registry already has a source named ${source.name}'; |
| + } |
| + |
| + _map[source.name] = source; |
| + } |
| + |
| + /** |
| + * Returns the source named [name]. Throws an error if no such source has been |
| + * registered. If [name] is null, returns the default source. |
| + */ |
| + Source operator[](String name) { |
| + if (name == null) { |
| + if (defaultSource != null) return defaultSource; |
| + // TODO(nweiz): Real error-handling system |
| + throw 'No default source has been registered'; |
| + } |
| + if (_map.containsKey(name)) return _map[name]; |
| + throw 'No source named $name is registered'; |
| + } |
| +} |