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 /// Lists all of the visible files in [package]. |
| 53 /// |
| 54 /// This is the recursive contents of the "asset" and "lib" directories (if |
| 55 /// present). If [package] is the entrypoint package, it also includes the |
| 56 /// contents of "web". |
| 57 List<AssetId> listAssets(String package) { |
| 58 var files = <AssetId>[]; |
| 59 |
| 60 addFiles(String dirPath) { |
| 61 var packageDir = _packageDirs[package]; |
| 62 var dir = path.join(packageDir, dirPath); |
| 63 if (!dirExists(dir)) return; |
| 64 var entries = listDir(dir, recursive: true); |
| 65 files.addAll(entries |
| 66 .where((entry) => !path.split(entry).contains("packages")) |
| 67 .where(fileExists) |
| 68 .map((entry) => new AssetId(package, |
| 69 path.relative(entry, from: packageDir)))); |
| 70 } |
| 71 |
| 72 // Expose the "asset" and "lib" directories. |
| 73 addFiles("asset"); |
| 74 addFiles("lib"); |
| 75 |
| 76 // The entrypoint's "web" directory is also visible. |
| 77 if (package == _entrypoint.root.name) { |
| 78 addFiles("web"); |
| 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 |