OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 pub.command.serve; | 5 library pub.command.serve; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
| 9 import '../barback/dart_forwarding_transformer.dart'; |
| 10 import '../barback/dart2js_transformer.dart'; |
| 11 import '../barback/pub_package_provider.dart'; |
9 import '../barback.dart' as barback; | 12 import '../barback.dart' as barback; |
10 import '../barback/pub_package_provider.dart'; | |
11 import '../command.dart'; | 13 import '../command.dart'; |
12 import '../entrypoint.dart'; | 14 import '../entrypoint.dart'; |
13 import '../exit_codes.dart' as exit_codes; | 15 import '../exit_codes.dart' as exit_codes; |
14 import '../io.dart'; | 16 import '../io.dart'; |
15 import '../log.dart' as log; | 17 import '../log.dart' as log; |
16 import '../utils.dart'; | 18 import '../utils.dart'; |
17 | 19 |
18 final _green = getSpecial('\u001b[32m'); | 20 final _green = getSpecial('\u001b[32m'); |
19 final _red = getSpecial('\u001b[31m'); | 21 final _red = getSpecial('\u001b[31m'); |
20 final _none = getSpecial('\u001b[0m'); | 22 final _none = getSpecial('\u001b[0m'); |
21 final _arrow = getSpecial('\u2192', '=>'); | 23 final _arrow = getSpecial('\u2192', '=>'); |
22 | 24 |
23 /// Handles the `serve` pub command. | 25 /// Handles the `serve` pub command. |
24 class ServeCommand extends PubCommand { | 26 class ServeCommand extends PubCommand { |
25 String get description => "Run a local web development server."; | 27 String get description => "Run a local web development server."; |
26 String get usage => 'pub serve'; | 28 String get usage => 'pub serve'; |
27 | 29 |
28 PubPackageProvider _provider; | 30 PubPackageProvider _provider; |
29 | 31 |
30 String get hostname => commandOptions['hostname']; | 32 String get hostname => commandOptions['hostname']; |
31 | 33 |
| 34 /// `true` if Dart entrypoints should be compiled to JavaScript. |
| 35 bool get useDart2JS => commandOptions['dart2js']; |
| 36 |
32 ServeCommand() { | 37 ServeCommand() { |
33 commandParser.addOption('port', defaultsTo: '8080', | 38 commandParser.addOption('port', defaultsTo: '8080', |
34 help: 'The port to listen on.'); | 39 help: 'The port to listen on.'); |
35 | 40 |
36 // A hidden option for the tests to work around a bug in some of the OS X | 41 // A hidden option for the tests to work around a bug in some of the OS X |
37 // bots where "localhost" very rarely resolves to the IPv4 loopback address | 42 // bots where "localhost" very rarely resolves to the IPv4 loopback address |
38 // instead of IPv6 (or vice versa). The tests will always set this to | 43 // instead of IPv6 (or vice versa). The tests will always set this to |
39 // 127.0.0.1. | 44 // 127.0.0.1. |
40 commandParser.addOption('hostname', | 45 commandParser.addOption('hostname', |
41 defaultsTo: 'localhost', | 46 defaultsTo: 'localhost', |
42 hide: true); | 47 hide: true); |
| 48 |
| 49 commandParser.addFlag('dart2js', defaultsTo: true, |
| 50 help: 'Compile Dart to JavaScript.'); |
43 } | 51 } |
44 | 52 |
45 Future onRun() { | 53 Future onRun() { |
46 var port; | 54 var port; |
47 try { | 55 try { |
48 port = int.parse(commandOptions['port']); | 56 port = int.parse(commandOptions['port']); |
49 } on FormatException catch (_) { | 57 } on FormatException catch (_) { |
50 log.error('Could not parse port "${commandOptions['port']}"'); | 58 log.error('Could not parse port "${commandOptions['port']}"'); |
51 this.printUsage(); | 59 this.printUsage(); |
52 return flushThenExit(exit_codes.USAGE); | 60 return flushThenExit(exit_codes.USAGE); |
53 } | 61 } |
54 | 62 |
55 return ensureLockFileIsUpToDate() | 63 return ensureLockFileIsUpToDate().then((_) { |
56 .then((_) => entrypoint.loadPackageGraph()) | 64 return entrypoint.loadPackageGraph(); |
57 .then((graph) => barback.createServer(hostname, port, graph)) | 65 }).then((graph) { |
58 .then((server) { | 66 // TODO(rnystrom): Add support for dart2dart transformer here. |
| 67 var builtInTransformers = null; |
| 68 if (useDart2JS) { |
| 69 builtInTransformers = [ |
| 70 new Dart2JSTransformer(graph), |
| 71 new DartForwardingTransformer() |
| 72 ]; |
| 73 } |
| 74 |
| 75 return barback.createServer(hostname, port, graph, |
| 76 builtInTransformers: builtInTransformers); |
| 77 }).then((server) { |
59 /// This completer is used to keep pub running (by not completing) and | 78 /// This completer is used to keep pub running (by not completing) and |
60 /// to pipe fatal errors to pub's top-level error-handling machinery. | 79 /// to pipe fatal errors to pub's top-level error-handling machinery. |
61 var completer = new Completer(); | 80 var completer = new Completer(); |
62 | 81 |
63 server.barback.errors.listen((error) { | 82 server.barback.errors.listen((error) { |
64 log.error("${_red}Build error:\n$error$_none"); | 83 log.error("${_red}Build error:\n$error$_none"); |
65 }); | 84 }); |
66 | 85 |
67 server.barback.results.listen((result) { | 86 server.barback.results.listen((result) { |
68 if (result.succeeded) { | 87 if (result.succeeded) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // needed. | 128 // needed. |
110 if (!entrypoint.isLockFileUpToDate()) { | 129 if (!entrypoint.isLockFileUpToDate()) { |
111 log.message("Dependencies have changed, installing..."); | 130 log.message("Dependencies have changed, installing..."); |
112 return entrypoint.installDependencies().then((_) { | 131 return entrypoint.installDependencies().then((_) { |
113 log.message("Dependencies installed!"); | 132 log.message("Dependencies installed!"); |
114 }); | 133 }); |
115 } | 134 } |
116 }); | 135 }); |
117 } | 136 } |
118 } | 137 } |
OLD | NEW |