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

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

Issue 203413004: Provide URLs for assets from lib/ and asset/ in pub's websocket API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fix some small bugs Created 6 years, 9 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
« no previous file with comments | « sdk/lib/_internal/pub/lib/src/barback/server.dart ('k') | sdk/lib/_internal/pub/lib/src/utils.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/pub/lib/src/barback/web_socket_api.dart
diff --git a/sdk/lib/_internal/pub/lib/src/barback/web_socket_api.dart b/sdk/lib/_internal/pub/lib/src/barback/web_socket_api.dart
index fe3c1b0e02029cfcb11c9afb05caaed3961213c1..0eee8e7d28b28d381ab72a52fc3a9d04a6f956e7 100644
--- a/sdk/lib/_internal/pub/lib/src/barback/web_socket_api.dart
+++ b/sdk/lib/_internal/pub/lib/src/barback/web_socket_api.dart
@@ -8,6 +8,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
+import 'package:barback/barback.dart';
import 'package:path/path.dart' as path;
import 'package:stack_trace/stack_trace.dart';
@@ -212,12 +213,12 @@ class WebSocketApi {
return result;
}
- /// Given an asset ID in the root package, returns the URLs served by pub
- /// that can be used to access that asset.
+ /// Given a path on the filesystem, returns the URLs served by pub that can be
+ /// used to access asset found at that path.
///
/// The command name is "pathToUrls" and it takes a "path" key (a native OS
- /// path relative to the root directory of the entrypoint package) for the
- /// path being mapped:
+ /// path which may be absolute or relative to the root directory of the
+ /// entrypoint package) for the path being mapped:
///
/// {
/// "command": "pathToUrls",
@@ -231,6 +232,26 @@ class WebSocketApi {
/// "urls": ["http://localhost:8080/index.html"]
/// }
///
+ /// The "path" key may refer to a path in another package, either by referring
+ /// to its location within the top-level "packages" directory or by referring
+ /// to its location on disk. Only the "lib" and "asset" directories are
+ /// visible in other packages:
+ ///
+ /// {
+ /// "command": "assetIdToUrl",
+ /// "path": "packages/http/http.dart"
+ /// }
+ ///
+ /// Assets in the "lib" and "asset" directories will usually have one URL for
+ /// each server:
+ ///
+ /// {
+ /// "urls": [
+ /// "http://localhost:8080/packages/http/http.dart",
+ /// "http://localhost:8081/packages/http/http.dart"
+ /// ]
+ /// }
+ ///
/// An optional "line" key may be provided whose value must be an integer. If
/// given, the result will also include a "line" key that maps the line in
/// the source file to the corresponding output line in the resulting asset
@@ -245,32 +266,19 @@ class WebSocketApi {
/// If the asset is not in a directory being served by pub, returns an error:
///
/// example/index.html -> NOT_SERVED error
- ///
- /// This cannot currently be used to access assets in other packages aside
- /// from the root. Nor can it be used to access assets in the root package's
- /// "lib" or "asset" directories.
- ///
- /// lib/myapp.dart -> BAD_ARGUMENT error
Map _pathToUrls(Map command) {
- // TODO(rnystrom): Support assets in other packages. See #17146.
- var assetPath = _validateRelativePath(command, "path");
+ var assetPath = _validateString(command, "path");
var line = _validateOptionalInt(command, "line");
- // Find all of the servers whose root directories contain the asset and
- // generate appropriate URLs for each.
- var urls = _environment.servers
- .where((server) => path.isWithin(server.rootDirectory, assetPath))
- .map((server) {
- var relativePath = path.relative(assetPath, from: server.rootDirectory);
- return "${server.url}/${path.toUri(relativePath)}";
- }).toList();
-
+ var urls = _environment.getUrlsForAssetPath(assetPath);
if (urls.isEmpty) {
+ var name = '"$assetPath"';
+ if (command['package'] != null) name += ' in package "$package"';
throw new _WebSocketException(_ErrorCode.NOT_SERVED,
- 'Asset path "$assetPath" is not currently being served.');
+ 'Asset path $name is not currently being served.');
}
- var result = {"urls": urls};
+ var result = {"urls": urls.map((url) => url.toString()).toList()};
// Map the line.
// TODO(rnystrom): Right now, source maps are not supported and it just
@@ -305,7 +313,7 @@ class WebSocketApi {
var rootDirectory = _validateRelativePath(command, "path");
return _environment.serveDirectory(rootDirectory).then((server) {
return {
- "url": server.url
+ "url": server.url.toString()
};
});
}
@@ -314,14 +322,15 @@ class WebSocketApi {
///
/// Returns the string if found, or throws a [_WebSocketException] if
/// validation failed.
- String _validateString(Map command, String key) {
- if (!command.containsKey(key)) {
+ String _validateString(Map command, String key, {bool optional: false}) {
+ if (!optional && !command.containsKey(key)) {
throw new _WebSocketException(_ErrorCode.BAD_ARGUMENT,
'Missing "$key" argument.');
}
var field = command[key];
if (field is String) return field;
+ if (field == null && optional) return null;
throw new _WebSocketException(_ErrorCode.BAD_ARGUMENT,
'"$key" must be a string. Got ${JSON.encode(field)}.');
« no previous file with comments | « sdk/lib/_internal/pub/lib/src/barback/server.dart ('k') | sdk/lib/_internal/pub/lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698