Chromium Code Reviews| 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:convert'; | |
| 8 import 'dart:io'; | 7 import 'dart:io'; |
| 9 | 8 |
| 10 import 'package:args/args.dart'; | 9 import 'package:args/args.dart'; |
| 11 | 10 |
| 12 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; | 11 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; |
| 13 | 12 |
| 14 | 13 |
| 15 const BINARY_NAME = 'dartfmt'; | 14 const BINARY_NAME = 'dartfmt'; |
| 15 const DART_EXT = '.dart'; | |
| 16 final argParser = _initArgParser(); | 16 final argParser = _initArgParser(); |
| 17 | 17 |
| 18 void main() { | 18 void main() { |
| 19 var options = argParser.parse(new Options().arguments); | 19 var options = argParser.parse(new Options().arguments); |
| 20 if (options['help']) { | 20 if (options['help']) { |
| 21 _printUsage(); | 21 _printUsage(); |
| 22 return; | 22 return; |
| 23 } | 23 } |
| 24 if (options.rest.isEmpty) { | 24 if (options.rest.isEmpty) { |
| 25 _formatStdin(options); | 25 _formatStdin(options); |
| 26 } else { | 26 } else { |
| 27 _formatFiles(options.rest); | 27 _formatPaths(options.rest); |
| 28 } | 28 } |
| 29 } | 29 } |
| 30 | 30 |
| 31 _formatFiles(files) { | 31 _formatPaths(paths) { |
| 32 for (var file in files) { | 32 paths.forEach((path) { |
| 33 _formatFile(file); | 33 if (FileSystemEntity.isDirectorySync(path)) { |
| 34 _formatDirectory(new Directory(path)); | |
| 35 } else { | |
| 36 _formatFile(new File(path)); | |
| 37 } | |
| 38 }); | |
| 39 } | |
| 40 | |
| 41 _formatResource(resource) { | |
| 42 if (resource is Directory) { | |
| 43 _formatDirectory(resource); | |
| 44 } else if (resource is File) { | |
| 45 _formatFile(resource); | |
| 34 } | 46 } |
| 35 } | 47 } |
| 36 | 48 |
| 37 _formatFile(path) { | 49 _formatDirectory(dir) => |
| 38 var buffer = new StringBuffer(); | 50 dir.listSync().forEach((resource) => _formatResource(resource)); |
| 39 var file = new File(path); | 51 |
| 40 file.openRead() | 52 |
| 41 .transform(UTF8.decoder) | 53 void _formatFile(file) { |
| 42 .listen((data) => buffer.write(data), | 54 if (_isDartFile(file)) { |
| 43 onError: (error) => print('Error, could not open "$path"'), | 55 try { |
| 44 onDone: () => print(_formatCU(buffer.toString()))); | 56 var buffer = new StringBuffer(); |
| 57 var rawSource = file.readAsStringSync(); | |
| 58 var formatted = _formatCU(rawSource); | |
| 59 print(formatted); | |
| 60 file.writeAsStringSync(formatted); | |
| 61 } catch (e) { | |
| 62 _log('Error formatting "${file.path}": $e'); | |
| 63 } | |
| 64 } | |
| 45 } | 65 } |
| 46 | 66 |
| 67 _isDartFile(file) => !file.path.startsWith('.') && file.path.endsWith(DART_EXT); | |
|
scheglov
2013/08/27 23:17:20
Ignore case?
| |
| 68 | |
| 47 _formatStdin(options) { | 69 _formatStdin(options) { |
| 48 _log('not supported yet!'); | 70 _log('not supported yet!'); |
| 49 // stdin.transform(new StringDecoder()) | 71 // stdin.transform(new StringDecoder()) |
| 50 // .listen((String data) => print(data), | 72 // .listen((String data) => print(data), |
| 51 // onError: (error) => print('Error reading from stdin'), | 73 // onError: (error) => print('Error reading from stdin'), |
| 52 // onDone: () => print('Finished reading data')); | 74 // onDone: () => print('Finished reading data')); |
| 53 } | 75 } |
| 54 | 76 |
| 55 /// Initialize the arg parser instance. | 77 /// Initialize the arg parser instance. |
| 56 ArgParser _initArgParser() { | 78 ArgParser _initArgParser() { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 85 | 107 |
| 86 /// Format the given [src] as a compilation unit. | 108 /// Format the given [src] as a compilation unit. |
| 87 String _formatCU(src, {options: const FormatterOptions()}) => | 109 String _formatCU(src, {options: const FormatterOptions()}) => |
| 88 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); | 110 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); |
| 89 | 111 |
| 90 /// Log the given [msg]. | 112 /// Log the given [msg]. |
| 91 _log(String msg) { | 113 _log(String msg) { |
| 92 //TODO(pquitslund): add proper log support | 114 //TODO(pquitslund): add proper log support |
| 93 print(msg); | 115 print(msg); |
| 94 } | 116 } |
| OLD | NEW |