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 'package:barback/barback.dart'; | 9 import 'package:barback/barback.dart'; |
10 | 10 |
11 import '../barback/dart_forwarding_transformer.dart'; | 11 import '../barback/build_environment.dart'; |
12 import '../barback/dart2js_transformer.dart'; | |
13 import '../barback/pub_package_provider.dart'; | 12 import '../barback/pub_package_provider.dart'; |
14 import '../barback.dart' as barback; | |
15 import '../command.dart'; | 13 import '../command.dart'; |
16 import '../entrypoint.dart'; | |
17 import '../exit_codes.dart' as exit_codes; | 14 import '../exit_codes.dart' as exit_codes; |
18 import '../io.dart'; | 15 import '../io.dart'; |
19 import '../log.dart' as log; | 16 import '../log.dart' as log; |
20 import '../utils.dart'; | 17 import '../utils.dart'; |
21 | 18 |
22 final _arrow = getSpecial('\u2192', '=>'); | 19 final _arrow = getSpecial('\u2192', '=>'); |
23 | 20 |
24 /// Handles the `serve` pub command. | 21 /// Handles the `serve` pub command. |
25 class ServeCommand extends PubCommand { | 22 class ServeCommand extends PubCommand { |
26 String get description => "Run a local web development server."; | 23 String get description => "Run a local web development server."; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 Future onRun() { | 55 Future onRun() { |
59 var port; | 56 var port; |
60 try { | 57 try { |
61 port = int.parse(commandOptions['port']); | 58 port = int.parse(commandOptions['port']); |
62 } on FormatException catch (_) { | 59 } on FormatException catch (_) { |
63 log.error('Could not parse port "${commandOptions['port']}"'); | 60 log.error('Could not parse port "${commandOptions['port']}"'); |
64 this.printUsage(); | 61 this.printUsage(); |
65 return flushThenExit(exit_codes.USAGE); | 62 return flushThenExit(exit_codes.USAGE); |
66 } | 63 } |
67 | 64 |
68 return entrypoint.loadPackageGraph().then((graph) { | 65 var watcherType = commandOptions['force-poll'] ? |
69 var builtInTransformers = [new DartForwardingTransformer(mode)]; | 66 WatcherType.POLLING : WatcherType.AUTO; |
70 if (useDart2JS) { | |
71 builtInTransformers.add(new Dart2JSTransformer(graph, mode)); | |
72 // TODO(rnystrom): Add support for dart2dart transformer here. | |
73 } | |
74 | 67 |
75 var watcherType = commandOptions['force-poll'] ? | 68 return BuildEnvironment.create(entrypoint, hostname, port, mode, |
76 barback.WatcherType.POLLING : barback.WatcherType.AUTO; | 69 watcherType, ["web"].toSet(), |
77 return barback.createServer(hostname, port, graph, mode, | 70 useDart2JS: useDart2JS).then((environment) { |
78 builtInTransformers: builtInTransformers, | 71 |
79 watcher: watcherType); | |
80 }).then((server) { | |
81 // In release mode, strip out .dart files since all relevant ones have | 72 // In release mode, strip out .dart files since all relevant ones have |
82 // been compiled to JavaScript already. | 73 // been compiled to JavaScript already. |
83 if (mode == BarbackMode.RELEASE) { | 74 if (mode == BarbackMode.RELEASE) { |
84 server.allowAsset = (url) => !url.path.endsWith(".dart"); | 75 environment.server.allowAsset = (url) => !url.path.endsWith(".dart"); |
85 } | 76 } |
86 | 77 |
87 /// 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 |
88 /// 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. |
89 var completer = new Completer(); | 80 var completer = new Completer(); |
90 | 81 |
91 server.barback.errors.listen((error) { | 82 environment.server.barback.errors.listen((error) { |
92 log.error(log.red("Build error:\n$error")); | 83 log.error(log.red("Build error:\n$error")); |
93 }); | 84 }); |
94 | 85 |
95 server.barback.results.listen((result) { | 86 environment.server.barback.results.listen((result) { |
96 if (result.succeeded) { | 87 if (result.succeeded) { |
97 // TODO(rnystrom): Report using growl/inotify-send where available. | 88 // TODO(rnystrom): Report using growl/inotify-send where available. |
98 log.message("Build completed ${log.green('successfully')}"); | 89 log.message("Build completed ${log.green('successfully')}"); |
99 } else { | 90 } else { |
100 log.message("Build completed with " | 91 log.message("Build completed with " |
101 "${log.red(result.errors.length)} errors."); | 92 "${log.red(result.errors.length)} errors."); |
102 } | 93 } |
103 }, onError: (error, [stackTrace]) { | 94 }, onError: (error, [stackTrace]) { |
104 if (!completer.isCompleted) completer.completeError(error, stackTrace); | 95 if (!completer.isCompleted) completer.completeError(error, stackTrace); |
105 }); | 96 }); |
106 | 97 |
107 server.results.listen((result) { | 98 environment.server.results.listen((result) { |
108 if (result.isSuccess) { | 99 if (result.isSuccess) { |
109 log.message("${log.green('GET')} ${result.url.path} $_arrow " | 100 log.message("${log.green('GET')} ${result.url.path} $_arrow " |
110 "${result.id}"); | 101 "${result.id}"); |
111 return; | 102 return; |
112 } | 103 } |
113 | 104 |
114 var msg = "${log.red('GET')} ${result.url.path} $_arrow"; | 105 var msg = "${log.red('GET')} ${result.url.path} $_arrow"; |
115 var error = result.error.toString(); | 106 var error = result.error.toString(); |
116 if (error.contains("\n")) { | 107 if (error.contains("\n")) { |
117 log.message("$msg\n${prefixLines(error)}"); | 108 log.message("$msg\n${prefixLines(error)}"); |
118 } else { | 109 } else { |
119 log.message("$msg $error"); | 110 log.message("$msg $error"); |
120 } | 111 } |
121 }, onError: (error, [stackTrace]) { | 112 }, onError: (error, [stackTrace]) { |
122 if (!completer.isCompleted) completer.completeError(error, stackTrace); | 113 if (!completer.isCompleted) completer.completeError(error, stackTrace); |
123 }); | 114 }); |
124 | 115 |
125 log.message("Serving ${entrypoint.root.name} " | 116 log.message("Serving ${entrypoint.root.name} " |
126 "on http://$hostname:${server.port}"); | 117 "on http://$hostname:${environment.server.port}"); |
127 | 118 |
128 return completer.future; | 119 return completer.future; |
129 }); | 120 }); |
130 } | 121 } |
131 } | 122 } |
OLD | NEW |