Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, 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.pub_package_provider; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:io'; | |
| 9 | |
| 10 import 'package:barback/barback.dart'; | |
| 11 import 'package:path/path.dart' as path; | |
| 12 | |
| 13 import 'entrypoint.dart'; | |
| 14 import 'io.dart'; | |
| 15 | |
| 16 /// An implementation of barback's [PackageProvider] interface so that barback | |
| 17 /// can assets within pub packages. | |
| 18 class PubPackageProvider implements PackageProvider { | |
| 19 /// The [Entrypoint] package being served. | |
| 20 final Entrypoint _entrypoint; | |
| 21 | |
| 22 /// Maps the names of all of the packages in [_entrypoint]'s transitive | |
| 23 /// dependency graph to the local path of the directory for that package. | |
| 24 final Map<String, String> _packageDirs; | |
| 25 | |
| 26 /// Creates a new provider for [entrypoint]. | |
| 27 static Future<PubPackageProvider> create(Entrypoint entrypoint) { | |
| 28 var packageDirs = <String, String>{}; | |
| 29 | |
| 30 packageDirs[entrypoint.root.name] = entrypoint.root.dir; | |
| 31 | |
| 32 // Cache package directories up front so we can have synchronous access | |
| 33 // to them. | |
| 34 // TODO(rnystrom): Handle missing or out of date lockfile. | |
| 35 var futures = []; | |
| 36 entrypoint.loadLockFile().packages.forEach((name, package) { | |
| 37 var source = entrypoint.cache.sources[package.source]; | |
| 38 futures.add(source.getDirectory(package).then((packageDir) { | |
| 39 packageDirs[name] = packageDir; | |
| 40 })); | |
| 41 }); | |
| 42 | |
| 43 return Future.wait(futures).then((_) { | |
| 44 return new PubPackageProvider._(entrypoint, packageDirs); | |
| 45 }); | |
| 46 } | |
| 47 | |
| 48 PubPackageProvider._(this._entrypoint, this._packageDirs); | |
| 49 | |
| 50 Iterable<String> get packages => _packageDirs.keys; | |
| 51 | |
| 52 List<AssetId> listAssets(String package, {String within}) { | |
|
nweiz
2013/07/29 20:23:15
Since this is now not part of the PackageProvider
Bob Nystrom
2013/07/29 21:45:43
Done.
| |
| 53 var files = <AssetId>[]; | |
| 54 | |
| 55 addFiles(String dirPath) { | |
| 56 var packageDir = _packageDirs[package]; | |
| 57 var dir = path.join(packageDir, dirPath); | |
| 58 if (!dirExists(dir)) return; | |
| 59 var entries = listDir(dir, recursive: true); | |
| 60 files.addAll(entries | |
| 61 .where((entry) => !path.split(entry).contains("packages")) | |
| 62 .where(fileExists) | |
| 63 .map((entry) => new AssetId(package, | |
| 64 path.relative(entry, from: packageDir)))); | |
| 65 } | |
| 66 | |
| 67 // If we aren't given a subdirectory, use all externally visible files. | |
| 68 if (within == null) { | |
| 69 // Expose the "asset" and "lib" directories. | |
| 70 addFiles("asset"); | |
| 71 addFiles("lib"); | |
| 72 | |
| 73 // The entrypoint's "web" directory is also visible. | |
| 74 if (package == _entrypoint.root.name) { | |
| 75 addFiles("web"); | |
| 76 } | |
| 77 } else { | |
| 78 addFiles(within); | |
| 79 } | |
| 80 | |
| 81 return files; | |
| 82 } | |
| 83 | |
| 84 // TODO(rnystrom): Actually support transformers. | |
| 85 Iterable<Iterable<Transformer>> getTransformers(String package) => []; | |
| 86 | |
| 87 Future<Asset> getAsset(AssetId id) { | |
| 88 var file = path.join(_packageDirs[id.package], id.path); | |
| 89 return new Future.value(new Asset.fromPath(id, file)); | |
| 90 } | |
| 91 } | |
| OLD | NEW |