| 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'; | 7 import 'dart:convert'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:utf'; | 9 import 'dart:utf'; |
| 10 | 10 |
| 11 import 'package:args/args.dart'; | 11 import 'package:args/args.dart'; |
| 12 import 'package:path/path.dart' as path; | 12 import 'package:path/path.dart' as path; |
| 13 | 13 |
| 14 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; | 14 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; |
| 15 | 15 |
| 16 | 16 |
| 17 const BINARY_NAME = 'dartfmt'; | 17 const BINARY_NAME = 'dartfmt'; |
| 18 final dartFileRegExp = new RegExp(r'^[^.].*\.dart$', caseSensitive: false); | 18 final dartFileRegExp = new RegExp(r'^[^.].*\.dart$', caseSensitive: false); |
| 19 final argParser = _initArgParser(); | 19 final argParser = _initArgParser(); |
| 20 final defaultSelection = new Selection(-1, -1); | 20 final defaultSelection = new Selection(-1, -1); |
| 21 | 21 |
| 22 var formatterSettings; |
| 23 |
| 22 bool machineFormat; | 24 bool machineFormat; |
| 23 bool overwriteFileContents; | 25 bool overwriteFileContents; |
| 24 Selection selection; | 26 Selection selection; |
| 25 const followLinks = false; | 27 const followLinks = false; |
| 26 | 28 |
| 29 |
| 27 main() { | 30 main() { |
| 28 var options = argParser.parse(new Options().arguments); | 31 var options = argParser.parse(new Options().arguments); |
| 29 if (options['help']) { | 32 if (options['help']) { |
| 30 _printUsage(); | 33 _printUsage(); |
| 31 return; | 34 return; |
| 32 } | 35 } |
| 33 | 36 |
| 34 _readOptions(options); | 37 _readOptions(options); |
| 35 | 38 |
| 36 if (options.rest.isEmpty) { | 39 if (options.rest.isEmpty) { |
| 37 _formatStdin(options); | 40 _formatStdin(options); |
| 38 } else { | 41 } else { |
| 39 _formatPaths(options.rest); | 42 _formatPaths(options.rest); |
| 40 } | 43 } |
| 41 } | 44 } |
| 42 | 45 |
| 43 _readOptions(options) { | 46 _readOptions(options) { |
| 44 machineFormat = options['machine']; | 47 machineFormat = options['machine']; |
| 45 overwriteFileContents = options['write']; | 48 overwriteFileContents = options['write']; |
| 46 selection = _parseSelection(options['selection']); | 49 selection = _parseSelection(options['selection']); |
| 50 formatterSettings = |
| 51 new FormatterOptions(codeTransforms: options['transform']); |
| 47 } | 52 } |
| 48 | 53 |
| 49 Selection _parseSelection(selectionOption) { | 54 Selection _parseSelection(selectionOption) { |
| 50 if (selectionOption != null) { | 55 if (selectionOption != null) { |
| 51 var units = selectionOption.split(','); | 56 var units = selectionOption.split(','); |
| 52 if (units.length == 2) { | 57 if (units.length == 2) { |
| 53 var offset = _toInt(units[0]); | 58 var offset = _toInt(units[0]); |
| 54 var length = _toInt(units[1]); | 59 var length = _toInt(units[1]); |
| 55 if (offset != null && length != null) { | 60 if (offset != null && length != null) { |
| 56 return new Selection(offset, length); | 61 return new Selection(offset, length); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 'default, $BINARY_NAME prints the reformatted sources to ' | 148 'default, $BINARY_NAME prints the reformatted sources to ' |
| 144 'standard output.') | 149 'standard output.') |
| 145 ..write('\n\n') | 150 ..write('\n\n') |
| 146 ..write('Supported flags are:') | 151 ..write('Supported flags are:') |
| 147 ..write('Usage: $BINARY_NAME [flags] [path...]\n\n') | 152 ..write('Usage: $BINARY_NAME [flags] [path...]\n\n') |
| 148 ..write('${argParser.getUsage()}\n\n'); | 153 ..write('${argParser.getUsage()}\n\n'); |
| 149 _log(buffer.toString()); | 154 _log(buffer.toString()); |
| 150 } | 155 } |
| 151 | 156 |
| 152 /// Format the given [src] as a compilation unit. | 157 /// Format the given [src] as a compilation unit. |
| 153 String _formatCU(src, {options: const FormatterOptions()}) { | 158 String _formatCU(src) { |
| 154 var formatResult = new CodeFormatter(options).format( | 159 var formatResult = new CodeFormatter(formatterSettings).format( |
| 155 CodeKind.COMPILATION_UNIT, src, selection: selection); | 160 CodeKind.COMPILATION_UNIT, src, selection: selection); |
| 156 if (machineFormat) { | 161 if (machineFormat) { |
| 157 if (formatResult.selection == null) { | 162 if (formatResult.selection == null) { |
| 158 formatResult.selection = defaultSelection; | 163 formatResult.selection = defaultSelection; |
| 159 } | 164 } |
| 160 return _toJson(formatResult); | 165 return _toJson(formatResult); |
| 161 } | 166 } |
| 162 return formatResult.source; | 167 return formatResult.source; |
| 163 } | 168 } |
| 164 | 169 |
| 165 _toJson(formatResult) => | 170 _toJson(formatResult) => |
| 166 // Actual JSON format TBD | 171 // Actual JSON format TBD |
| 167 JSON.encode({'source': formatResult.source, | 172 JSON.encode({'source': formatResult.source, |
| 168 'selection': { | 173 'selection': { |
| 169 'offset': formatResult.selection.offset, | 174 'offset': formatResult.selection.offset, |
| 170 'length': formatResult.selection.length | 175 'length': formatResult.selection.length |
| 171 } | 176 } |
| 172 }); | 177 }); |
| 173 | 178 |
| 174 /// Log the given [msg]. | 179 /// Log the given [msg]. |
| 175 _log(String msg) { | 180 _log(String msg) { |
| 176 //TODO(pquitslund): add proper log support | 181 //TODO(pquitslund): add proper log support |
| 177 print(msg); | 182 print(msg); |
| 178 } | 183 } |
| OLD | NEW |