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 library pub.source_registry; | |
6 | |
7 import 'dart:collection'; | |
8 | |
9 import 'package.dart'; | |
10 import 'source.dart'; | |
11 import 'source/unknown.dart'; | |
12 | |
13 /// A class that keeps track of [Source]s used for getting packages. | |
14 class SourceRegistry extends IterableBase<Source> { | |
15 final _sources = new Map<String, Source>(); | |
16 Source _default; | |
17 | |
18 /// Returns the default source, which is used when no source is specified. | |
19 Source get defaultSource => _default; | |
20 | |
21 /// Iterates over the registered sources in name order. | |
22 Iterator<Source> get iterator { | |
23 var sources = _sources.values.toList(); | |
24 sources.sort((a, b) => a.name.compareTo(b.name)); | |
25 return sources.iterator; | |
26 } | |
27 | |
28 /// Returns whether [id1] and [id2] refer to the same package, including | |
29 /// validating that their descriptions are equivalent. | |
30 bool idsEqual(PackageId id1, PackageId id2) { | |
31 if (id1 != id2) return false; | |
32 if (id1 == null && id2 == null) return true; | |
33 return idDescriptionsEqual(id1, id2); | |
34 } | |
35 | |
36 /// Returns whether [id1] and [id2] have the same source and description. | |
37 /// | |
38 /// This doesn't check whether the name or versions are equal. | |
39 bool idDescriptionsEqual(PackageId id1, PackageId id2) { | |
40 if (id1.source != id2.source) return false; | |
41 return this[id1.source].descriptionsEqual(id1.description, id2.description); | |
42 } | |
43 | |
44 /// Sets the default source. | |
45 /// | |
46 /// This takes a string, which must be the name of a registered source. | |
47 void setDefault(String name) { | |
48 if (!_sources.containsKey(name)) { | |
49 throw new StateError('Default source $name is not in the registry'); | |
50 } | |
51 | |
52 _default = _sources[name]; | |
53 } | |
54 | |
55 /// Registers a new source. | |
56 /// | |
57 /// This source may not have the same name as a source that's already been | |
58 /// registered. | |
59 void register(Source source) { | |
60 if (_sources.containsKey(source.name)) { | |
61 throw new StateError( | |
62 'Source registry already has a source named ' '${source.name}'); | |
63 } | |
64 | |
65 _sources[source.name] = source; | |
66 } | |
67 | |
68 /// Returns the source named [name]. | |
69 /// | |
70 /// Returns an [UnknownSource] if no source with that name has been | |
71 /// registered. If [name] is null, returns the default source. | |
72 Source operator [](String name) { | |
73 if (name == null) { | |
74 if (defaultSource != null) return defaultSource; | |
75 throw new StateError('No default source has been registered'); | |
76 } | |
77 | |
78 if (_sources.containsKey(name)) return _sources[name]; | |
79 return new UnknownSource(name); | |
80 } | |
81 } | |
OLD | NEW |