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

Unified Diff: tools/dart2js/sourceMapViewer/bin/source_map_viewer.dart

Issue 280513002: Add tool for viewing source maps. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 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: tools/dart2js/sourceMapViewer/bin/source_map_viewer.dart
diff --git a/tools/dart2js/sourceMapViewer/bin/source_map_viewer.dart b/tools/dart2js/sourceMapViewer/bin/source_map_viewer.dart
new file mode 100644
index 0000000000000000000000000000000000000000..8d82a082475d76dbbcd3005f34e992a963656295
--- /dev/null
+++ b/tools/dart2js/sourceMapViewer/bin/source_map_viewer.dart
@@ -0,0 +1,118 @@
+// Copyright (c) 2014, 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.
+import 'dart:io';
Johnni Winther 2014/05/14 12:00:38 Add blank line before import. Maybe add library ta
zarah 2014/05/14 20:51:03 Done.
+import 'package:http_server/http_server.dart' as http_server;
+import 'package:route/server.dart';
+import 'package:path/path.dart';
+import 'package:http_server/http_server.dart';
+
+/*
+ * This program serves a visualization of a JavaScript source map file generated
+ * by dart2js or pub.
+ *
+ * Usage: dart source_map_viewer.dart <path to map file>.
+ *
+ * The default system browser is started and pointed to the viewer if
+ * available.
+ */
+
+Directory rootDir = null;
+String sourceMapFile;
+
+void main(List<String> args) {
+ if (args.length != 1) {
+ print('One argument expected; the source map file.');
+ return;
+ }
+
+ File mapFile = new File(args[0]);
+ if (!mapFile.existsSync()) {
+ print('Map file not found at ${args[0]}');
+ return;
ricow1 2014/05/14 12:28:08 I would set the exitCode to non zero here - it is
zarah 2014/05/14 20:51:03 Done.
+ }
+
+ sourceMapFile = basename(mapFile.path);
+ rootDir = mapFile.parent;
+ startServer(rootDir);
+}
+
+// Sends the content of the file requested in the path parameter.
+void handleFile(HttpRequest request) {
+ String path = request.uri.queryParameters["path"];
+ if (path == null) {
+ request.response.close();
+ return;
+ }
+
+ path = rootDir.path + '/' + path;
+ new File(Uri.parse(path).toFilePath()).openRead()
+ .pipe(request.response).catchError((e) {
+ print("Error: $e");
ricow1 2014/05/14 12:28:08 I would indent this 2
zarah 2014/05/14 20:51:03 Done.
+ request.response.close();
+ });
+}
+
+// Sends back the name of the source map file.
+void handleSourceMapFile(HttpRequest request) {
+ request.response.write(sourceMapFile);
+ request.response.close();
+}
+
+// Starts an HttpServer rooted in [dir] with two special routes, /file and /map.
+//
+// /file takes a parameter [path] and serves the content of the specified file
+// in path relative to [dir]
+//
+// /map serves the name of the map file such that its content can be requested
+// with a /file request as above.
+void startServer(Directory dir) {
+ rootDir = dir;
+ int port = 9223;
ricow1 2014/05/14 12:28:08 I would use an ephemeral port here, you can easily
zarah 2014/05/14 20:51:03 Done.
+ HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((server) {
+ print("Source mapping server is running on "
+ "'http://${server.address.address}:$port/'");
+ Router router = new Router(server)
+ ..serve('/file').listen(handleFile)
+ ..serve('/map').listen(handleSourceMapFile);
+
+ // Set up default handler. This will serve files from our 'build'
+ // directory. Disable jail root, as packages are local symlinks.
+ VirtualDirectory virDir = new http_server.VirtualDirectory(dir.path)
+ ..jailRoot = false
+ ..allowDirectoryListing = true;
+
+ virDir.directoryHandler = (dir, request) {
+ // Redirect directory requests to index.html files.
+ Uri indexUri = new Uri.file(dir.path).resolve('display.html');
+ virDir.serveFile(new File(indexUri.toFilePath()), request);
+ };
+
+ // Add an error page handler.
+ virDir.errorPageHandler = (HttpRequest request) {
+ print("Resource not found: ${request.uri.path}");
+ request.response.statusCode = HttpStatus.NOT_FOUND;
+ request.response.close();
+ };
+
+ // Serve everything not routed elsewhere through the virtual directory.
+ virDir.serve(router.defaultStream);
+
+ // Start the system' default browser
+ startBrowser('http://${server.address.address}:$port/');
+ });
+}
+
+startBrowser(String url) {
+ String command;
+ if (Platform.isWindows) {
+ command = 'start';
Johnni Winther 2014/05/14 12:00:38 Change 'start' to 'cmd.exe /C start'.
zarah 2014/05/14 20:51:03 Done.
+ } else if (Platform.isMacOS) {
+ command = 'open';
+ } else {
+ command = '/usr/bin/google-chrome';
ricow1 2014/05/14 12:28:08 that is not really the default browser, you could
zarah 2014/05/14 20:51:03 Done.
+ }
+
+ print('Starting browser: ${command} ${url}');
+ Process.run(command, ['$url']);
ricow1 2014/05/14 12:28:08 You may want to get the output and print an error
zarah 2014/05/14 20:51:03 Done.
+}

Powered by Google App Engine
This is Rietveld 408576698