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

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

Issue 1142363005: Added .packages, packages/ discovery. (Closed) Base URL: https://github.com/dart-lang/package_config.git@master
Patch Set: Created 5 years, 7 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
(Empty)
1 // Copyright (c) 2015, 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 package_config.packages_impl;
6
7 import "dart:collection" show UnmodifiableMapView;
8 import "dart:io" show Directory;
9 import "package:path/path.dart" as path;
10 import "../packages.dart";
11 import "util.dart" show checkValidPackageUri;
12
13 /// A [Packages] null-object.
14 class NoPackages implements Packages {
15 const NoPackages();
16
17 Uri resolve(Uri packageUri, {Uri notFound(Uri packageUri)}) {
18 String packageName = checkValidPackageUri(packageUri);
19 if (notFound != null) return notFound(packageUri);
20 throw new ArgumentError.value(packageUri, "packageUri",
21 'No package named "$packageName"');
22 }
23
24 Iterable<String> get packages {
25 return new Iterable<String>.empty();
26 }
pquitslund 2015/05/21 18:14:22 Maybe: Iterable<String> get packages => new Itera
Lasse Reichstein Nielsen 2015/05/21 18:30:21 I think it fits on a line, so yes.
27
28 Map<String, Uri> asMap() => const<String,Uri>{};
29 }
30
31 abstract class PackagesBase implements Packages {
pquitslund 2015/05/21 18:14:23 Nit: class level comment for PackageBase?
Lasse Reichstein Nielsen 2015/05/21 18:30:22 Acknowledged.
32 Uri resolve(Uri packageUri, {Uri notFound(Uri packageUri)}) {
33 packageUri = packageUri.normalizePath();
34 String packageName = checkValidPackageUri(packageUri);
35 Uri packageBase = _getBase(packageName);
36 if (packageBase == null) {
37 if (notFound != null) return notFound(packageUri);
38 throw new ArgumentError.value(packageUri, "packageUri",
39 'No package named "$packageName"');
40 }
41 String packagePath = packageUri.path.substring(packageName.length + 1);
42 return packageBase.resolve(packagePath);
43 }
44 Uri _getBase(String packageName);
45 }
46
47 /// A [Packages] implementation based on an existing map.
48 class MapPackages extends PackagesBase {
49 final Map<String, Uri> _mapping;
50 MapPackages(this._mapping);
51
52 Uri _getBase(String packageName) => _mapping[packageName];
53
54 Iterable<String> get packages => _mapping.keys;
55
56 Map<String, Uri> asMap() => new UnmodifiableMapView<String, Uri>(_mapping);
57 }
58
59 /// A [Packages] implementation based on a local directory.
60 class FilePackagesDirectoryPackages extends PackagesBase {
61 final Directory _packageDir;
62 FilePackagesDirectoryPackages(this._packageDir);
63
64 Uri _getBase(String packageName) =>
65 new Uri.directory(path.join(packageName,''));
66
67 Iterable<String> _listPackageNames() {
68 return _packageDir.listSync()
69 .where((e) => e is Directory)
70 .map((e) => path.basename(e.path));
71 }
72
73 Iterable<String> get packages {
74 return _listPackageNames();
75 }
pquitslund 2015/05/21 18:14:22 Maybe: Iterable<String> get packages => _listPack
Lasse Reichstein Nielsen 2015/05/21 18:30:21 Acknowledged.
76
77 Map<String, Uri> asMap() {
78 var result = <String, Uri>{};
79 for (var packageName in _listPackageNames()) {
80 result[packageName] = _getBase(packageName);
81 }
82 return new UnmodifiableMapView<String, Uri>(result);
83 }
84 }
85
86 /// A [Packages] implementation based on a remote (e.g., HTTP) directory.
87 ///
88 /// There is no way to detect which packages exist short of trying to use
89 /// them. You can't necessarily check whether a directory exists,
90 /// except by checking for a know file in the directory.
91 class NonFilePackagesDirectoryPackages extends PackagesBase {
92 final Uri _packageBase;
93 NonFilePackagesDirectoryPackages(this._packageBase);
94
95 Uri _getBase(String packageName) => _packageBase.resolve("$packageName/");
96
97 Error _failListingPackages() {
98 return new UnsupportedError(
99 "Cannot list packages for a ${_packageBase.scheme}: "
100 "based package root");
101 }
102
103 Iterable<String> get packages {
104 throw _failListingPackages();
105 }
106
107 Map<String, Uri> asMap() {
108 throw _failListingPackages();
109 }
110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698