| Index: packages/dart_style/lib/src/formatter_options.dart
|
| diff --git a/packages/dart_style/lib/src/formatter_options.dart b/packages/dart_style/lib/src/formatter_options.dart
|
| index 69a46ffac44d8a951aaa142f68c4c37d3cef5060..989977191677bc66cb98106d953423e083b16bf3 100644
|
| --- a/packages/dart_style/lib/src/formatter_options.dart
|
| +++ b/packages/dart_style/lib/src/formatter_options.dart
|
| @@ -32,17 +32,17 @@ class FormatterOptions {
|
| abstract class OutputReporter {
|
| /// Prints only the names of files whose contents are different from their
|
| /// formatted version.
|
| - static final dryRun = new _DryRunReporter();
|
| + static final OutputReporter dryRun = new _DryRunReporter();
|
|
|
| /// Prints the formatted results of each file to stdout.
|
| - static final print = new _PrintReporter();
|
| + static final OutputReporter print = new _PrintReporter();
|
|
|
| /// Prints the formatted result and selection info of each file to stdout as
|
| /// a JSON map.
|
| - static final printJson = new _PrintJsonReporter();
|
| + static final OutputReporter printJson = new _PrintJsonReporter();
|
|
|
| /// Overwrites each file with its formatted result.
|
| - static final overwrite = new _OverwriteReporter();
|
| + static final OutputReporter overwrite = new _OverwriteReporter();
|
|
|
| /// Describe the directory whose contents are about to be processed.
|
| void showDirectory(String path) {}
|
| @@ -116,18 +116,48 @@ class _PrintJsonReporter extends OutputReporter {
|
| class _OverwriteReporter extends _PrintReporter {
|
| void afterFile(File file, String label, SourceCode output, {bool changed}) {
|
| if (changed) {
|
| - file.writeAsStringSync(output.text);
|
| - print("Formatted $label");
|
| + try {
|
| + file.writeAsStringSync(output.text);
|
| + print("Formatted $label");
|
| + } on FileSystemException catch (err) {
|
| + stderr.writeln("Could not overwrite $label: "
|
| + "${err.osError.message} (error code ${err.osError.errorCode})");
|
| + }
|
| } else {
|
| print("Unchanged $label");
|
| }
|
| }
|
| }
|
|
|
| -/// A decorating reporter that reports how long it took for format each file.
|
| -class ProfileReporter implements OutputReporter {
|
| +/// Base clase for a reporter that decorates an inner reporter.
|
| +abstract class _ReporterDecorator implements OutputReporter {
|
| final OutputReporter _inner;
|
|
|
| + _ReporterDecorator(this._inner);
|
| +
|
| + void showDirectory(String path) {
|
| + _inner.showDirectory(path);
|
| + }
|
| +
|
| + void showSkippedLink(String path) {
|
| + _inner.showSkippedLink(path);
|
| + }
|
| +
|
| + void showHiddenPath(String path) {
|
| + _inner.showHiddenPath(path);
|
| + }
|
| +
|
| + void beforeFile(File file, String label) {
|
| + _inner.beforeFile(file, label);
|
| + }
|
| +
|
| + void afterFile(File file, String label, SourceCode output, {bool changed}) {
|
| + _inner.afterFile(file, label, output, changed: changed);
|
| + }
|
| +}
|
| +
|
| +/// A decorating reporter that reports how long it took for format each file.
|
| +class ProfileReporter extends _ReporterDecorator {
|
| /// The files that have been started but have not completed yet.
|
| ///
|
| /// Maps a file label to the time that it started being formatted.
|
| @@ -140,7 +170,7 @@ class ProfileReporter implements OutputReporter {
|
| /// tracking.
|
| int _elided = 0;
|
|
|
| - ProfileReporter(this._inner);
|
| + ProfileReporter(OutputReporter inner) : super(inner);
|
|
|
| /// Show the times for the slowest files to format.
|
| void showProfile() {
|
| @@ -160,25 +190,9 @@ class ProfileReporter implements OutputReporter {
|
| }
|
| }
|
|
|
| - /// Describe the directory whose contents are about to be processed.
|
| - void showDirectory(String path) {
|
| - _inner.showDirectory(path);
|
| - }
|
| -
|
| - /// Describe the symlink at [path] that wasn't followed.
|
| - void showSkippedLink(String path) {
|
| - _inner.showSkippedLink(path);
|
| - }
|
| -
|
| - /// Describe the hidden [path] that wasn't processed.
|
| - void showHiddenPath(String path) {
|
| - _inner.showHiddenPath(path);
|
| - }
|
| -
|
| /// Called when [file] is about to be formatted.
|
| void beforeFile(File file, String label) {
|
| - _inner.beforeFile(file, label);
|
| -
|
| + super.beforeFile(file, label);
|
| _ongoing[label] = new DateTime.now();
|
| }
|
|
|
| @@ -194,6 +208,21 @@ class ProfileReporter implements OutputReporter {
|
| _elided++;
|
| }
|
|
|
| - _inner.afterFile(file, label, output, changed: changed);
|
| + super.afterFile(file, label, output, changed: changed);
|
| + }
|
| +}
|
| +
|
| +/// A decorating reporter that sets the exit code to 1 if any changes are made.
|
| +class SetExitReporter extends _ReporterDecorator {
|
| + SetExitReporter(OutputReporter inner) : super(inner);
|
| +
|
| + /// Describe the processed file at [path] whose formatted result is [output].
|
| + ///
|
| + /// If the contents of the file are the same as the formatted output,
|
| + /// [changed] will be false.
|
| + void afterFile(File file, String label, SourceCode output, {bool changed}) {
|
| + if (changed) exitCode = 1;
|
| +
|
| + super.afterFile(file, label, output, changed: changed);
|
| }
|
| }
|
|
|