Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library dart2js_incremental.server; | 5 library dart2js_incremental.server; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'dart:async' show | 9 import 'dart:async' show |
| 10 Future, | 10 Future, |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 </head> | 152 </head> |
| 153 <body> | 153 <body> |
| 154 <h1>$title</h1> | 154 <h1>$title</h1> |
| 155 <p style='white-space:pre'>$text</p> | 155 <p style='white-space:pre'>$text</p> |
| 156 </body> | 156 </body> |
| 157 </html> | 157 </html> |
| 158 """; | 158 """; |
| 159 } | 159 } |
| 160 } | 160 } |
| 161 | 161 |
| 162 class Options { | |
|
kasperl
2015/01/19 09:37:27
Consider having this in a separate part or library
ahe
2015/01/19 10:23:08
Done.
| |
| 163 final List<String> arguments; | |
| 164 final Uri packageRoot; | |
| 165 final String host; | |
| 166 final int port; | |
| 167 | |
| 168 Options({this.arguments, this.packageRoot, this.host, this.port}); | |
| 169 | |
| 170 static String extractArgument(String option, String short, {String long}) { | |
| 171 if (option.startsWith(short)) { | |
| 172 return option.substring(short.length); | |
| 173 } | |
| 174 if (long != null && option.startsWith(long)) { | |
| 175 return option.substring(long.length); | |
| 176 } | |
| 177 return null; | |
| 178 } | |
| 179 | |
| 180 static Options parse(List<String> commandLine) { | |
| 181 Iterator<String> iterator = commandLine.iterator; | |
| 182 List<String> arguments = <String>[]; | |
| 183 Uri packageRoot; | |
| 184 String host = "127.0.0.1"; | |
| 185 int port = 0; | |
| 186 bool showHelp = false; | |
| 187 List<String> unknownOptions = <String>[]; | |
| 188 | |
| 189 LOOP: while (iterator.moveNext()) { | |
| 190 String option = iterator.current; | |
| 191 switch (option) { | |
| 192 case "-p": | |
| 193 iterator.moveNext(); | |
| 194 packageRoot = Uri.base.resolve(iterator.current); | |
| 195 continue; | |
| 196 | |
| 197 case "-h": | |
| 198 iterator.moveNext(); | |
| 199 host = iterator.current; | |
| 200 continue; | |
| 201 | |
| 202 case "-n": | |
| 203 iterator.moveNext(); | |
| 204 port = int.parse(iterator.current); | |
| 205 continue; | |
| 206 | |
| 207 case "--help": | |
| 208 showHelp = true; | |
| 209 continue; | |
| 210 | |
| 211 case "--": | |
| 212 break LOOP; | |
| 213 | |
| 214 default: | |
| 215 String argument; | |
| 216 | |
| 217 argument = extractArgument(option, "-p", long: "--package-root"); | |
| 218 if (argument != null) { | |
| 219 packageRoot = Uri.base.resolve(argument); | |
| 220 continue; | |
| 221 } | |
| 222 | |
| 223 argument = extractArgument(option, "-h", long: "--host"); | |
| 224 if (argument != null) { | |
| 225 host = argument; | |
| 226 continue; | |
| 227 } | |
| 228 | |
| 229 argument = extractArgument(option, "-n", long: "--port"); | |
| 230 if (argument != null) { | |
| 231 port = int.parse(option); | |
| 232 continue; | |
| 233 } | |
| 234 | |
| 235 if (option.startsWith("-")) { | |
| 236 unknownOptions.add(option); | |
| 237 continue; | |
| 238 } | |
| 239 | |
| 240 arguments.add(option); | |
| 241 break; | |
| 242 } | |
| 243 } | |
| 244 if (showHelp) { | |
| 245 print(USAGE); | |
| 246 } | |
| 247 if (!unknownOptions.isEmpty) { | |
| 248 print(USAGE); | |
| 249 print("Unknown options: '${unknownOptions.join('\', \'')}'"); | |
| 250 return null; | |
| 251 } | |
| 252 while (iterator.moveNext()) { | |
| 253 arguments.add(iterator.current); | |
| 254 } | |
| 255 if (arguments.length > 1) { | |
| 256 print(USAGE); | |
| 257 print("Extra arguments: '${arguments.skip(1).join('\', \'')}'"); | |
| 258 return null; | |
| 259 } | |
| 260 if (packageRoot == null) { | |
| 261 packageRoot = Uri.base.resolve('packages/'); | |
| 262 } | |
| 263 return new Options( | |
| 264 arguments: arguments, packageRoot: packageRoot, host: host, port: port); | |
| 265 } | |
| 266 } | |
| 267 | |
| 162 main(List<String> arguments) { | 268 main(List<String> arguments) { |
| 163 if (arguments.length > 0) { | 269 Options options = Options.parse(arguments); |
| 164 Conversation.documentRoot = Uri.base.resolve(arguments[0]); | 270 if (options == null) { |
| 271 exit(1); | |
| 165 } | 272 } |
| 166 var host = '127.0.0.1'; | 273 if (!options.arguments.isEmpty) { |
| 167 if (arguments.length > 1) { | 274 Conversation.documentRoot = Uri.base.resolve(options.arguments.single); |
| 168 host = arguments[1]; | |
| 169 } | 275 } |
| 170 int port = 0; | 276 Conversation.packageRoot = options.packageRoot; |
| 171 if (arguments.length > 2) { | 277 String host = options.host; |
| 172 port = int.parse(arguments[2]); | 278 int port = options.port; |
| 173 } | |
| 174 if (arguments.length > 3) { | |
| 175 Conversation.packageRoot = Uri.base.resolve(arguments[4]); | |
| 176 } | |
| 177 HttpServer.bind(host, port).then((HttpServer server) { | 279 HttpServer.bind(host, port).then((HttpServer server) { |
| 178 print('HTTP server started on http://$host:${server.port}/'); | 280 print('HTTP server started on http://$host:${server.port}/'); |
| 179 server.listen(Conversation.onRequest, onError: Conversation.onError); | 281 server.listen(Conversation.onRequest, onError: Conversation.onError); |
| 180 }).catchError((e) { | 282 }).catchError((e) { |
| 181 print("HttpServer.bind error: $e"); | 283 print("HttpServer.bind error: $e"); |
| 182 exit(1); | 284 exit(1); |
| 183 }); | 285 }); |
| 184 } | 286 } |
| 287 | |
| 288 const String USAGE = """ | |
| 289 Usage: server.dart [options] [--] documentroot | |
| 290 | |
| 291 Development web server which serves files relative to [documentroot]. If a file | |
| 292 is missing, and the requested file name ends with '.dart.js', the server will | |
| 293 look for a file with the same name save '.js', compile it to JavaScript, and | |
| 294 serve that file instead. | |
|
ahe
2015/01/19 09:32:22
Not implemented yet.
| |
| 295 | |
| 296 Supported options: | |
| 297 | |
| 298 -p<path>, --package-root=<path> | |
| 299 Where to find packages, that is, "package:..." imports. | |
| 300 | |
| 301 -h<name>, --host=<name> | |
| 302 Host name to bind the web server to (default 127.0.0.1). | |
| 303 | |
| 304 -n<port>, --port=<port> | |
| 305 Port number to bind the web server to. | |
| 306 | |
| 307 --help | |
| 308 Show this message. | |
| 309 """; | |
| OLD | NEW |