OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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.source.cached; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:path/path.dart' as path; | |
10 | |
11 import '../io.dart'; | |
12 import '../package.dart'; | |
13 import '../pubspec.dart'; | |
14 import '../source.dart'; | |
15 import '../utils.dart'; | |
16 | |
17 /// Base class for a [Source] that installs packages into pub's [SystemCache]. | |
18 /// | |
19 /// A source should be cached if it requires network access to retrieve | |
20 /// packages or the package needs to be "frozen" at the point in time that it's | |
21 /// installed. (For example, Git packages are cached because installing from | |
22 /// the same repo over time may yield different commits.) | |
23 abstract class CachedSource extends Source { | |
24 /// The root directory of this source's cache within the system cache. | |
25 /// | |
26 /// This shouldn't be overridden by subclasses. | |
27 String get systemCacheRoot => path.join(systemCache.rootDir, name); | |
28 | |
29 /// If [id] is already in the system cache, just loads it from there. | |
30 /// | |
31 /// Otherwise, defers to the subclass. | |
32 Future<Pubspec> doDescribe(PackageId id) { | |
33 return getDirectory(id).then((packageDir) { | |
34 if (fileExists(path.join(packageDir, "pubspec.yaml"))) { | |
35 return new Pubspec.load( | |
36 packageDir, | |
37 systemCache.sources, | |
38 expectedName: id.name); | |
39 } | |
40 | |
41 return describeUncached(id); | |
42 }); | |
43 } | |
44 | |
45 /// Loads the (possibly remote) pubspec for the package version identified by | |
46 /// [id]. | |
47 /// | |
48 /// This will only be called for packages that have not yet been installed in | |
49 /// the system cache. | |
50 Future<Pubspec> describeUncached(PackageId id); | |
51 | |
52 Future get(PackageId id, String symlink) { | |
53 return downloadToSystemCache(id).then((pkg) { | |
54 createPackageSymlink(id.name, pkg.dir, symlink); | |
55 }); | |
56 } | |
57 | |
58 /// Determines if the package with [id] is already downloaded to the system | |
59 /// cache. | |
60 Future<bool> isInSystemCache(PackageId id) => | |
61 getDirectory(id).then(dirExists); | |
62 | |
63 /// Downloads the package identified by [id] to the system cache. | |
64 Future<Package> downloadToSystemCache(PackageId id); | |
65 | |
66 /// Returns the [Package]s that have been downloaded to the system cache. | |
67 List<Package> getCachedPackages(); | |
68 | |
69 /// Reinstalls all packages that have been previously installed into the | |
70 /// system cache by this source. | |
71 /// | |
72 /// Returns a [Pair] whose first element is the number of packages | |
73 /// successfully repaired and the second is the number of failures. | |
74 Future<Pair<int, int>> repairCachedPackages(); | |
75 } | |
OLD | NEW |