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 | |
5 import 'dart:io'; | |
6 import 'package:http_server/http_server.dart' as http_server; | |
7 import 'package:route/server.dart'; | |
8 import 'package:path/path.dart'; | |
9 import 'package:http_server/http_server.dart'; | |
10 | |
11 /* | |
12 * This program serves a visualization of a JavaScript source map file generated | |
13 * by dart2js or pub. | |
14 * | |
15 * Usage: dart source_map_viewer.dart <path to map file>. | |
16 * | |
17 * The default system browser is started and pointed to the viewer if | |
18 * available. | |
19 */ | |
20 | |
21 Directory rootDir = null; | |
22 String sourceMapFile; | |
23 | |
24 void main(List<String> args) { | |
25 if (args.length != 1) { | |
26 print('One argument expected; the source map file.'); | |
27 exit(-1); | |
28 return; | |
29 } | |
30 | |
31 File mapFile = new File(args[0]); | |
32 if (!mapFile.existsSync()) { | |
33 print('Map file not found at ${args[0]}'); | |
34 exit(-2); | |
35 return; | |
36 } | |
37 | |
38 sourceMapFile = basename(mapFile.path); | |
39 rootDir = mapFile.parent; | |
40 startServer(rootDir); | |
41 } | |
42 | |
43 // Sends the content of the file requested in the path parameter. | |
44 void handleFile(HttpRequest request) { | |
45 String path = request.uri.queryParameters["path"]; | |
46 if (path == null) { | |
47 request.response.close(); | |
48 return; | |
49 } | |
50 | |
51 path = rootDir.path + '/' + path; | |
52 new File(Uri.parse(path).toFilePath()).openRead() | |
53 .pipe(request.response).catchError((e) { | |
54 print("Error: $e"); | |
55 request.response.close(); | |
56 }); | |
57 } | |
58 | |
59 // Sends back the name of the source map file. | |
60 void handleSourceMapFile(HttpRequest request) { | |
61 request.response.write(sourceMapFile); | |
62 request.response.close(); | |
63 } | |
64 | |
65 // Starts an HttpServer rooted in [dir] with two special routes, /file and /map. | |
66 // | |
67 // /file takes a parameter [path] and serves the content of the specified file | |
68 // in path relative to [dir] | |
69 // | |
70 // /map serves the name of the map file such that its content can be requested | |
71 // with a /file request as above. | |
72 void startServer(Directory dir) { | |
73 rootDir = dir; | |
74 // Use port 0 to get an ephemeral port. | |
75 int port = 0; | |
76 HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((server) { | |
77 port = server.port; | |
78 print("Source mapping server is running on " | |
79 "'http://${server.address.address}:$port/'"); | |
80 Router router = new Router(server) | |
81 ..serve('/file').listen(handleFile) | |
82 ..serve('/map').listen(handleSourceMapFile); | |
83 | |
84 // Set up default handler. This will serve files from our 'build' | |
85 // directory. Disable jail root, as packages are local symlinks. | |
86 VirtualDirectory virDir = new http_server.VirtualDirectory(dir.path) | |
87 ..jailRoot = false | |
88 ..allowDirectoryListing = true; | |
89 | |
90 virDir.directoryHandler = (dir, request) { | |
91 // Redirect directory requests to index.html files. | |
92 Uri indexUri = new Uri.file(dir.path).resolve('display.html'); | |
93 virDir.serveFile(new File(indexUri.toFilePath()), request); | |
94 }; | |
95 | |
96 // Add an error page handler. | |
97 virDir.errorPageHandler = (HttpRequest request) { | |
98 print("Resource not found: ${request.uri.path}"); | |
99 request.response.statusCode = HttpStatus.NOT_FOUND; | |
100 request.response.close(); | |
101 }; | |
102 | |
103 // Serve everything not routed elsewhere through the virtual directory. | |
104 virDir.serve(router.defaultStream); | |
105 | |
106 // Start the system' default browser | |
107 startBrowser('http://${server.address.address}:$port/'); | |
108 }); | |
109 } | |
110 | |
111 startBrowser(String url) { | |
112 String command; | |
113 if (Platform.isWindows) { | |
114 command = 'cmd.exe /C start'; | |
ricow1
2014/05/15 12:04:55
just out of curiosity, why do we need to do cmd.ex
Johnni Winther
2014/05/15 13:07:44
'start' is not a program but a command in cmd.exe.
| |
115 } else if (Platform.isMacOS) { | |
116 command = 'open'; | |
117 } else { | |
118 String xdg = '/usr/bin/xdg-open'; | |
ricow1
2014/05/15 12:04:55
I don't really mind if you do this, but you could
| |
119 if (new File(xdg).existsSync()) { | |
120 command = xdg; | |
121 } else { | |
122 command = '/usr/bin/google-chrome'; | |
123 } | |
124 } | |
125 | |
126 print('Starting browser: ${command} ${url}'); | |
127 Process.run(command, ['$url']).then((ProcessResult result) { | |
128 if (result.exitCode != 0) { | |
129 print(result.stderr); | |
130 } | |
131 }); | |
132 } | |
OLD | NEW |