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

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

Issue 1165473002: Start pulling pub from its own repo. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Code review changes Created 5 years, 6 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
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),
52 {bool replace: false}) {
53 if (_servedPackageDir == null) {
54 _builder = new PackageServerBuilder();
55 _servedApiPackageDir = d.dir('packages', []);
56 _servedPackageDir = d.dir('packages', []);
57 serve([
58 d.dir('api', [_servedApiPackageDir]),
59 _servedPackageDir
60 ]);
61
62 currentSchedule.onComplete.schedule(() {
63 _builder = null;
64 _servedApiPackageDir = null;
65 _servedPackageDir = null;
66 }, 'cleaning up served packages');
67 }
68
69 schedule(() {
70 if (replace) _builder = new PackageServerBuilder();
71 callback(_builder);
72 return _builder._await().then((resolvedPubspecs) {
73 _servedApiPackageDir.contents.clear();
74 _servedPackageDir.contents.clear();
75 _builder._packages.forEach((name, versions) {
76 _servedApiPackageDir.contents.addAll([
77 d.file('$name', JSON.encode({
78 'name': name,
79 'uploaders': ['nweiz@google.com'],
80 'versions': versions.map((version) =>
81 packageVersionApiMap(version.pubspec)).toList()
82 })),
83 d.dir(name, [
84 d.dir('versions', versions.map((version) {
85 return d.file(version.version.toString(), JSON.encode(
86 packageVersionApiMap(version.pubspec, full: true)));
87 }))
88 ])
89 ]);
90
91 _servedPackageDir.contents.add(d.dir(name, [
92 d.dir('versions', versions.map((version) =>
93 d.tar('${version.version}.tar.gz', version.contents)))
94 ]));
95 });
96 });
97 }, 'initializing the package server');
98 }
99
100 /// Like [servePackages], but instead creates an empty server with no packages
101 /// registered.
102 ///
103 /// This will always replace a previous server.
104 void serveNoPackages() => servePackages((_) {}, replace: true);
105
106 /// A builder for specifying which packages should be served by [servePackages].
107 class PackageServerBuilder {
108 /// A map from package names to a list of concrete packages to serve.
109 final _packages = new Map<String, List<_ServedPackage>>();
110
111 /// A group of futures from [serve] calls.
112 ///
113 /// This should be accessed by calling [_awair].
114 var _futures = new FutureGroup();
115
116 /// Specifies that a package named [name] with [version] should be served.
117 ///
118 /// If [deps] is passed, it's used as the "dependencies" field of the pubspec.
119 /// If [pubspec] is passed, it's used as the rest of the pubspec. Either of
120 /// these may recursively contain Futures.
121 ///
122 /// If [contents] is passed, it's used as the contents of the package. By
123 /// default, a package just contains a dummy lib directory.
124 void serve(String name, String version, {Map deps, Map pubspec,
125 Iterable<d.Descriptor> contents}) {
126 _futures.add(Future.wait([
127 awaitObject(deps),
128 awaitObject(pubspec)
129 ]).then((pair) {
130 var resolvedDeps = pair.first;
131 var resolvedPubspec = pair.last;
132
133 var pubspecFields = {
134 "name": name,
135 "version": version
136 };
137 if (resolvedPubspec != null) pubspecFields.addAll(resolvedPubspec);
138 if (resolvedDeps != null) pubspecFields["dependencies"] = resolvedDeps;
139
140 if (contents == null) contents = [d.libDir(name, "$name $version")];
141 contents = [d.file("pubspec.yaml", yaml(pubspecFields))]
142 ..addAll(contents);
143
144 var packages = _packages.putIfAbsent(name, () => []);
145 packages.add(new _ServedPackage(pubspecFields, contents));
146 }));
147 }
148
149 /// Serves the versions of [package] and all its dependencies that are
150 /// currently checked into the Dart repository.
151 void serveRepoPackage(String package) {
152 _addPackage(name) {
153 if (_packages.containsKey(name)) return;
154 _packages[name] = [];
155
156 var root = packagePath(name);
157 var pubspec = new Map.from(loadYaml(
158 readTextFile(p.join(root, 'pubspec.yaml'))));
159
160 // Remove any SDK constraints since we don't have a valid SDK version
161 // while testing.
162 pubspec.remove('environment');
163
164 _packages[name].add(new _ServedPackage(pubspec, [
165 d.file('pubspec.yaml', yaml(pubspec)),
166 new d.DirectoryDescriptor.fromFilesystem('lib', p.join(root, 'lib'))
167 ]));
168
169 if (pubspec.containsKey('dependencies')) {
170 pubspec['dependencies'].keys.forEach(_addPackage);
171 }
172 }
173
174 _addPackage(package);
175 }
176
177 /// Returns a Future that completes once all the [serve] calls have been fully
178 /// processed.
179 Future _await() {
180 if (_futures.futures.isEmpty) return new Future.value();
181 return _futures.future.then((_) {
182 _futures = new FutureGroup();
183 });
184 }
185 }
186
187 /// A package that's intended to be served.
188 class _ServedPackage {
189 final Map pubspec;
190 final List<d.Descriptor> contents;
191
192 Version get version => new Version.parse(pubspec['version']);
193
194 _ServedPackage(this.pubspec, this.contents);
195 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698