Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: lib/src/source_registry.dart

Issue 2044253003: Refactor Source and SourceRegistry. (Closed) Base URL: git@github.com:dart-lang/pub.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 import 'dart:collection';
6
7 import 'package.dart'; 5 import 'package.dart';
8 import 'source.dart'; 6 import 'source.dart';
7 import 'source/git.dart';
8 import 'source/hosted.dart';
9 import 'source/path.dart';
9 import 'source/unknown.dart'; 10 import 'source/unknown.dart';
10 11
11 /// A class that keeps track of [Source]s used for getting packages. 12 /// A class that keeps track of [Source]s used for getting packages.
12 class SourceRegistry extends IterableBase<Source> { 13 class SourceRegistry {
13 final _sources = new Map<String, Source>(); 14 /// The registered sources.
15 ///
16 /// This is initialized with the three built-in sources.
17 final _sources = {
18 "git": new GitSource(),
19 "hosted": new HostedSource(),
20 "path": new PathSource()
21 };
22
23 /// The default source, which is used when no source is specified.
24 ///
25 /// This defaults to [hosted].
26 Source get defaultSource => _default;
14 Source _default; 27 Source _default;
15 28
16 /// Returns the default source, which is used when no source is specified. 29 /// The registered sources, in name order.
17 Source get defaultSource => _default; 30 List<Source> get sources {
Bob Nystrom 2016/06/14 23:21:55 What do you think of calling this "all"? Elsewher
nweiz 2016/06/20 20:46:08 Done.
18
19 /// Iterates over the registered sources in name order.
20 Iterator<Source> get iterator {
21 var sources = _sources.values.toList(); 31 var sources = _sources.values.toList();
22 sources.sort((a, b) => a.name.compareTo(b.name)); 32 sources.sort((a, b) => a.name.compareTo(b.name));
23 return sources.iterator; 33 return sources;
34 }
35
36 /// The built-in [GitSource].
37 GitSource get git => _sources["git"] as GitSource;
38
39 /// The built-in [HostedSource].
40 HostedSource get hosted => _sources["hosted"] as HostedSource;
41
42 /// The built-in [PathSource].
43 PathSource get path => _sources["path"] as PathSource;
44
45 SourceRegistry() {
46 _default = hosted;
24 } 47 }
25 48
26 /// Returns whether [id1] and [id2] refer to the same package, including 49 /// Returns whether [id1] and [id2] refer to the same package, including
27 /// validating that their descriptions are equivalent. 50 /// validating that their descriptions are equivalent.
28 bool idsEqual(PackageId id1, PackageId id2) { 51 bool idsEqual(PackageId id1, PackageId id2) {
29 if (id1 != id2) return false; 52 if (id1 != id2) return false;
30 if (id1 == null && id2 == null) return true; 53 if (id1 == null && id2 == null) return true;
31 return idDescriptionsEqual(id1, id2); 54 return idDescriptionsEqual(id1, id2);
32 } 55 }
33 56
(...skipping 22 matching lines...) Expand all
56 /// registered. 79 /// registered.
57 void register(Source source) { 80 void register(Source source) {
58 if (_sources.containsKey(source.name)) { 81 if (_sources.containsKey(source.name)) {
59 throw new StateError('Source registry already has a source named ' 82 throw new StateError('Source registry already has a source named '
60 '${source.name}'); 83 '${source.name}');
61 } 84 }
62 85
63 _sources[source.name] = source; 86 _sources[source.name] = source;
64 } 87 }
65 88
66 /// Loads the package identified by [id].
67 ///
68 /// Throws an [ArgumentError] if [id] has an invalid source.
69 Package load(PackageId id) {
70 var source = this[id.source];
71 if (source == null) throw new ArgumentError("Unknown source ${id.source}.");
72
73 var dir = source.getDirectory(id);
74 return new Package.load(id.name, dir, this);
75 }
76
77 /// Returns the source named [name]. 89 /// Returns the source named [name].
78 /// 90 ///
79 /// Returns an [UnknownSource] if no source with that name has been 91 /// Returns an [UnknownSource] if no source with that name has been
80 /// registered. If [name] is null, returns the default source. 92 /// registered. If [name] is null, returns the default source.
81 Source operator[](String name) { 93 Source operator[](String name) {
82 if (name == null) { 94 if (name == null) return _default;
83 if (defaultSource != null) return defaultSource;
84 throw new StateError('No default source has been registered');
85 }
86
87 if (_sources.containsKey(name)) return _sources[name]; 95 if (_sources.containsKey(name)) return _sources[name];
88 return new UnknownSource(name); 96 return new UnknownSource(name);
89 } 97 }
90 } 98 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698