| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 | 2 |
| 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 4 // for details. All rights reserved. Use of this source code is governed by a | 4 // for details. All rights reserved. Use of this source code is governed by a |
| 5 // BSD-style license that can be found in the LICENSE file. | 5 // BSD-style license that can be found in the LICENSE file. |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'package:args/args.dart'; | 9 import 'package:args/args.dart'; |
| 10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 11 | 11 |
| 12 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; | 12 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; |
| 13 | 13 |
| 14 | 14 |
| 15 const BINARY_NAME = 'dartfmt'; | 15 const BINARY_NAME = 'dartfmt'; |
| 16 final dartFileRegExp = new RegExp(r'^[^.].*\.dart$', caseSensitive: false); | 16 final dartFileRegExp = new RegExp(r'^[^.].*\.dart$', caseSensitive: false); |
| 17 final argParser = _initArgParser(); | 17 final argParser = _initArgParser(); |
| 18 | 18 |
| 19 bool overwriteFileContents; |
| 20 |
| 19 void main() { | 21 void main() { |
| 20 var options = argParser.parse(new Options().arguments); | 22 var options = argParser.parse(new Options().arguments); |
| 21 if (options['help']) { | 23 if (options['help']) { |
| 22 _printUsage(); | 24 _printUsage(); |
| 23 return; | 25 return; |
| 24 } | 26 } |
| 27 overwriteFileContents = options['write']; |
| 28 |
| 25 if (options.rest.isEmpty) { | 29 if (options.rest.isEmpty) { |
| 26 _formatStdin(options); | 30 _formatStdin(options); |
| 27 } else { | 31 } else { |
| 28 _formatPaths(options.rest); | 32 _formatPaths(options.rest); |
| 29 } | 33 } |
| 30 } | 34 } |
| 31 | 35 |
| 32 _formatPaths(paths) { | 36 _formatPaths(paths) { |
| 33 paths.forEach((path) { | 37 paths.forEach((path) { |
| 34 if (FileSystemEntity.isDirectorySync(path)) { | 38 if (FileSystemEntity.isDirectorySync(path)) { |
| 35 _formatDirectory(new Directory(path)); | 39 _formatDirectory(new Directory(path)); |
| 36 } else { | 40 } else { |
| 37 _formatFile(new File(path)); | 41 _formatFile(new File(path)); |
| 38 } | 42 } |
| 39 }); | 43 }); |
| 40 } | 44 } |
| 41 | 45 |
| 42 _formatResource(resource) { | 46 _formatResource(resource) { |
| 43 if (resource is Directory) { | 47 if (resource is Directory) { |
| 44 _formatDirectory(resource); | 48 _formatDirectory(resource); |
| 45 } else if (resource is File) { | 49 } else if (resource is File) { |
| 46 _formatFile(resource); | 50 _formatFile(resource); |
| 47 } | 51 } |
| 48 } | 52 } |
| 49 | 53 |
| 50 _formatDirectory(dir) => | 54 _formatDirectory(dir) => |
| 51 dir.listSync().forEach((resource) => _formatResource(resource)); | 55 dir.listSync().forEach((resource) => _formatResource(resource)); |
| 52 | 56 |
| 53 | |
| 54 void _formatFile(file) { | 57 void _formatFile(file) { |
| 55 if (_isDartFile(file)) { | 58 if (_isDartFile(file)) { |
| 56 try { | 59 try { |
| 57 var buffer = new StringBuffer(); | 60 var buffer = new StringBuffer(); |
| 58 var rawSource = file.readAsStringSync(); | 61 var rawSource = file.readAsStringSync(); |
| 59 var formatted = _formatCU(rawSource); | 62 var formatted = _formatCU(rawSource); |
| 60 print(formatted); | 63 if (overwriteFileContents) { |
| 61 file.writeAsStringSync(formatted); | 64 file.writeAsStringSync(formatted); |
| 65 } else { |
| 66 print(formatted); |
| 67 } |
| 62 } catch (e) { | 68 } catch (e) { |
| 63 _log('Error formatting "${file.path}": $e'); | 69 _log('Error formatting "${file.path}": $e'); |
| 64 } | 70 } |
| 65 } | 71 } |
| 66 } | 72 } |
| 67 | 73 |
| 68 _isDartFile(file) => dartFileRegExp.hasMatch(path.basename(file.path)); | 74 _isDartFile(file) => dartFileRegExp.hasMatch(path.basename(file.path)); |
| 69 | 75 |
| 70 _formatStdin(options) { | 76 _formatStdin(options) { |
| 71 _log('not supported yet!'); | 77 _log('not supported yet!'); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 | 114 |
| 109 /// Format the given [src] as a compilation unit. | 115 /// Format the given [src] as a compilation unit. |
| 110 String _formatCU(src, {options: const FormatterOptions()}) => | 116 String _formatCU(src, {options: const FormatterOptions()}) => |
| 111 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); | 117 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); |
| 112 | 118 |
| 113 /// Log the given [msg]. | 119 /// Log the given [msg]. |
| 114 _log(String msg) { | 120 _log(String msg) { |
| 115 //TODO(pquitslund): add proper log support | 121 //TODO(pquitslund): add proper log support |
| 116 print(msg); | 122 print(msg); |
| 117 } | 123 } |
| OLD | NEW |