OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 | 7 |
| 8 import 'package:async/async.dart'; |
8 import 'package:path/path.dart' as p; | 9 import 'package:path/path.dart' as p; |
9 import 'package:pub/src/io.dart'; | 10 import 'package:pub/src/io.dart'; |
10 import 'package:pub/src/utils.dart'; | 11 import 'package:pub/src/utils.dart'; |
11 import 'package:pub_semver/pub_semver.dart'; | 12 import 'package:pub_semver/pub_semver.dart'; |
12 import 'package:scheduled_test/scheduled_test.dart'; | 13 import 'package:scheduled_test/scheduled_test.dart'; |
13 import 'package:yaml/yaml.dart'; | 14 import 'package:yaml/yaml.dart'; |
14 | 15 |
15 import 'descriptor.dart' as d; | 16 import 'descriptor.dart' as d; |
16 import 'test_pub.dart'; | 17 import 'test_pub.dart'; |
17 | 18 |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 PackageServerBuilder._(); | 144 PackageServerBuilder._(); |
144 | 145 |
145 /// Specifies that a package named [name] with [version] should be served. | 146 /// Specifies that a package named [name] with [version] should be served. |
146 /// | 147 /// |
147 /// If [deps] is passed, it's used as the "dependencies" field of the pubspec. | 148 /// If [deps] is passed, it's used as the "dependencies" field of the pubspec. |
148 /// If [pubspec] is passed, it's used as the rest of the pubspec. Either of | 149 /// If [pubspec] is passed, it's used as the rest of the pubspec. Either of |
149 /// these may recursively contain Futures. | 150 /// these may recursively contain Futures. |
150 /// | 151 /// |
151 /// If [contents] is passed, it's used as the contents of the package. By | 152 /// If [contents] is passed, it's used as the contents of the package. By |
152 /// default, a package just contains a dummy lib directory. | 153 /// default, a package just contains a dummy lib directory. |
153 void serve(String name, String version, {Map deps, Map pubspec, | 154 void serve(String name, String version, {Map<String, dynamic> deps, |
154 Iterable<d.Descriptor> contents}) { | 155 Map<String, dynamic> pubspec, Iterable<d.Descriptor> contents}) { |
155 _futures.add(Future.wait([ | 156 _futures.add(new Future.sync(() async { |
156 awaitObject(deps), | 157 var resolvedDeps = await awaitObject(deps); |
157 awaitObject(pubspec) | 158 var resolvedPubspec = await awaitObject(pubspec); |
158 ]).then((pair) { | |
159 var resolvedDeps = pair.first; | |
160 var resolvedPubspec = pair.last; | |
161 | 159 |
162 var pubspecFields = { | 160 var pubspecFields = <String, dynamic>{ |
163 "name": name, | 161 "name": name, |
164 "version": version | 162 "version": version |
165 }; | 163 }; |
166 if (resolvedPubspec != null) pubspecFields.addAll(resolvedPubspec); | 164 if (resolvedPubspec != null) pubspecFields.addAll(resolvedPubspec); |
167 if (resolvedDeps != null) pubspecFields["dependencies"] = resolvedDeps; | 165 if (resolvedDeps != null) pubspecFields["dependencies"] = resolvedDeps; |
168 | 166 |
169 if (contents == null) contents = [d.libDir(name, "$name $version")]; | 167 if (contents == null) contents = [d.libDir(name, "$name $version")]; |
170 contents = [d.file("pubspec.yaml", yaml(pubspecFields))] | 168 contents = [d.file("pubspec.yaml", yaml(pubspecFields))] |
171 ..addAll(contents); | 169 ..addAll(contents); |
172 | 170 |
(...skipping 25 matching lines...) Expand all Loading... |
198 if (pubspec.containsKey('dependencies')) { | 196 if (pubspec.containsKey('dependencies')) { |
199 pubspec['dependencies'].keys.forEach(_addPackage); | 197 pubspec['dependencies'].keys.forEach(_addPackage); |
200 } | 198 } |
201 } | 199 } |
202 | 200 |
203 _addPackage(package); | 201 _addPackage(package); |
204 } | 202 } |
205 | 203 |
206 /// Returns a Future that completes once all the [serve] calls have been fully | 204 /// Returns a Future that completes once all the [serve] calls have been fully |
207 /// processed. | 205 /// processed. |
208 Future _await() { | 206 Future _await() async { |
209 if (_futures.futures.isEmpty) return new Future.value(); | 207 _futures.close(); |
210 return _futures.future.then((_) { | 208 await _futures.future; |
211 _futures = new FutureGroup(); | 209 _futures = new FutureGroup(); |
212 }); | |
213 } | 210 } |
214 | 211 |
215 /// Clears all existing packages from this builder. | 212 /// Clears all existing packages from this builder. |
216 void _clear() { | 213 void _clear() { |
217 _packages.clear(); | 214 _packages.clear(); |
218 } | 215 } |
219 } | 216 } |
220 | 217 |
221 /// A package that's intended to be served. | 218 /// A package that's intended to be served. |
222 class _ServedPackage { | 219 class _ServedPackage { |
223 final Map pubspec; | 220 final Map pubspec; |
224 final List<d.Descriptor> contents; | 221 final List<d.Descriptor> contents; |
225 | 222 |
226 Version get version => new Version.parse(pubspec['version']); | 223 Version get version => new Version.parse(pubspec['version']); |
227 | 224 |
228 _ServedPackage(this.pubspec, this.contents); | 225 _ServedPackage(this.pubspec, this.contents); |
229 } | 226 } |
OLD | NEW |