Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 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.
| |
| 5 import 'package:http_server/http_server.dart' as http_server; | |
| 6 import 'package:route/server.dart'; | |
| 7 import 'package:path/path.dart'; | |
| 8 import 'package:http_server/http_server.dart'; | |
| 9 | |
| 10 /* | |
| 11 * This program serves a visualization of a JavaScript source map file generated | |
| 12 * by dart2js or pub. | |
| 13 * | |
| 14 * Usage: dart source_map_viewer.dart <path to map file>. | |
| 15 * | |
| 16 * The default system browser is started and pointed to the viewer if | |
| 17 * available. | |
| 18 */ | |
| 19 | |
| 20 Directory rootDir = null; | |
| 21 String sourceMapFile; | |
| 22 | |
| 23 void main(List<String> args) { | |
| 24 if (args.length != 1) { | |
| 25 print('One argument expected; the source map file.'); | |
| 26 return; | |
| 27 } | |
| 28 | |
| 29 File mapFile = new File(args[0]); | |
| 30 if (!mapFile.existsSync()) { | |
| 31 print('Map file not found at ${args[0]}'); | |
| 32 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.
| |
| 33 } | |
| 34 | |
| 35 sourceMapFile = basename(mapFile.path); | |
| 36 rootDir = mapFile.parent; | |
| 37 startServer(rootDir); | |
| 38 } | |
| 39 | |
| 40 // Sends the content of the file requested in the path parameter. | |
| 41 void handleFile(HttpRequest request) { | |
| 42 String path = request.uri.queryParameters["path"]; | |
| 43 if (path == null) { | |
| 44 request.response.close(); | |
| 45 return; | |
| 46 } | |
| 47 | |
| 48 path = rootDir.path + '/' + path; | |
| 49 new File(Uri.parse(path).toFilePath()).openRead() | |
| 50 .pipe(request.response).catchError((e) { | |
| 51 print("Error: $e"); | |
|
ricow1
2014/05/14 12:28:08
I would indent this 2
zarah
2014/05/14 20:51:03
Done.
| |
| 52 request.response.close(); | |
| 53 }); | |
| 54 } | |
| 55 | |
| 56 // Sends back the name of the source map file. | |
| 57 void handleSourceMapFile(HttpRequest request) { | |
| 58 request.response.write(sourceMapFile); | |
| 59 request.response.close(); | |
| 60 } | |
| 61 | |
| 62 // Starts an HttpServer rooted in [dir] with two special routes, /file and /map. | |
| 63 // | |
| 64 // /file takes a parameter [path] and serves the content of the specified file | |
| 65 // in path relative to [dir] | |
| 66 // | |
| 67 // /map serves the name of the map file such that its content can be requested | |
| 68 // with a /file request as above. | |
| 69 void startServer(Directory dir) { | |
| 70 rootDir = dir; | |
| 71 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.
| |
| 72 HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((server) { | |
| 73 print("Source mapping server is running on " | |
| 74 "'http://${server.address.address}:$port/'"); | |
| 75 Router router = new Router(server) | |
| 76 ..serve('/file').listen(handleFile) | |
| 77 ..serve('/map').listen(handleSourceMapFile); | |
| 78 | |
| 79 // Set up default handler. This will serve files from our 'build' | |
| 80 // directory. Disable jail root, as packages are local symlinks. | |
| 81 VirtualDirectory virDir = new http_server.VirtualDirectory(dir.path) | |
| 82 ..jailRoot = false | |
| 83 ..allowDirectoryListing = true; | |
| 84 | |
| 85 virDir.directoryHandler = (dir, request) { | |
| 86 // Redirect directory requests to index.html files. | |
| 87 Uri indexUri = new Uri.file(dir.path).resolve('display.html'); | |
| 88 virDir.serveFile(new File(indexUri.toFilePath()), request); | |
| 89 }; | |
| 90 | |
| 91 // Add an error page handler. | |
| 92 virDir.errorPageHandler = (HttpRequest request) { | |
| 93 print("Resource not found: ${request.uri.path}"); | |
| 94 request.response.statusCode = HttpStatus.NOT_FOUND; | |
| 95 request.response.close(); | |
| 96 }; | |
| 97 | |
| 98 // Serve everything not routed elsewhere through the virtual directory. | |
| 99 virDir.serve(router.defaultStream); | |
| 100 | |
| 101 // Start the system' default browser | |
| 102 startBrowser('http://${server.address.address}:$port/'); | |
| 103 }); | |
| 104 } | |
| 105 | |
| 106 startBrowser(String url) { | |
| 107 String command; | |
| 108 if (Platform.isWindows) { | |
| 109 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.
| |
| 110 } else if (Platform.isMacOS) { | |
| 111 command = 'open'; | |
| 112 } else { | |
| 113 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.
| |
| 114 } | |
| 115 | |
| 116 print('Starting browser: ${command} ${url}'); | |
| 117 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.
| |
| 118 } | |
| OLD | NEW |