| Index: sdk/lib/_internal/pub/lib/src/command_serve.dart
|
| diff --git a/sdk/lib/_internal/pub/lib/src/command_serve.dart b/sdk/lib/_internal/pub/lib/src/command_serve.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..aa18daafe88995b6a7eeb5ac2e71e526ddb09bdb
|
| --- /dev/null
|
| +++ b/sdk/lib/_internal/pub/lib/src/command_serve.dart
|
| @@ -0,0 +1,129 @@
|
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
| +// for details. All rights reserved. Use of this source code is governed by a
|
| +// BSD-style license that can be found in the LICENSE file.
|
| +
|
| +library command_serve;
|
| +
|
| +import 'dart:async';
|
| +import 'dart:io';
|
| +
|
| +import 'package:barback/barback.dart';
|
| +import 'package:barback/transformer.dart'; // TODO(rnystrom): Get rid of this?
|
| +import 'package:pathos/path.dart' as pathos;
|
| +
|
| +import 'command.dart';
|
| +import 'entrypoint.dart';
|
| +import 'log.dart' as log;
|
| +import 'utils.dart';
|
| +
|
| +final _green = getSpecial('\u001b[32m');
|
| +final _red = getSpecial('\u001b[31m');
|
| +final _none = getSpecial('\u001b[0m');
|
| +
|
| +/// Handles the `serve` pub command.
|
| +class ServeCommand extends PubCommand {
|
| + String get description => "TODO";
|
| + String get usage => 'pub serve';
|
| +
|
| + Barback _barback;
|
| +
|
| + Future onRun() {
|
| + return PubAssetProvider.create(entrypoint).then((provider) {
|
| + _barback = new Barback(entrypoint.root.name, provider);
|
| + return _barback.init();
|
| + }).then((_) {
|
| + HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 8080).then((server) {
|
| + print("Serving ${entrypoint.root.name} on http://localhost:${server.port}...");
|
| + server.listen((HttpRequest request) {
|
| + _barback.getByUri(request.uri).then((asset) {
|
| + print("$_green${request.method}$_none ${request.uri} -> $asset");
|
| + request.response.addStream(asset.read()).then((_) {
|
| + request.response.close();
|
| + });
|
| + }).catchError((error) {
|
| + if (error is! AssetNotFoundException) throw error;
|
| + print("$_red${request.method}$_none ${request.uri} -> $error");
|
| + request.response.statusCode = 404;
|
| + request.response.reasonPhrase = error.toString();
|
| + request.response.close();
|
| + return;
|
| + });
|
| + });
|
| + });
|
| +
|
| + // TODO(rnystrom): Hack! Return a future that never completes to leave
|
| + // pub running.
|
| + return new Completer().future;
|
| + }).whenComplete(() {
|
| + if (_barback == null) return null;
|
| + return _barback.shutdown();
|
| + });
|
| + }
|
| +}
|
| +
|
| +class PubAssetProvider extends AssetProvider {
|
| + final Entrypoint _entrypoint;
|
| +
|
| + final Map<String, String> _packageDirs;
|
| +
|
| + static Future<PubAssetProvider> create(Entrypoint entrypoint) {
|
| + var packageDirs = <String, String>{};
|
| + var futures = [];
|
| +
|
| + packageDirs[entrypoint.root.name] = entrypoint.root.dir;
|
| +
|
| + // TODO(rnystrom): Hackish. Cache package directories up front so we can
|
| + // have synchronous look-up for them later.
|
| + entrypoint.loadLockFile().packages.forEach((name, package) {
|
| + var source = entrypoint.cache.sources[package.source];
|
| + futures.add(source.getDirectory(package).then((packageDir) {
|
| + packageDirs[name] = packageDir;
|
| + }));
|
| + });
|
| +
|
| + return Future.wait(futures).then((_) {
|
| + return new PubAssetProvider._(entrypoint, packageDirs);
|
| + });
|
| + }
|
| +
|
| + PubAssetProvider._(this._entrypoint, this._packageDirs);
|
| +
|
| + Iterable<String> get packages => _packageDirs.keys;
|
| +
|
| + List<String> listFiles(String package, {String within}) {
|
| + var paths = <String>[];
|
| +
|
| + addFiles(String dirPath) {
|
| + var packageDir = _packageDirs[package];
|
| + var dir = new Directory(pathos.join(packageDir, dirPath));
|
| + if (!dir.existsSync()) return;
|
| + var entries = dir.listSync(recursive: true);
|
| + paths.addAll(entries
|
| + .where((entry) => entry is File)
|
| + .map((entry) => pathos.relative(entry.path, from: packageDir)));
|
| + }
|
| +
|
| + // If we aren't given a subdirectory, use all externally visible files.
|
| + if (within == null) {
|
| + // Expose the "asset" and "lib" directories.
|
| + addFiles("asset");
|
| + addFiles("lib");
|
| +
|
| + // The entrypoint's "web" directory is also visible.
|
| + if (package == _entrypoint.root.name) {
|
| + addFiles("web");
|
| + }
|
| + } else {
|
| + addFiles(within);
|
| + }
|
| +
|
| + return paths;
|
| + }
|
| +
|
| + Future<Asset> loadAsset(AssetId id) {
|
| + var path = pathos.join(_packageDirs[id.package], id.path);
|
| + var file = new File(pathos.normalize(path));
|
| + // TODO(rnystrom): Handle file not found.
|
| + return new Future.value(new Asset.fromFile(file));
|
| + }
|
| +}
|
|
|