| 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 import 'package:compiler/compiler_new.dart'; |
| 11 | 11 |
| 12 class BufferedEventSink implements EventSink<String> { | 12 class BufferedOutputSink implements OutputSink { |
| 13 StringBuffer sb = new StringBuffer(); | 13 StringBuffer sb = new StringBuffer(); |
| 14 String text; | 14 String text; |
| 15 | 15 |
| 16 void add(String event) { | 16 void add(String event) { |
| 17 sb.write(event); | 17 sb.write(event); |
| 18 } | 18 } |
| 19 | 19 |
| 20 void addError(errorEvent, [StackTrace stackTrace]) { | |
| 21 // Do not support this. | |
| 22 } | |
| 23 | |
| 24 void close() { | 20 void close() { |
| 25 text = sb.toString(); | 21 text = sb.toString(); |
| 26 sb = null; | 22 sb = null; |
| 27 } | 23 } |
| 28 } | 24 } |
| 29 | 25 |
| 30 class CloningEventSink implements EventSink<String> { | 26 class CloningOutputSink implements OutputSink { |
| 31 final List<EventSink<String>> sinks; | 27 final List<OutputSink> sinks; |
| 32 | 28 |
| 33 CloningEventSink(this.sinks); | 29 CloningOutputSink(this.sinks); |
| 34 | 30 |
| 35 @override | 31 @override |
| 36 void add(String event) { | 32 void add(String event) { |
| 37 sinks.forEach((EventSink<String> sink) => sink.add(event)); | 33 sinks.forEach((OutputSink sink) => sink.add(event)); |
| 38 } | |
| 39 | |
| 40 @override | |
| 41 void addError(errorEvent, [StackTrace stackTrace]) { | |
| 42 sinks.forEach((EventSink<String> sink) { | |
| 43 sink.addError(errorEvent, stackTrace); | |
| 44 }); | |
| 45 } | 34 } |
| 46 | 35 |
| 47 @override | 36 @override |
| 48 void close() { | 37 void close() { |
| 49 sinks.forEach((EventSink<String> sink) => sink.close()); | 38 sinks.forEach((OutputSink sink) => sink.close()); |
| 50 } | 39 } |
| 51 } | 40 } |
| 52 | 41 |
| 53 class OutputCollector implements CompilerOutput { | 42 class OutputCollector implements CompilerOutput { |
| 54 Map<String, Map<String, BufferedEventSink>> outputMap = {}; | 43 Map<OutputType, Map<String, BufferedOutputSink>> outputMap = {}; |
| 55 | 44 |
| 56 EventSink<String> call(String name, String extension) { | 45 String getOutput(String name, OutputType type) { |
| 57 return createEventSink(name, extension); | 46 Map<String, BufferedOutputSink> sinkMap = outputMap[type]; |
| 58 } | |
| 59 | |
| 60 String getOutput(String name, String extension) { | |
| 61 Map<String, BufferedEventSink> sinkMap = outputMap[extension]; | |
| 62 if (sinkMap == null) return null; | 47 if (sinkMap == null) return null; |
| 63 BufferedEventSink sink = sinkMap[name]; | 48 BufferedOutputSink sink = sinkMap[name]; |
| 64 return sink != null ? sink.text : null; | 49 return sink != null ? sink.text : null; |
| 65 } | 50 } |
| 66 | 51 |
| 67 /// `true` if any output has been collected. | 52 /// `true` if any output has been collected. |
| 68 bool get hasOutput => !outputMap.isEmpty; | 53 bool get hasOutput => !outputMap.isEmpty; |
| 69 | 54 |
| 70 /// `true` if any output other than main output has been collected. | 55 /// `true` if any output other than main output has been collected. |
| 71 bool get hasExtraOutput { | 56 bool get hasExtraOutput { |
| 72 for (String extension in outputMap.keys) { | 57 for (OutputType type in outputMap.keys) { |
| 73 for (String name in outputMap[extension].keys) { | 58 for (String name in outputMap[type].keys) { |
| 74 if (name != '') return true; | 59 if (name != '') return true; |
| 75 } | 60 } |
| 76 } | 61 } |
| 77 return false; | 62 return false; |
| 78 } | 63 } |
| 79 | 64 |
| 80 @override | 65 @override |
| 81 EventSink<String> createEventSink(String name, String extension) { | 66 OutputSink createOutputSink(String name, String extension, OutputType type) { |
| 82 Map<String, BufferedEventSink> sinkMap = | 67 Map<String, BufferedOutputSink> sinkMap = |
| 83 outputMap.putIfAbsent(extension, () => {}); | 68 outputMap.putIfAbsent(type, () => {}); |
| 84 return sinkMap.putIfAbsent(name, () => new BufferedEventSink()); | 69 return sinkMap.putIfAbsent(name, () => new BufferedOutputSink()); |
| 85 } | 70 } |
| 86 } | 71 } |
| OLD | NEW |