| 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.io; | 5 library dart_style.src.io; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'package:path/path.dart' as p; | 9 import 'package:path/path.dart' as p; |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 } | 31 } |
| 32 } | 32 } |
| 33 | 33 |
| 34 /// Runs the formatter on [file]. | 34 /// Runs the formatter on [file]. |
| 35 void processFile(File file, {String label, bool overwrite, int pageWidth}) { | 35 void processFile(File file, {String label, bool overwrite, int pageWidth}) { |
| 36 if (label == null) label = file.path; | 36 if (label == null) label = file.path; |
| 37 if (overwrite == null) overwrite = false; | 37 if (overwrite == null) overwrite = false; |
| 38 | 38 |
| 39 var formatter = new DartFormatter(pageWidth: pageWidth); | 39 var formatter = new DartFormatter(pageWidth: pageWidth); |
| 40 try { | 40 try { |
| 41 var output = formatter.format(file.readAsStringSync(), uri: file.path); | 41 var source = file.readAsStringSync(); |
| 42 var output = formatter.format(source, uri: file.path); |
| 42 if (overwrite) { | 43 if (overwrite) { |
| 43 file.writeAsStringSync(output); | 44 if (source != output) { |
| 44 print("Formatted $label"); | 45 file.writeAsStringSync(output); |
| 46 print("Formatted $label"); |
| 47 } else { |
| 48 print("Unchanged $label"); |
| 49 } |
| 45 } else { | 50 } else { |
| 46 // Don't add an extra newline. | 51 // Don't add an extra newline. |
| 47 stdout.write(output); | 52 stdout.write(output); |
| 48 } | 53 } |
| 49 } on FormatterException catch (err) { | 54 } on FormatterException catch (err) { |
| 50 stderr.writeln(err.message()); | 55 stderr.writeln(err.message()); |
| 51 } catch (err, stack) { | 56 } catch (err, stack) { |
| 52 stderr.writeln('''Hit a bug in the formatter when formatting $label | 57 stderr.writeln('''Hit a bug in the formatter when formatting $label |
| 53 Please report at: github.com/dart-lang/dart_style/issues | 58 Please report at: github.com/dart-lang/dart_style/issues |
| 54 $err | 59 $err |
| 55 $stack'''); | 60 $stack'''); |
| 56 } | 61 } |
| 57 } | 62 } |
| OLD | NEW |