| 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 |
| 11 import 'dart_formatter.dart'; | 11 import 'dart_formatter.dart'; |
| 12 import 'formatter_options.dart'; | 12 import 'formatter_options.dart'; |
| 13 import 'formatter_exception.dart'; | 13 import 'formatter_exception.dart'; |
| 14 import 'source_code.dart'; |
| 14 | 15 |
| 15 /// Runs the formatter on every .dart file in [path] (and its subdirectories), | 16 /// Runs the formatter on every .dart file in [path] (and its subdirectories), |
| 16 /// and replaces them with their formatted output. | 17 /// and replaces them with their formatted output. |
| 17 /// | 18 /// |
| 18 /// Returns `true` if successful or `false` if an error occurred in any of the | 19 /// Returns `true` if successful or `false` if an error occurred in any of the |
| 19 /// files. | 20 /// files. |
| 20 bool processDirectory(FormatterOptions options, Directory directory) { | 21 bool processDirectory(FormatterOptions options, Directory directory) { |
| 21 options.reporter.showDirectory(directory.path); | 22 options.reporter.showDirectory(directory.path); |
| 22 | 23 |
| 23 var success = true; | 24 var success = true; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 40 | 41 |
| 41 if (!processFile(options, entry, label: relative)) success = false; | 42 if (!processFile(options, entry, label: relative)) success = false; |
| 42 } | 43 } |
| 43 | 44 |
| 44 return success; | 45 return success; |
| 45 } | 46 } |
| 46 | 47 |
| 47 /// Runs the formatter on [file]. | 48 /// Runs the formatter on [file]. |
| 48 /// | 49 /// |
| 49 /// Returns `true` if successful or `false` if an error occurred. | 50 /// Returns `true` if successful or `false` if an error occurred. |
| 50 bool processFile(FormatterOptions options, File file, | 51 bool processFile(FormatterOptions options, File file, {String label}) { |
| 51 {String label}) { | |
| 52 if (label == null) label = file.path; | 52 if (label == null) label = file.path; |
| 53 | 53 |
| 54 var formatter = new DartFormatter(pageWidth: options.pageWidth); | 54 var formatter = new DartFormatter(pageWidth: options.pageWidth); |
| 55 try { | 55 try { |
| 56 var source = file.readAsStringSync(); | 56 var source = new SourceCode(file.readAsStringSync(), uri: file.path); |
| 57 var output = formatter.format(source, uri: file.path); | 57 var output = formatter.formatSource(source); |
| 58 options.reporter.showFile(file, label, output, changed: source != output); | 58 options.reporter.showFile(file, label, output, |
| 59 changed: source.text != output.text); |
| 59 return true; | 60 return true; |
| 60 } on FormatterException catch (err) { | 61 } on FormatterException catch (err) { |
| 61 stderr.writeln(err.message()); | 62 stderr.writeln(err.message()); |
| 62 } catch (err, stack) { | 63 } catch (err, stack) { |
| 63 stderr.writeln('''Hit a bug in the formatter when formatting $label. | 64 stderr.writeln('''Hit a bug in the formatter when formatting $label. |
| 64 Please report at: github.com/dart-lang/dart_style/issues | 65 Please report at: github.com/dart-lang/dart_style/issues |
| 65 $err | 66 $err |
| 66 $stack'''); | 67 $stack'''); |
| 67 } | 68 } |
| 68 | 69 |
| 69 return false; | 70 return false; |
| 70 } | 71 } |
| OLD | NEW |