OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:io'; | 5 import 'dart:io'; |
6 import 'package:http_server/http_server.dart' as http_server; | 6 import 'package:http_server/http_server.dart' as http_server; |
7 import 'package:route/server.dart'; | 7 import 'package:route/server.dart'; |
8 import 'package:path/path.dart'; | 8 import 'package:path/path.dart'; |
9 import 'package:http_server/http_server.dart'; | 9 import 'package:http_server/http_server.dart'; |
10 | 10 |
11 /* | 11 /* |
12 * This program serves a visualization of a JavaScript source map file generated | 12 * This program serves a visualization of a JavaScript source map file generated |
13 * by dart2js or pub. | 13 * by dart2js or pub. |
14 * | 14 * |
15 * Usage: dart source_map_viewer.dart <path to map file>. | 15 * Usage: dart source_map_viewer.dart <path to map file>. |
16 * | 16 * |
17 * The default system browser is started and pointed to the viewer if | 17 * The default system browser is started and pointed to the viewer if |
18 * available. | 18 * available. |
19 */ | 19 */ |
20 | 20 |
21 Directory rootDir = null; | 21 Uri sourceMapFile; |
22 String sourceMapFile; | |
23 | 22 |
24 void main(List<String> args) { | 23 void main(List<String> args) { |
25 if (args.length != 1) { | 24 if (args.length != 1) { |
26 print('One argument expected; the source map file.'); | 25 print('One argument expected; the source map file.'); |
27 exit(-1); | 26 exit(-1); |
28 return; | 27 return; |
29 } | 28 } |
30 | 29 |
31 File mapFile = new File(args[0]); | 30 File mapFile = new File(args[0]); |
32 if (!mapFile.existsSync()) { | 31 if (!mapFile.existsSync()) { |
33 print('Map file not found at ${args[0]}'); | 32 print('Map file not found at ${args[0]}'); |
34 exit(-2); | 33 exit(-2); |
35 return; | 34 return; |
36 } | 35 } |
37 | 36 |
38 sourceMapFile = basename(mapFile.path); | 37 sourceMapFile = toUri(mapFile.path); |
39 rootDir = mapFile.parent; | 38 startServer(); |
40 startServer(rootDir); | |
41 } | 39 } |
42 | 40 |
43 // Sends the content of the file requested in the path parameter. | 41 // Sends the content of the file requested in the path parameter. |
44 void handleFile(HttpRequest request) { | 42 void handleFile(HttpRequest request) { |
45 String path = request.uri.queryParameters["path"]; | 43 String path = request.uri.queryParameters["path"]; |
46 if (path == null) { | 44 if (path == null) { |
47 request.response.close(); | 45 request.response.close(); |
48 return; | 46 return; |
49 } | 47 } |
50 | 48 |
51 path = rootDir.path + '/' + path; | 49 Uri uri = sourceMapFile.resolve(path); |
52 new File(Uri.parse(path).toFilePath()).openRead() | 50 new File.fromUri(uri).openRead().pipe(request.response).catchError((e) { |
53 .pipe(request.response).catchError((e) { | 51 print("Error: $e"); |
54 print("Error: $e"); | |
55 request.response.close(); | 52 request.response.close(); |
56 }); | 53 }); |
57 } | 54 } |
58 | 55 |
59 // Sends back the name of the source map file. | 56 // Sends back the name of the source map file. |
60 void handleSourceMapFile(HttpRequest request) { | 57 void handleSourceMapFile(HttpRequest request) { |
61 request.response.write(sourceMapFile); | 58 request.response.write(basename(sourceMapFile.path)); |
62 request.response.close(); | 59 request.response.close(); |
63 } | 60 } |
64 | 61 |
65 // Starts an HttpServer rooted in [dir] with two special routes, /file and /map. | 62 // Starts an HttpServer rooted in [dir] with two special routes, /file and /map. |
66 // | 63 // |
67 // /file takes a parameter [path] and serves the content of the specified file | 64 // /file takes a parameter [path] and serves the content of the specified file |
68 // in path relative to [dir] | 65 // in path relative to [dir] |
69 // | 66 // |
70 // /map serves the name of the map file such that its content can be requested | 67 // /map serves the name of the map file such that its content can be requested |
71 // with a /file request as above. | 68 // with a /file request as above. |
72 void startServer(Directory dir) { | 69 void startServer() { |
73 rootDir = dir; | 70 String root = fromUri(Platform.script.resolve('../build/web/')); |
| 71 Directory directory = new Directory(root); |
| 72 if (!directory.existsSync()) { |
| 73 print("Directory '$root' does not exist. " |
| 74 "Run 'pub build' to generate the output."); |
| 75 exit(-1); |
| 76 } |
| 77 |
74 // Use port 0 to get an ephemeral port. | 78 // Use port 0 to get an ephemeral port. |
75 int port = 0; | 79 int port = 0; |
76 HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((server) { | 80 HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((server) { |
77 port = server.port; | 81 port = server.port; |
78 print("Source mapping server is running on " | 82 print("Source mapping server is running on " |
79 "'http://${server.address.address}:$port/'"); | 83 "'http://${server.address.address}:$port/'"); |
80 Router router = new Router(server) | 84 Router router = new Router(server) |
81 ..serve('/file').listen(handleFile) | 85 ..serve('/file').listen(handleFile) |
82 ..serve('/map').listen(handleSourceMapFile); | 86 ..serve('/map').listen(handleSourceMapFile); |
83 | 87 |
84 // Set up default handler. This will serve files from our 'build' | 88 // Set up default handler. This will serve files from our 'build' |
85 // directory. Disable jail root, as packages are local symlinks. | 89 // directory. Disable jail root, as packages are local symlinks. |
86 VirtualDirectory virDir = new http_server.VirtualDirectory(dir.path) | 90 VirtualDirectory virDir = new http_server.VirtualDirectory(root) |
87 ..jailRoot = false | 91 ..jailRoot = false |
88 ..allowDirectoryListing = true; | 92 ..allowDirectoryListing = true; |
89 | 93 |
90 virDir.directoryHandler = (dir, request) { | 94 virDir.directoryHandler = (dir, request) { |
91 // Redirect directory requests to index.html files. | 95 // Redirect directory requests to index.html files. |
92 Uri indexUri = new Uri.file(dir.path).resolve('display.html'); | 96 Uri indexUri = new Uri.file(dir.path).resolve('display.html'); |
93 virDir.serveFile(new File(indexUri.toFilePath()), request); | 97 virDir.serveFile(new File(indexUri.toFilePath()), request); |
94 }; | 98 }; |
95 | 99 |
96 // Add an error page handler. | 100 // Add an error page handler. |
(...skipping 26 matching lines...) Expand all Loading... |
123 } | 127 } |
124 } | 128 } |
125 | 129 |
126 print('Starting browser: ${command} ${url}'); | 130 print('Starting browser: ${command} ${url}'); |
127 Process.run(command, ['$url']).then((ProcessResult result) { | 131 Process.run(command, ['$url']).then((ProcessResult result) { |
128 if (result.exitCode != 0) { | 132 if (result.exitCode != 0) { |
129 print(result.stderr); | 133 print(result.stderr); |
130 } | 134 } |
131 }); | 135 }); |
132 } | 136 } |
OLD | NEW |