OLD | NEW |
| 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 |
1 library serve_packages; | 5 library serve_packages; |
| 6 |
2 import 'dart:async'; | 7 import 'dart:async'; |
3 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 |
4 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
5 import 'package:pub_semver/pub_semver.dart'; | 11 import 'package:pub_semver/pub_semver.dart'; |
6 import 'package:scheduled_test/scheduled_test.dart'; | 12 import 'package:scheduled_test/scheduled_test.dart'; |
7 import 'package:yaml/yaml.dart'; | 13 import 'package:yaml/yaml.dart'; |
| 14 |
8 import '../lib/src/io.dart'; | 15 import '../lib/src/io.dart'; |
9 import '../lib/src/utils.dart'; | 16 import '../lib/src/utils.dart'; |
10 import 'descriptor.dart' as d; | 17 import 'descriptor.dart' as d; |
11 import 'test_pub.dart'; | 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. |
12 d.DirectoryDescriptor _servedApiPackageDir; | 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. |
13 d.DirectoryDescriptor _servedPackageDir; | 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. |
14 PackageServerBuilder _builder; | 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. |
15 void servePackages(void callback(PackageServerBuilder builder), {bool replace: | 51 void servePackages(void callback(PackageServerBuilder builder), {bool replace: |
16 false}) { | 52 false}) { |
17 if (_servedPackageDir == null) { | 53 if (_servedPackageDir == null) { |
18 _builder = new PackageServerBuilder(); | 54 _builder = new PackageServerBuilder(); |
19 _servedApiPackageDir = d.dir('packages', []); | 55 _servedApiPackageDir = d.dir('packages', []); |
20 _servedPackageDir = d.dir('packages', []); | 56 _servedPackageDir = d.dir('packages', []); |
21 serve([d.dir('api', [_servedApiPackageDir]), _servedPackageDir]); | 57 serve([d.dir('api', [_servedApiPackageDir]), _servedPackageDir]); |
| 58 |
22 currentSchedule.onComplete.schedule(() { | 59 currentSchedule.onComplete.schedule(() { |
23 _builder = null; | 60 _builder = null; |
24 _servedApiPackageDir = null; | 61 _servedApiPackageDir = null; |
25 _servedPackageDir = null; | 62 _servedPackageDir = null; |
26 }, 'cleaning up served packages'); | 63 }, 'cleaning up served packages'); |
27 } | 64 } |
| 65 |
28 schedule(() { | 66 schedule(() { |
29 if (replace) _builder = new PackageServerBuilder(); | 67 if (replace) _builder = new PackageServerBuilder(); |
30 callback(_builder); | 68 callback(_builder); |
31 return _builder._await().then((resolvedPubspecs) { | 69 return _builder._await().then((resolvedPubspecs) { |
32 _servedApiPackageDir.contents.clear(); | 70 _servedApiPackageDir.contents.clear(); |
33 _servedPackageDir.contents.clear(); | 71 _servedPackageDir.contents.clear(); |
34 _builder._packages.forEach((name, versions) { | 72 _builder._packages.forEach((name, versions) { |
35 _servedApiPackageDir.contents.addAll([d.file('$name', JSON.encode({ | 73 _servedApiPackageDir.contents.addAll([d.file('$name', JSON.encode({ |
36 'name': name, | 74 'name': name, |
37 'uploaders': ['nweiz@google.com'], | 75 'uploaders': ['nweiz@google.com'], |
38 'versions': versions.map( | 76 'versions': versions.map( |
39 (version) => packageVersionApiMap(version.pubspec)).toList() | 77 (version) => packageVersionApiMap(version.pubspec)).toList() |
40 })), d.dir(name, [d.dir('versions', versions.map((version) { | 78 })), d.dir(name, [d.dir('versions', versions.map((version) { |
41 return d.file( | 79 return d.file( |
42 version.version.toString(), | 80 version.version.toString(), |
43 JSON.encode(packageVersionApiMap(version.pubspec, full: true))
); | 81 JSON.encode(packageVersionApiMap(version.pubspec, full: true))
); |
44 }))])]); | 82 }))])]); |
| 83 |
45 _servedPackageDir.contents.add( | 84 _servedPackageDir.contents.add( |
46 d.dir( | 85 d.dir( |
47 name, | 86 name, |
48 [ | 87 [ |
49 d.dir( | 88 d.dir( |
50 'versions', | 89 'versions', |
51 versions.map( | 90 versions.map( |
52 (version) => d.tar('${version.version}.tar.gz', vers
ion.contents)))])); | 91 (version) => d.tar('${version.version}.tar.gz', vers
ion.contents)))])); |
53 }); | 92 }); |
54 }); | 93 }); |
55 }, 'initializing the package server'); | 94 }, 'initializing the package server'); |
56 } | 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. |
57 void serveNoPackages() => servePackages((_) {}, replace: true); | 101 void serveNoPackages() => servePackages((_) {}, replace: true); |
| 102 |
| 103 /// A builder for specifying which packages should be served by [servePackages]. |
58 class PackageServerBuilder { | 104 class PackageServerBuilder { |
| 105 /// A map from package names to a list of concrete packages to serve. |
59 final _packages = new Map<String, List<_ServedPackage>>(); | 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]. |
60 var _futures = new FutureGroup(); | 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. |
61 void serve(String name, String version, {Map deps, Map pubspec, | 121 void serve(String name, String version, {Map deps, Map pubspec, |
62 Iterable<d.Descriptor> contents}) { | 122 Iterable<d.Descriptor> contents}) { |
63 _futures.add( | 123 _futures.add( |
64 Future.wait([awaitObject(deps), awaitObject(pubspec)]).then((pair) { | 124 Future.wait([awaitObject(deps), awaitObject(pubspec)]).then((pair) { |
65 var resolvedDeps = pair.first; | 125 var resolvedDeps = pair.first; |
66 var resolvedPubspec = pair.last; | 126 var resolvedPubspec = pair.last; |
| 127 |
67 var pubspecFields = { | 128 var pubspecFields = { |
68 "name": name, | 129 "name": name, |
69 "version": version | 130 "version": version |
70 }; | 131 }; |
71 if (resolvedPubspec != null) pubspecFields.addAll(resolvedPubspec); | 132 if (resolvedPubspec != null) pubspecFields.addAll(resolvedPubspec); |
72 if (resolvedDeps != null) pubspecFields["dependencies"] = resolvedDeps; | 133 if (resolvedDeps != null) pubspecFields["dependencies"] = resolvedDeps; |
| 134 |
73 if (contents == null) contents = [d.libDir(name, "$name $version")]; | 135 if (contents == null) contents = [d.libDir(name, "$name $version")]; |
74 contents = | 136 contents = |
75 [d.file("pubspec.yaml", yaml(pubspecFields))]..addAll(contents); | 137 [d.file("pubspec.yaml", yaml(pubspecFields))]..addAll(contents); |
| 138 |
76 var packages = _packages.putIfAbsent(name, () => []); | 139 var packages = _packages.putIfAbsent(name, () => []); |
77 packages.add(new _ServedPackage(pubspecFields, contents)); | 140 packages.add(new _ServedPackage(pubspecFields, contents)); |
78 })); | 141 })); |
79 } | 142 } |
| 143 |
| 144 /// Serves the versions of [package] and all its dependencies that are |
| 145 /// currently checked into the Dart repository. |
80 void serveRepoPackage(String package) { | 146 void serveRepoPackage(String package) { |
81 _addPackage(name) { | 147 _addPackage(name) { |
82 if (_packages.containsKey(name)) return; | 148 if (_packages.containsKey(name)) return; |
83 _packages[name] = []; | 149 _packages[name] = []; |
| 150 |
84 var pubspec = new Map.from( | 151 var pubspec = new Map.from( |
85 loadYaml(readTextFile(p.join(repoRoot, 'pkg', name, 'pubspec.yaml'))))
; | 152 loadYaml(readTextFile(p.join(repoRoot, 'pkg', name, 'pubspec.yaml'))))
; |
| 153 |
| 154 // Remove any SDK constraints since we don't have a valid SDK version |
| 155 // while testing. |
86 pubspec.remove('environment'); | 156 pubspec.remove('environment'); |
| 157 |
87 _packages[name].add( | 158 _packages[name].add( |
88 new _ServedPackage( | 159 new _ServedPackage( |
89 pubspec, | 160 pubspec, |
90 [ | 161 [ |
91 d.file('pubspec.yaml', yaml(pubspec)), | 162 d.file('pubspec.yaml', yaml(pubspec)), |
92 new d.DirectoryDescriptor.fromFilesystem( | 163 new d.DirectoryDescriptor.fromFilesystem( |
93 'lib', | 164 'lib', |
94 p.join(repoRoot, 'pkg', name, 'lib'))])); | 165 p.join(repoRoot, 'pkg', name, 'lib'))])); |
| 166 |
95 if (pubspec.containsKey('dependencies')) { | 167 if (pubspec.containsKey('dependencies')) { |
96 pubspec['dependencies'].keys.forEach(_addPackage); | 168 pubspec['dependencies'].keys.forEach(_addPackage); |
97 } | 169 } |
98 } | 170 } |
| 171 |
99 _addPackage(package); | 172 _addPackage(package); |
100 } | 173 } |
| 174 |
| 175 /// Returns a Future that completes once all the [serve] calls have been fully |
| 176 /// processed. |
101 Future _await() { | 177 Future _await() { |
102 if (_futures.futures.isEmpty) return new Future.value(); | 178 if (_futures.futures.isEmpty) return new Future.value(); |
103 return _futures.future.then((_) { | 179 return _futures.future.then((_) { |
104 _futures = new FutureGroup(); | 180 _futures = new FutureGroup(); |
105 }); | 181 }); |
106 } | 182 } |
107 } | 183 } |
| 184 |
| 185 /// A package that's intended to be served. |
108 class _ServedPackage { | 186 class _ServedPackage { |
109 final Map pubspec; | 187 final Map pubspec; |
110 final List<d.Descriptor> contents; | 188 final List<d.Descriptor> contents; |
| 189 |
111 Version get version => new Version.parse(pubspec['version']); | 190 Version get version => new Version.parse(pubspec['version']); |
| 191 |
112 _ServedPackage(this.pubspec, this.contents); | 192 _ServedPackage(this.pubspec, this.contents); |
113 } | 193 } |
OLD | NEW |