| 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 | |
| 9 import 'package:barback/barback.dart'; | |
| 10 import 'package:path/path.dart' as path; | |
| 11 | |
| 12 import 'entrypoint.dart'; | |
| 13 | |
| 14 /// An implementation of barback's [PackageProvider] interface so that barback | |
| 15 /// can assets within pub packages. | |
| 16 class PubPackageProvider implements PackageProvider { | |
| 17 /// Maps the names of all of the packages in [_entrypoint]'s transitive | |
| 18 /// dependency graph to the local path of the directory for that package. | |
| 19 final Map<String, String> _packageDirs; | |
| 20 | |
| 21 /// Creates a new provider for [entrypoint]. | |
| 22 static Future<PubPackageProvider> create(Entrypoint entrypoint) { | |
| 23 var packageDirs = <String, String>{}; | |
| 24 | |
| 25 packageDirs[entrypoint.root.name] = entrypoint.root.dir; | |
| 26 | |
| 27 // Cache package directories up front so we can have synchronous access | |
| 28 // to them. | |
| 29 var futures = []; | |
| 30 entrypoint.loadLockFile().packages.forEach((name, package) { | |
| 31 var source = entrypoint.cache.sources[package.source]; | |
| 32 futures.add(source.getDirectory(package).then((packageDir) { | |
| 33 packageDirs[name] = packageDir; | |
| 34 })); | |
| 35 }); | |
| 36 | |
| 37 return Future.wait(futures).then((_) { | |
| 38 return new PubPackageProvider._(packageDirs); | |
| 39 }); | |
| 40 } | |
| 41 | |
| 42 PubPackageProvider._(this._packageDirs); | |
| 43 | |
| 44 Iterable<String> get packages => _packageDirs.keys; | |
| 45 | |
| 46 /// Gets the root directory of [package]. | |
| 47 String getPackageDir(String package) => _packageDirs[package]; | |
| 48 | |
| 49 Future<Asset> getAsset(AssetId id) { | |
| 50 var file = path.join(_packageDirs[id.package], id.path); | |
| 51 return new Future.value(new Asset.fromPath(id, file)); | |
| 52 } | |
| 53 } | |
| OLD | NEW |