| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env dart |
| 2 |
| 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 |
| 5 // BSD-style license that can be found in the LICENSE file. |
| 6 |
| 7 import 'dart:io'; |
| 8 |
| 9 import 'package:args/args.dart'; |
| 10 |
| 11 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; |
| 12 |
| 13 |
| 14 const BINARY_NAME = 'dartfmt'; |
| 15 final argParser = _initArgParser(); |
| 16 |
| 17 void main() { |
| 18 var options = argParser.parse(new Options().arguments); |
| 19 if (options['help']) { |
| 20 _printUsage(); |
| 21 return; |
| 22 } |
| 23 if (options.rest.isEmpty) { |
| 24 _formatStdin(options); |
| 25 } else { |
| 26 _formatFiles(options.rest); |
| 27 } |
| 28 } |
| 29 |
| 30 _formatFiles(files) { |
| 31 for (var file in files) { |
| 32 _formatFile(file); |
| 33 } |
| 34 } |
| 35 |
| 36 _formatFile(path) { |
| 37 var buffer = new StringBuffer(); |
| 38 var file = new File(path); |
| 39 file.openRead() |
| 40 .transform(new StringDecoder()) |
| 41 .listen((data) => buffer.write(data), |
| 42 onError: (error) => print('Error, could not open "$path"'), |
| 43 onDone: () => print(_formatCU(buffer.toString()))); |
| 44 } |
| 45 |
| 46 _formatStdin(options) { |
| 47 _log('not supported yet!'); |
| 48 // stdin.transform(new StringDecoder()) |
| 49 // .listen((String data) => print(data), |
| 50 // onError: (error) => print('Error reading from stdin'), |
| 51 // onDone: () => print('Finished reading data')); |
| 52 } |
| 53 |
| 54 /// Initialize the arg parser instance. |
| 55 ArgParser _initArgParser() { |
| 56 // NOTE: these flags are placeholders only! |
| 57 var parser = new ArgParser(); |
| 58 parser.addFlag('write', abbr: 'w', negatable: false, |
| 59 help: 'Write reformatted sources to files (overwriting contents). ' |
| 60 'Do not print reformatted sources to standard output.'); |
| 61 parser.addFlag('help', abbr: 'h', negatable: false, |
| 62 help: 'Print this usage information.'); |
| 63 return parser; |
| 64 } |
| 65 |
| 66 |
| 67 /// Displays usage information. |
| 68 _printUsage() { |
| 69 var buffer = new StringBuffer(); |
| 70 buffer..write('$BINARY_NAME formats Dart programs.') |
| 71 ..write('\n\n') |
| 72 ..write('Without an explicit path, $BINARY_NAME processes the standard ' |
| 73 'input. Given a file, it operates on that file; given a ' |
| 74 'directory, it operates on all .dart files in that directory, ' |
| 75 'recursively. (Files starting with a period are ignored.) By ' |
| 76 'default, $BINARY_NAME prints the reformatted sources to ' |
| 77 'standard output.') |
| 78 ..write('\n\n') |
| 79 ..write('Supported flags are:') |
| 80 ..write('Usage: $BINARY_NAME [flags] [path...]\n\n') |
| 81 ..write('${argParser.getUsage()}\n\n'); |
| 82 _log(buffer.toString()); |
| 83 } |
| 84 |
| 85 /// Format the given [src] as a compilation unit. |
| 86 String _formatCU(src, {options: const FormatterOptions()}) => |
| 87 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); |
| 88 |
| 89 /// Log the given [msg]. |
| 90 _log(String msg) { |
| 91 //TODO(pquitslund): add proper log support |
| 92 print(msg); |
| 93 } |
| OLD | NEW |