OLD | NEW |
(Empty) | |
| 1 library serve_packages; |
| 2 import 'dart:async'; |
| 3 import 'dart:convert'; |
| 4 import 'package:path/path.dart' as p; |
| 5 import 'package:scheduled_test/scheduled_test.dart'; |
| 6 import 'package:yaml/yaml.dart'; |
| 7 import '../lib/src/io.dart'; |
| 8 import '../lib/src/utils.dart'; |
| 9 import '../lib/src/version.dart'; |
| 10 import 'descriptor.dart' as d; |
| 11 import 'test_pub.dart'; |
| 12 d.DirectoryDescriptor _servedApiPackageDir; |
| 13 d.DirectoryDescriptor _servedPackageDir; |
| 14 PackageServerBuilder _builder; |
| 15 void servePackages(void callback(PackageServerBuilder builder), {bool replace: |
| 16 false}) { |
| 17 if (_servedPackageDir == null) { |
| 18 _builder = new PackageServerBuilder(); |
| 19 _servedApiPackageDir = d.dir('packages', []); |
| 20 _servedPackageDir = d.dir('packages', []); |
| 21 serve([d.dir('api', [_servedApiPackageDir]), _servedPackageDir]); |
| 22 currentSchedule.onComplete.schedule(() { |
| 23 _builder = null; |
| 24 _servedApiPackageDir = null; |
| 25 _servedPackageDir = null; |
| 26 }, 'cleaning up served packages'); |
| 27 } |
| 28 schedule(() { |
| 29 if (replace) _builder = new PackageServerBuilder(); |
| 30 callback(_builder); |
| 31 return _builder._await().then((resolvedPubspecs) { |
| 32 _servedApiPackageDir.contents.clear(); |
| 33 _servedPackageDir.contents.clear(); |
| 34 _builder._packages.forEach((name, versions) { |
| 35 _servedApiPackageDir.contents.addAll([d.file('$name', JSON.encode({ |
| 36 'name': name, |
| 37 'uploaders': ['nweiz@google.com'], |
| 38 'versions': versions.map( |
| 39 (version) => packageVersionApiMap(version.pubspec)).toList() |
| 40 })), d.dir(name, [d.dir('versions', versions.map((version) { |
| 41 return d.file( |
| 42 version.version.toString(), |
| 43 JSON.encode(packageVersionApiMap(version.pubspec, full: true))
); |
| 44 }))])]); |
| 45 _servedPackageDir.contents.add( |
| 46 d.dir( |
| 47 name, |
| 48 [ |
| 49 d.dir( |
| 50 'versions', |
| 51 versions.map( |
| 52 (version) => d.tar('${version.version}.tar.gz', vers
ion.contents)))])); |
| 53 }); |
| 54 }); |
| 55 }, 'initializing the package server'); |
| 56 } |
| 57 void serveNoPackages() => servePackages((_) {}, replace: true); |
| 58 class PackageServerBuilder { |
| 59 final _packages = new Map<String, List<_ServedPackage>>(); |
| 60 var _futures = new FutureGroup(); |
| 61 void serve(String name, String version, {Map deps, Map pubspec, |
| 62 Iterable<d.Descriptor> contents}) { |
| 63 _futures.add( |
| 64 Future.wait([awaitObject(deps), awaitObject(pubspec)]).then((pair) { |
| 65 var resolvedDeps = pair.first; |
| 66 var resolvedPubspec = pair.last; |
| 67 var pubspecFields = { |
| 68 "name": name, |
| 69 "version": version |
| 70 }; |
| 71 if (resolvedPubspec != null) pubspecFields.addAll(resolvedPubspec); |
| 72 if (resolvedDeps != null) pubspecFields["dependencies"] = resolvedDeps; |
| 73 if (contents == null) contents = [d.libDir(name, "$name $version")]; |
| 74 contents = |
| 75 [d.file("pubspec.yaml", yaml(pubspecFields))]..addAll(contents); |
| 76 var packages = _packages.putIfAbsent(name, () => []); |
| 77 packages.add(new _ServedPackage(pubspecFields, contents)); |
| 78 })); |
| 79 } |
| 80 void serveRepoPackage(String package) { |
| 81 _addPackage(name) { |
| 82 if (_packages.containsKey(name)) return; |
| 83 _packages[name] = []; |
| 84 var pubspec = new Map.from( |
| 85 loadYaml(readTextFile(p.join(repoRoot, 'pkg', name, 'pubspec.yaml'))))
; |
| 86 pubspec.remove('environment'); |
| 87 _packages[name].add( |
| 88 new _ServedPackage( |
| 89 pubspec, |
| 90 [ |
| 91 d.file('pubspec.yaml', yaml(pubspec)), |
| 92 new d.DirectoryDescriptor.fromFilesystem( |
| 93 'lib', |
| 94 p.join(repoRoot, 'pkg', name, 'lib'))])); |
| 95 if (pubspec.containsKey('dependencies')) { |
| 96 pubspec['dependencies'].keys.forEach(_addPackage); |
| 97 } |
| 98 } |
| 99 _addPackage(package); |
| 100 } |
| 101 Future _await() { |
| 102 if (_futures.futures.isEmpty) return new Future.value(); |
| 103 return _futures.future.then((_) { |
| 104 _futures = new FutureGroup(); |
| 105 }); |
| 106 } |
| 107 } |
| 108 class _ServedPackage { |
| 109 final Map pubspec; |
| 110 final List<d.Descriptor> contents; |
| 111 Version get version => new Version.parse(pubspec['version']); |
| 112 _ServedPackage(this.pubspec, this.contents); |
| 113 } |
OLD | NEW |