OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 dart_style.src.formatter_options; | 5 library dart_style.src.formatter_options; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'source_code.dart'; | 10 import 'source_code.dart'; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 } on FileSystemException catch (err) { | 122 } on FileSystemException catch (err) { |
123 stderr.writeln("Could not overwrite $label: " | 123 stderr.writeln("Could not overwrite $label: " |
124 "${err.osError.message} (error code ${err.osError.errorCode})"); | 124 "${err.osError.message} (error code ${err.osError.errorCode})"); |
125 } | 125 } |
126 } else { | 126 } else { |
127 print("Unchanged $label"); | 127 print("Unchanged $label"); |
128 } | 128 } |
129 } | 129 } |
130 } | 130 } |
131 | 131 |
| 132 /// Base clase for a reporter that decorates an inner reporter. |
| 133 abstract class _ReporterDecorator implements OutputReporter { |
| 134 final OutputReporter _inner; |
| 135 |
| 136 _ReporterDecorator(this._inner); |
| 137 |
| 138 void showDirectory(String path) { |
| 139 _inner.showDirectory(path); |
| 140 } |
| 141 |
| 142 void showSkippedLink(String path) { |
| 143 _inner.showSkippedLink(path); |
| 144 } |
| 145 |
| 146 void showHiddenPath(String path) { |
| 147 _inner.showHiddenPath(path); |
| 148 } |
| 149 |
| 150 void beforeFile(File file, String label) { |
| 151 _inner.beforeFile(file, label); |
| 152 } |
| 153 |
| 154 void afterFile(File file, String label, SourceCode output, {bool changed}) { |
| 155 _inner.afterFile(file, label, output, changed: changed); |
| 156 } |
| 157 } |
| 158 |
132 /// A decorating reporter that reports how long it took for format each file. | 159 /// A decorating reporter that reports how long it took for format each file. |
133 class ProfileReporter implements OutputReporter { | 160 class ProfileReporter extends _ReporterDecorator { |
134 final OutputReporter _inner; | |
135 | |
136 /// The files that have been started but have not completed yet. | 161 /// The files that have been started but have not completed yet. |
137 /// | 162 /// |
138 /// Maps a file label to the time that it started being formatted. | 163 /// Maps a file label to the time that it started being formatted. |
139 final Map<String, DateTime> _ongoing = {}; | 164 final Map<String, DateTime> _ongoing = {}; |
140 | 165 |
141 /// The elapsed time it took to format each completed file. | 166 /// The elapsed time it took to format each completed file. |
142 final Map<String, Duration> _elapsed = {}; | 167 final Map<String, Duration> _elapsed = {}; |
143 | 168 |
144 /// The number of files that completed so fast that they aren't worth | 169 /// The number of files that completed so fast that they aren't worth |
145 /// tracking. | 170 /// tracking. |
146 int _elided = 0; | 171 int _elided = 0; |
147 | 172 |
148 ProfileReporter(this._inner); | 173 ProfileReporter(OutputReporter inner) : super(inner); |
149 | 174 |
150 /// Show the times for the slowest files to format. | 175 /// Show the times for the slowest files to format. |
151 void showProfile() { | 176 void showProfile() { |
152 // Everything should be done. | 177 // Everything should be done. |
153 assert(_ongoing.isEmpty); | 178 assert(_ongoing.isEmpty); |
154 | 179 |
155 var files = _elapsed.keys.toList(); | 180 var files = _elapsed.keys.toList(); |
156 files.sort((a, b) => _elapsed[b].compareTo(_elapsed[a])); | 181 files.sort((a, b) => _elapsed[b].compareTo(_elapsed[a])); |
157 | 182 |
158 for (var file in files) { | 183 for (var file in files) { |
159 print("${_elapsed[file]}: $file"); | 184 print("${_elapsed[file]}: $file"); |
160 } | 185 } |
161 | 186 |
162 if (_elided >= 1) { | 187 if (_elided >= 1) { |
163 var s = _elided > 1 ? 's' : ''; | 188 var s = _elided > 1 ? 's' : ''; |
164 print("...$_elided more file$s each took less than 10ms."); | 189 print("...$_elided more file$s each took less than 10ms."); |
165 } | 190 } |
166 } | 191 } |
167 | 192 |
168 /// Describe the directory whose contents are about to be processed. | |
169 void showDirectory(String path) { | |
170 _inner.showDirectory(path); | |
171 } | |
172 | |
173 /// Describe the symlink at [path] that wasn't followed. | |
174 void showSkippedLink(String path) { | |
175 _inner.showSkippedLink(path); | |
176 } | |
177 | |
178 /// Describe the hidden [path] that wasn't processed. | |
179 void showHiddenPath(String path) { | |
180 _inner.showHiddenPath(path); | |
181 } | |
182 | |
183 /// Called when [file] is about to be formatted. | 193 /// Called when [file] is about to be formatted. |
184 void beforeFile(File file, String label) { | 194 void beforeFile(File file, String label) { |
185 _inner.beforeFile(file, label); | 195 super.beforeFile(file, label); |
186 | |
187 _ongoing[label] = new DateTime.now(); | 196 _ongoing[label] = new DateTime.now(); |
188 } | 197 } |
189 | 198 |
190 /// Describe the processed file at [path] whose formatted result is [output]. | 199 /// Describe the processed file at [path] whose formatted result is [output]. |
191 /// | 200 /// |
192 /// If the contents of the file are the same as the formatted output, | 201 /// If the contents of the file are the same as the formatted output, |
193 /// [changed] will be false. | 202 /// [changed] will be false. |
194 void afterFile(File file, String label, SourceCode output, {bool changed}) { | 203 void afterFile(File file, String label, SourceCode output, {bool changed}) { |
195 var elapsed = new DateTime.now().difference(_ongoing.remove(label)); | 204 var elapsed = new DateTime.now().difference(_ongoing.remove(label)); |
196 if (elapsed.inMilliseconds >= 10) { | 205 if (elapsed.inMilliseconds >= 10) { |
197 _elapsed[label] = elapsed; | 206 _elapsed[label] = elapsed; |
198 } else { | 207 } else { |
199 _elided++; | 208 _elided++; |
200 } | 209 } |
201 | 210 |
202 _inner.afterFile(file, label, output, changed: changed); | 211 super.afterFile(file, label, output, changed: changed); |
203 } | 212 } |
204 } | 213 } |
| 214 |
| 215 /// A decorating reporter that sets the exit code to 1 if any changes are made. |
| 216 class SetExitReporter extends _ReporterDecorator { |
| 217 SetExitReporter(OutputReporter inner) : super(inner); |
| 218 |
| 219 /// Describe the processed file at [path] whose formatted result is [output]. |
| 220 /// |
| 221 /// If the contents of the file are the same as the formatted output, |
| 222 /// [changed] will be false. |
| 223 void afterFile(File file, String label, SourceCode output, {bool changed}) { |
| 224 if (changed) exitCode = 1; |
| 225 |
| 226 super.afterFile(file, label, output, changed: changed); |
| 227 } |
| 228 } |
OLD | NEW |