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

Side by Side Diff: utils/pub/git_source.dart

Issue 12079112: Make a bunch of stuff in pub synchronous. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library git_source; 5 library git_source;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'git.dart' as git; 8 import 'git.dart' as git;
9 import 'io.dart'; 9 import 'io.dart';
10 import 'package.dart'; 10 import 'package.dart';
(...skipping 24 matching lines...) Expand all
35 Future<Package> installToSystemCache(PackageId id) { 35 Future<Package> installToSystemCache(PackageId id) {
36 var revisionCachePath; 36 var revisionCachePath;
37 37
38 return git.isInstalled.then((installed) { 38 return git.isInstalled.then((installed) {
39 if (!installed) { 39 if (!installed) {
40 throw new Exception( 40 throw new Exception(
41 "Cannot install '${id.name}' from Git (${_getUrl(id)}).\n" 41 "Cannot install '${id.name}' from Git (${_getUrl(id)}).\n"
42 "Please ensure Git is correctly installed."); 42 "Please ensure Git is correctly installed.");
43 } 43 }
44 44
45 return ensureDir(join(systemCacheRoot, 'cache')); 45 ensureDir(join(systemCacheRoot, 'cache'));
46 }).then((_) => _ensureRepoCache(id)) 46 return _ensureRepoCache(id);
47 .then((_) => _revisionCachePath(id)) 47 }).then((_) => _revisionCachePath(id))
48 .then((path) { 48 .then((path) {
49 revisionCachePath = path; 49 revisionCachePath = path;
50 return exists(revisionCachePath); 50 if (entryExists(revisionCachePath)) return;
51 }).then((exists) {
52 if (exists) return;
53 return _clone(_repoCachePath(id), revisionCachePath, mirror: false); 51 return _clone(_repoCachePath(id), revisionCachePath, mirror: false);
54 }).then((_) { 52 }).then((_) {
55 var ref = _getEffectiveRef(id); 53 var ref = _getEffectiveRef(id);
56 if (ref == 'HEAD') return; 54 if (ref == 'HEAD') return;
57 return _checkOut(revisionCachePath, ref); 55 return _checkOut(revisionCachePath, ref);
58 }).then((_) { 56 }).then((_) {
59 return Package.load(id.name, revisionCachePath, systemCache.sources); 57 return new Package(id.name, revisionCachePath, systemCache.sources);
60 }); 58 });
61 } 59 }
62 60
63 /// Ensures [description] is a Git URL. 61 /// Ensures [description] is a Git URL.
64 void validateDescription(description, {bool fromLockFile: false}) { 62 void validateDescription(description, {bool fromLockFile: false}) {
65 // A single string is assumed to be a Git URL. 63 // A single string is assumed to be a Git URL.
66 if (description is String) return; 64 if (description is String) return;
67 if (description is! Map || !description.containsKey('url')) { 65 if (description is! Map || !description.containsKey('url')) {
68 throw new FormatException("The description must be a Git URL or a map " 66 throw new FormatException("The description must be a Git URL or a map "
69 "with a 'url' key."); 67 "with a 'url' key.");
(...skipping 27 matching lines...) Expand all
97 description['resolved-ref'] = revision; 95 description['resolved-ref'] = revision;
98 return new PackageId(id.name, this, id.version, description); 96 return new PackageId(id.name, this, id.version, description);
99 }); 97 });
100 } 98 }
101 99
102 /// Ensure that the canonical clone of the repository referred to by [id] (the 100 /// Ensure that the canonical clone of the repository referred to by [id] (the
103 /// one in `<system cache>/git/cache`) exists and is up-to-date. Returns a 101 /// one in `<system cache>/git/cache`) exists and is up-to-date. Returns a
104 /// future that completes once this is finished and throws an exception if it 102 /// future that completes once this is finished and throws an exception if it
105 /// fails. 103 /// fails.
106 Future _ensureRepoCache(PackageId id) { 104 Future _ensureRepoCache(PackageId id) {
107 var path = _repoCachePath(id); 105 // Make sure all errors propagate through future.
108 return exists(path).then((exists) { 106 return defer(() {
109 if (!exists) return _clone(_getUrl(id), path, mirror: true); 107 var path = _repoCachePath(id);
110 108 if (!entryExists(path)) return _clone(_getUrl(id), path, mirror: true);
111 return git.run(["fetch"], workingDir: path).then((result) => null); 109 return git.run(["fetch"], workingDir: path).then((result) => null);
112 }); 110 });
113 } 111 }
114 112
115 /// Returns a future that completes to the revision hash of [id]. 113 /// Returns a future that completes to the revision hash of [id].
116 Future<String> _revisionAt(PackageId id) { 114 Future<String> _revisionAt(PackageId id) {
117 return git.run(["rev-parse", _getEffectiveRef(id)], 115 return git.run(["rev-parse", _getEffectiveRef(id)],
118 workingDir: _repoCachePath(id)).then((result) => result[0]); 116 workingDir: _repoCachePath(id)).then((result) => result[0]);
119 } 117 }
120 118
121 /// Returns the path to the revision-specific cache of [id]. 119 /// Returns the path to the revision-specific cache of [id].
122 Future<String> _revisionCachePath(PackageId id) { 120 Future<String> _revisionCachePath(PackageId id) {
123 return _revisionAt(id).then((rev) { 121 return _revisionAt(id).then((rev) {
124 var revisionCacheName = '${id.name}-$rev'; 122 var revisionCacheName = '${id.name}-$rev';
125 return join(systemCacheRoot, revisionCacheName); 123 return join(systemCacheRoot, revisionCacheName);
126 }); 124 });
127 } 125 }
128 126
129 /// Clones the repo at the URI [from] to the path [to] on the local 127 /// Clones the repo at the URI [from] to the path [to] on the local
130 /// filesystem. 128 /// filesystem.
131 /// 129 ///
132 /// If [mirror] is true, create a bare, mirrored clone. This doesn't check out 130 /// If [mirror] is true, create a bare, mirrored clone. This doesn't check out
133 /// the working tree, but instead makes the repository a local mirror of the 131 /// the working tree, but instead makes the repository a local mirror of the
134 /// remote repository. See the manpage for `git clone` for more information. 132 /// remote repository. See the manpage for `git clone` for more information.
135 Future _clone(String from, String to, {bool mirror: false}) { 133 Future _clone(String from, String to, {bool mirror: false}) {
136 // Git on Windows does not seem to automatically create the destination 134 // Make sure exceptions propagate through future.
137 // directory. 135 return defer(() {
138 return ensureDir(to).then((_) { 136 // Git on Windows does not seem to automatically create the destination
137 // directory.
138 ensureDir(to);
139 var args = ["clone", from, to]; 139 var args = ["clone", from, to];
140 if (mirror) args.insertRange(1, 1, "--mirror"); 140 if (mirror) args.insertRange(1, 1, "--mirror");
141 return git.run(args); 141 return git.run(args);
142 }).then((result) => null); 142 }).then((result) => null);
143 } 143 }
144 144
145 /// Checks out the reference [ref] in [repoPath]. 145 /// Checks out the reference [ref] in [repoPath].
146 Future _checkOut(String repoPath, String ref) { 146 Future _checkOut(String repoPath, String ref) {
147 return git.run(["checkout", ref], workingDir: repoPath).then( 147 return git.run(["checkout", ref], workingDir: repoPath).then(
148 (result) => null); 148 (result) => null);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 return description['ref']; 191 return description['ref'];
192 } 192 }
193 193
194 /// Returns [description] if it's a description, or [PackageId.description] if 194 /// Returns [description] if it's a description, or [PackageId.description] if
195 /// it's a [PackageId]. 195 /// it's a [PackageId].
196 _getDescription(description) { 196 _getDescription(description) {
197 if (description is PackageId) return description.description; 197 if (description is PackageId) return description.description;
198 return description; 198 return description;
199 } 199 }
200 } 200 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698