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

Side by Side Diff: sdk/lib/_internal/pub_generated/test/serve_packages.dart

Issue 937243002: Revert "Revert "Use native async/await support in pub."" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 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
(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 serve_packages;
6
7 import 'dart:async';
8 import 'dart:convert';
9
10 import 'package:path/path.dart' as p;
11 import 'package:pub_semver/pub_semver.dart';
12 import 'package:scheduled_test/scheduled_test.dart';
13 import 'package:yaml/yaml.dart';
14
15 import '../lib/src/io.dart';
16 import '../lib/src/utils.dart';
17 import 'descriptor.dart' as d;
18 import 'test_pub.dart';
19
20 /// The [d.DirectoryDescriptor] describing the server layout of `/api/packages`
21 /// on the test server.
22 ///
23 /// This contains metadata for packages that are being served via
24 /// [servePackages]. It's `null` if [servePackages] has not yet been called for
25 /// this test.
26 d.DirectoryDescriptor _servedApiPackageDir;
27
28 /// The [d.DirectoryDescriptor] describing the server layout of `/packages` on
29 /// the test server.
30 ///
31 /// This contains the tarballs for packages that are being served via
32 /// [servePackages]. It's `null` if [servePackages] has not yet been called for
33 /// this test.
34 d.DirectoryDescriptor _servedPackageDir;
35
36 /// The current [PackageServerBuilder] that a user uses to specify which package
37 /// to serve.
38 ///
39 /// This is preserved over multiple calls to [servePackages] within the same
40 /// test so that additional packages can be added.
41 PackageServerBuilder _builder;
42
43 /// Creates an HTTP server that replicates the structure of pub.dartlang.org.
44 ///
45 /// Calls [callback] with a [PackageServerBuilder] that's used to specify
46 /// which packages to serve.
47 ///
48 /// If [replace] is false, subsequent calls to [servePackages] will add to the
49 /// set of packages that are being served. Previous packages will continue to be
50 /// served. Otherwise, the previous packages will no longer be served.
51 void servePackages(void callback(PackageServerBuilder builder), {bool replace:
52 false}) {
53 if (_servedPackageDir == null) {
54 _builder = new PackageServerBuilder();
55 _servedApiPackageDir = d.dir('packages', []);
56 _servedPackageDir = d.dir('packages', []);
57 serve([d.dir('api', [_servedApiPackageDir]), _servedPackageDir]);
58
59 currentSchedule.onComplete.schedule(() {
60 _builder = null;
61 _servedApiPackageDir = null;
62 _servedPackageDir = null;
63 }, 'cleaning up served packages');
64 }
65
66 schedule(() {
67 if (replace) _builder = new PackageServerBuilder();
68 callback(_builder);
69 return _builder._await().then((resolvedPubspecs) {
70 _servedApiPackageDir.contents.clear();
71 _servedPackageDir.contents.clear();
72 _builder._packages.forEach((name, versions) {
73 _servedApiPackageDir.contents.addAll([d.file('$name', JSON.encode({
74 'name': name,
75 'uploaders': ['nweiz@google.com'],
76 'versions': versions.map(
77 (version) => packageVersionApiMap(version.pubspec)).toList()
78 })), d.dir(name, [d.dir('versions', versions.map((version) {
79 return d.file(
80 version.version.toString(),
81 JSON.encode(packageVersionApiMap(version.pubspec, full: true)) );
82 }))])]);
83
84 _servedPackageDir.contents.add(
85 d.dir(
86 name,
87 [
88 d.dir(
89 'versions',
90 versions.map(
91 (version) => d.tar('${version.version}.tar.gz', vers ion.contents)))]));
92 });
93 });
94 }, 'initializing the package server');
95 }
96
97 /// Like [servePackages], but instead creates an empty server with no packages
98 /// registered.
99 ///
100 /// This will always replace a previous server.
101 void serveNoPackages() => servePackages((_) {}, replace: true);
102
103 /// A builder for specifying which packages should be served by [servePackages].
104 class PackageServerBuilder {
105 /// A map from package names to a list of concrete packages to serve.
106 final _packages = new Map<String, List<_ServedPackage>>();
107
108 /// A group of futures from [serve] calls.
109 ///
110 /// This should be accessed by calling [_awair].
111 var _futures = new FutureGroup();
112
113 /// Specifies that a package named [name] with [version] should be served.
114 ///
115 /// If [deps] is passed, it's used as the "dependencies" field of the pubspec.
116 /// If [pubspec] is passed, it's used as the rest of the pubspec. Either of
117 /// these may recursively contain Futures.
118 ///
119 /// If [contents] is passed, it's used as the contents of the package. By
120 /// default, a package just contains a dummy lib directory.
121 void serve(String name, String version, {Map deps, Map pubspec,
122 Iterable<d.Descriptor> contents}) {
123 _futures.add(
124 Future.wait([awaitObject(deps), awaitObject(pubspec)]).then((pair) {
125 var resolvedDeps = pair.first;
126 var resolvedPubspec = pair.last;
127
128 var pubspecFields = {
129 "name": name,
130 "version": version
131 };
132 if (resolvedPubspec != null) pubspecFields.addAll(resolvedPubspec);
133 if (resolvedDeps != null) pubspecFields["dependencies"] = resolvedDeps;
134
135 if (contents == null) contents = [d.libDir(name, "$name $version")];
136 contents =
137 [d.file("pubspec.yaml", yaml(pubspecFields))]..addAll(contents);
138
139 var packages = _packages.putIfAbsent(name, () => []);
140 packages.add(new _ServedPackage(pubspecFields, contents));
141 }));
142 }
143
144 /// Serves the versions of [package] and all its dependencies that are
145 /// currently checked into the Dart repository.
146 void serveRepoPackage(String package) {
147 _addPackage(name) {
148 if (_packages.containsKey(name)) return;
149 _packages[name] = [];
150
151 var root = packagePath(name);
152 var pubspec =
153 new Map.from(loadYaml(readTextFile(p.join(root, 'pubspec.yaml'))));
154
155 // Remove any SDK constraints since we don't have a valid SDK version
156 // while testing.
157 pubspec.remove('environment');
158
159 _packages[name].add(
160 new _ServedPackage(
161 pubspec,
162 [
163 d.file('pubspec.yaml', yaml(pubspec)),
164 new d.DirectoryDescriptor.fromFilesystem('lib', p.join(root, ' lib'))]));
165
166 if (pubspec.containsKey('dependencies')) {
167 pubspec['dependencies'].keys.forEach(_addPackage);
168 }
169 }
170
171 _addPackage(package);
172 }
173
174 /// Returns a Future that completes once all the [serve] calls have been fully
175 /// processed.
176 Future _await() {
177 if (_futures.futures.isEmpty) return new Future.value();
178 return _futures.future.then((_) {
179 _futures = new FutureGroup();
180 });
181 }
182 }
183
184 /// A package that's intended to be served.
185 class _ServedPackage {
186 final Map pubspec;
187 final List<d.Descriptor> contents;
188
189 Version get version => new Version.parse(pubspec['version']);
190
191 _ServedPackage(this.pubspec, this.contents);
192 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698