OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // Output provider that collects the output in string buffers. | 5 // Output provider that collects the output in string buffers. |
6 | 6 |
7 library output_collector; | 7 library output_collector; |
8 | 8 |
9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 import 'package:compiler/compiler_new.dart'; |
10 | 11 |
11 class BufferedEventSink implements EventSink<String> { | 12 class BufferedEventSink implements EventSink<String> { |
12 StringBuffer sb = new StringBuffer(); | 13 StringBuffer sb = new StringBuffer(); |
13 String text; | 14 String text; |
14 | 15 |
15 void add(String event) { | 16 void add(String event) { |
16 sb.write(event); | 17 sb.write(event); |
17 } | 18 } |
18 | 19 |
19 void addError(errorEvent, [StackTrace stackTrace]) { | 20 void addError(errorEvent, [StackTrace stackTrace]) { |
(...skipping 22 matching lines...) Expand all Loading... |
42 sink.addError(errorEvent, stackTrace); | 43 sink.addError(errorEvent, stackTrace); |
43 }); | 44 }); |
44 } | 45 } |
45 | 46 |
46 @override | 47 @override |
47 void close() { | 48 void close() { |
48 sinks.forEach((EventSink<String> sink) => sink.close()); | 49 sinks.forEach((EventSink<String> sink) => sink.close()); |
49 } | 50 } |
50 } | 51 } |
51 | 52 |
52 class OutputCollector { | 53 class OutputCollector implements CompilerOutput { |
53 Map<String, Map<String, BufferedEventSink>> outputMap = {}; | 54 Map<String, Map<String, BufferedEventSink>> outputMap = {}; |
54 | 55 |
55 EventSink<String> call(String name, String extension) { | 56 EventSink<String> call(String name, String extension) { |
56 Map<String, BufferedEventSink> sinkMap = | 57 return createEventSink(name, extension); |
57 outputMap.putIfAbsent(extension, () => {}); | |
58 return sinkMap.putIfAbsent(name, () => new BufferedEventSink()); | |
59 } | 58 } |
60 | 59 |
61 String getOutput(String name, String extension) { | 60 String getOutput(String name, String extension) { |
62 Map<String, BufferedEventSink> sinkMap = outputMap[extension]; | 61 Map<String, BufferedEventSink> sinkMap = outputMap[extension]; |
63 if (sinkMap == null) return null; | 62 if (sinkMap == null) return null; |
64 BufferedEventSink sink = sinkMap[name]; | 63 BufferedEventSink sink = sinkMap[name]; |
65 return sink != null ? sink.text : null; | 64 return sink != null ? sink.text : null; |
66 } | 65 } |
| 66 |
| 67 /// `true` if any output has been collected. |
| 68 bool get hasOutput => !outputMap.isEmpty; |
| 69 |
| 70 /// `true` if any output other than main output has been collected. |
| 71 bool get hasExtraOutput { |
| 72 for (String extension in outputMap.keys) { |
| 73 for (String name in outputMap[extension].keys) { |
| 74 if (name != '') return true; |
| 75 } |
| 76 } |
| 77 return false; |
| 78 } |
| 79 |
| 80 @override |
| 81 EventSink<String> createEventSink(String name, String extension) { |
| 82 Map<String, BufferedEventSink> sinkMap = |
| 83 outputMap.putIfAbsent(extension, () => {}); |
| 84 return sinkMap.putIfAbsent(name, () => new BufferedEventSink()); |
| 85 } |
67 } | 86 } |
OLD | NEW |