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

Unified Diff: sdk/lib/_internal/pub/lib/src/barback.dart

Issue 24886002: Support dart2js in pub serve. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/pub/lib/src/barback.dart
diff --git a/sdk/lib/_internal/pub/lib/src/barback.dart b/sdk/lib/_internal/pub/lib/src/barback.dart
index 803ec64fb69a049146313b9d4a05aaa3017c63fe..b41c48ad381c9a05bf17e7fbef97f799b67d877b 100644
--- a/sdk/lib/_internal/pub/lib/src/barback.dart
+++ b/sdk/lib/_internal/pub/lib/src/barback.dart
@@ -7,7 +7,10 @@ library pub.barback;
import 'dart:async';
import 'package:barback/barback.dart';
+import 'package:path/path.dart' as path;
+import 'barback/dart_forwarding_transformer.dart';
+import 'barback/dart2js_transformer.dart';
import 'barback/load_all_transformers.dart';
import 'barback/pub_package_provider.dart';
import 'barback/server.dart';
@@ -54,6 +57,12 @@ class TransformerId {
Future<BarbackServer> createServer(String host, int port, PackageGraph graph) {
var provider = new PubPackageProvider(graph);
var barback = new Barback(provider);
+
+ var builtInTransformers = [
nweiz 2013/09/27 22:21:17 Add a TODO here to add a dart2dart transformer and
Bob Nystrom 2013/09/28 00:56:11 Done.
+ new Dart2JSTransformer(graph),
+ new DartForwardingTransformer()
+ ];
+
return BarbackServer.bind(host, port, barback, graph.entrypoint.root.name)
.then((server) {
watchSources(graph, barback);
@@ -75,7 +84,7 @@ Future<BarbackServer> createServer(String host, int port, PackageGraph graph) {
})
];
- loadAllTransformers(server, graph).then((_) {
+ loadAllTransformers(server, graph, builtInTransformers).then((_) {
if (!completer.isCompleted) completer.complete(server);
}).catchError((error) {
if (!completer.isCompleted) completer.completeError(error);
@@ -141,3 +150,56 @@ Uri idToPackageUri(AssetId id) {
return new Uri(scheme: 'package', path: id.path.replaceFirst('lib/', ''));
}
+
+/// Converts a [uri] served by pub serve into an [AssetId] that can be
nweiz 2013/09/27 22:21:17 "uri" -> "url", throughout this function (except t
Bob Nystrom 2013/09/28 00:56:11 Done.
+/// requested from barback.
+AssetId uriToId(String rootPackage, Uri uri) {
nweiz 2013/09/27 22:21:17 It feels like this should be a method on [BarbackS
Bob Nystrom 2013/09/28 00:56:11 Done.
+ var parts = path.url.split(uri.path);
+
+ // Strip the leading "/" from the URL.
+ parts.removeAt(0);
+
+ try {
+ var id = specialPathToId(parts);
+ if (id != null) return id;
+ } on FormatException catch(_) {
nweiz 2013/09/27 22:21:17 Style nit: "catch (_)"
Bob Nystrom 2013/09/28 00:56:11 Done.
+ // If we got here, we had a path like "/packages" which is a special
+ // directory, but not a valid path since it lacks a following package name.
+ return null;
nweiz 2013/09/27 22:21:17 It feels like throwing an error for an invalid URI
Bob Nystrom 2013/09/28 00:56:11 Done.
+ }
+
+ // Otherwise, it's a path in current package's web directory.
+ var relativePath = path.url.join("web", path.url.joinAll(parts));
+ return new AssetId(rootPackage, relativePath);
+}
+
+/// Converts [parts], a list of path components into an [AssetId] if [parts]
nweiz 2013/09/27 22:21:17 "components" -> "components,"
Bob Nystrom 2013/09/28 00:56:11 Done.
+/// is from a path containing "packages" or "assets".
+///
+/// If the path doesn't contain one of those special directories, returns null.
+/// If it does contain a special directory, but lacks a following package name,
+/// throws a [FormatException].
+AssetId specialPathToId(List<String> parts) {
nweiz 2013/09/27 22:21:17 As a public API function of this library, it doesn
Bob Nystrom 2013/09/28 00:56:11 The problem is that this can be called with both U
nweiz 2013/09/30 17:33:34 That's easy to change by delaying the URI-to-path
Bob Nystrom 2013/10/01 19:08:55 Done.
+ for (var pair in [["packages", "lib"], ["assets", "asset"]]) {
+ var partName = pair[0];
+ var dirName = pair[1];
nweiz 2013/09/27 22:21:17 Style nit: I like using [Iterable.first] and [Iter
Bob Nystrom 2013/09/28 00:56:11 Done.
+
+ // Find the package name and the relative path in the package.
+ var index = parts.indexOf(partName);
+ if (index == -1) continue;
+
+ // If we got here, the path *did* contain the special directory, which
+ // means we should not interpret it as a regular path, even if it's
+ // missing the package name after it, which makes it invalid here.
nweiz 2013/09/27 22:21:17 Nit: "as a regular path. If it's missing the packa
Bob Nystrom 2013/09/28 00:56:11 Done.
+ if (index + 1 >= parts.length) {
+ throw new FormatException('Expected package named after "$partName".');
nweiz 2013/09/27 22:21:17 "named" -> "name" I would include more context he
Bob Nystrom 2013/09/28 00:56:11 Done.
+ }
+
+ var package = parts[index + 1];
+ var assetPath = path.url.join(dirName,
+ path.url.joinAll(parts.skip(index + 2)));
+ return new AssetId(package, assetPath);
+ }
+
+ return null;
+}

Powered by Google App Engine
This is Rietveld 408576698