| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:convert'; | 5 import 'dart:convert'; |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 | 7 |
| 8 import 'package:args/args.dart'; | 8 import 'package:args/args.dart'; |
| 9 | 9 |
| 10 import 'package:dart_style/src/dart_formatter.dart'; | 10 import 'package:dart_style/src/dart_formatter.dart'; |
| 11 import 'package:dart_style/src/formatter_exception.dart'; | 11 import 'package:dart_style/src/formatter_exception.dart'; |
| 12 import 'package:dart_style/src/formatter_options.dart'; | 12 import 'package:dart_style/src/formatter_options.dart'; |
| 13 import 'package:dart_style/src/io.dart'; | 13 import 'package:dart_style/src/io.dart'; |
| 14 import 'package:dart_style/src/source_code.dart'; | 14 import 'package:dart_style/src/source_code.dart'; |
| 15 | 15 |
| 16 // Note: The following line of code is modified by tool/grind.dart. | 16 // Note: The following line of code is modified by tool/grind.dart. |
| 17 const version = "0.2.1"; | 17 const version = "0.2.16"; |
| 18 | 18 |
| 19 void main(List<String> args) { | 19 void main(List<String> args) { |
| 20 var parser = new ArgParser(allowTrailingOptions: true); | 20 var parser = new ArgParser(allowTrailingOptions: true); |
| 21 | 21 |
| 22 parser.addFlag("help", | 22 parser.addFlag("help", |
| 23 abbr: "h", negatable: false, help: "Shows usage information."); | 23 abbr: "h", negatable: false, help: "Shows usage information."); |
| 24 parser.addFlag("version", | 24 parser.addFlag("version", |
| 25 negatable: false, help: "Shows version information."); | 25 negatable: false, help: "Shows version information."); |
| 26 parser.addOption("line-length", | 26 parser.addOption("line-length", |
| 27 abbr: "l", help: "Wrap lines longer than this.", defaultsTo: "80"); | 27 abbr: "l", help: "Wrap lines longer than this.", defaultsTo: "80"); |
| 28 parser.addOption("indent", | 28 parser.addOption("indent", |
| 29 abbr: "i", help: "Spaces of leading indentation.", defaultsTo: "0"); | 29 abbr: "i", help: "Spaces of leading indentation.", defaultsTo: "0"); |
| 30 parser.addOption("preserve", | 30 parser.addOption("preserve", |
| 31 help: 'Selection to preserve, formatted as "start:length".'); | 31 help: 'Selection to preserve, formatted as "start:length".'); |
| 32 parser.addFlag("dry-run", | 32 parser.addFlag("dry-run", |
| 33 abbr: "n", | 33 abbr: "n", |
| 34 negatable: false, | 34 negatable: false, |
| 35 help: "Show which files would be modified but make no changes."); | 35 help: "Show which files would be modified but make no changes."); |
| 36 parser.addFlag("set-exit-if-changed", |
| 37 negatable: false, |
| 38 help: "Return exit code 1 if there are any formatting changes."); |
| 36 parser.addFlag("overwrite", | 39 parser.addFlag("overwrite", |
| 37 abbr: "w", | 40 abbr: "w", |
| 38 negatable: false, | 41 negatable: false, |
| 39 help: "Overwrite input files with formatted output."); | 42 help: "Overwrite input files with formatted output."); |
| 40 parser.addFlag("machine", | 43 parser.addFlag("machine", |
| 41 abbr: "m", | 44 abbr: "m", |
| 42 negatable: false, | 45 negatable: false, |
| 43 help: "Produce machine-readable JSON output."); | 46 help: "Produce machine-readable JSON output."); |
| 44 parser.addFlag("profile", | 47 parser.addFlag("profile", |
| 45 negatable: false, help: "Display profile times after running."); | 48 negatable: false, help: "Display profile times after running."); |
| 46 parser.addFlag("follow-links", | 49 parser.addFlag("follow-links", |
| 47 negatable: false, | 50 negatable: false, |
| 48 help: "Follow links to files and directories.\n" | 51 help: "Follow links to files and directories.\n" |
| 49 "If unset, links will be ignored."); | 52 "If unset, links will be ignored."); |
| 50 parser.addFlag("transform", | 53 parser.addFlag("transform", |
| 51 abbr: "t", | 54 abbr: "t", |
| 52 negatable: false, | 55 negatable: false, |
| 53 help: "Unused flag for compability with the old formatter."); | 56 help: "Unused flag for compability with the old formatter."); |
| 54 | 57 |
| 55 var argResults; | 58 ArgResults argResults; |
| 56 try { | 59 try { |
| 57 argResults = parser.parse(args); | 60 argResults = parser.parse(args); |
| 58 } on FormatException catch (err) { | 61 } on FormatException catch (err) { |
| 59 usageError(parser, err.message); | 62 usageError(parser, err.message); |
| 60 } | 63 } |
| 61 | 64 |
| 62 if (argResults["help"]) { | 65 if (argResults["help"]) { |
| 63 printUsage(parser); | 66 printUsage(parser); |
| 64 return; | 67 return; |
| 65 } | 68 } |
| 66 | 69 |
| 67 if (argResults["version"]) { | 70 if (argResults["version"]) { |
| 68 print(version); | 71 print(version); |
| 69 return; | 72 return; |
| 70 } | 73 } |
| 71 | 74 |
| 72 // Can only preserve a selection when parsing from stdin. | 75 // Can only preserve a selection when parsing from stdin. |
| 73 var selection; | 76 List<int> selection; |
| 74 | 77 |
| 75 if (argResults["preserve"] != null && argResults.rest.isNotEmpty) { | 78 if (argResults["preserve"] != null && argResults.rest.isNotEmpty) { |
| 76 usageError(parser, "Can only use --preserve when reading from stdin."); | 79 usageError(parser, "Can only use --preserve when reading from stdin."); |
| 77 } | 80 } |
| 78 | 81 |
| 79 try { | 82 try { |
| 80 selection = parseSelection(argResults["preserve"]); | 83 selection = parseSelection(argResults["preserve"]); |
| 81 } on FormatException catch (_) { | 84 } on FormatException catch (_) { |
| 82 usageError( | 85 usageError( |
| 83 parser, | 86 parser, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 112 | 115 |
| 113 reporter = OutputReporter.overwrite; | 116 reporter = OutputReporter.overwrite; |
| 114 } else if (argResults["machine"]) { | 117 } else if (argResults["machine"]) { |
| 115 reporter = OutputReporter.printJson; | 118 reporter = OutputReporter.printJson; |
| 116 } | 119 } |
| 117 | 120 |
| 118 if (argResults["profile"]) { | 121 if (argResults["profile"]) { |
| 119 reporter = new ProfileReporter(reporter); | 122 reporter = new ProfileReporter(reporter); |
| 120 } | 123 } |
| 121 | 124 |
| 125 if (argResults["set-exit-if-changed"]) { |
| 126 reporter = new SetExitReporter(reporter); |
| 127 } |
| 128 |
| 122 var pageWidth; | 129 var pageWidth; |
| 123 try { | 130 try { |
| 124 pageWidth = int.parse(argResults["line-length"]); | 131 pageWidth = int.parse(argResults["line-length"]); |
| 125 } on FormatException catch (_) { | 132 } on FormatException catch (_) { |
| 126 usageError( | 133 usageError( |
| 127 parser, | 134 parser, |
| 128 '--line-length must be an integer, was ' | 135 '--line-length must be an integer, was ' |
| 129 '"${argResults['line-length']}".'); | 136 '"${argResults['line-length']}".'); |
| 130 } | 137 } |
| 131 | 138 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 new DartFormatter(indent: options.indent, pageWidth: options.pageWidth); | 192 new DartFormatter(indent: options.indent, pageWidth: options.pageWidth); |
| 186 try { | 193 try { |
| 187 options.reporter.beforeFile(null, "<stdin>"); | 194 options.reporter.beforeFile(null, "<stdin>"); |
| 188 var source = new SourceCode(input.toString(), | 195 var source = new SourceCode(input.toString(), |
| 189 uri: "stdin", | 196 uri: "stdin", |
| 190 selectionStart: selectionStart, | 197 selectionStart: selectionStart, |
| 191 selectionLength: selectionLength); | 198 selectionLength: selectionLength); |
| 192 var output = formatter.formatSource(source); | 199 var output = formatter.formatSource(source); |
| 193 options.reporter | 200 options.reporter |
| 194 .afterFile(null, "<stdin>", output, changed: source != output); | 201 .afterFile(null, "<stdin>", output, changed: source != output); |
| 195 return true; | 202 return; |
| 196 } on FormatterException catch (err) { | 203 } on FormatterException catch (err) { |
| 197 stderr.writeln(err.message()); | 204 stderr.writeln(err.message()); |
| 198 exitCode = 65; // sysexits.h: EX_DATAERR | 205 exitCode = 65; // sysexits.h: EX_DATAERR |
| 199 } catch (err, stack) { | 206 } catch (err, stack) { |
| 200 stderr.writeln('''Hit a bug in the formatter when formatting stdin. | 207 stderr.writeln('''Hit a bug in the formatter when formatting stdin. |
| 201 Please report at: github.com/dart-lang/dart_style/issues | 208 Please report at: github.com/dart-lang/dart_style/issues |
| 202 $err | 209 $err |
| 203 $stack'''); | 210 $stack'''); |
| 204 exitCode = 70; // sysexits.h: EX_SOFTWARE | 211 exitCode = 70; // sysexits.h: EX_SOFTWARE |
| 205 } | 212 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 output = stdout; | 250 output = stdout; |
| 244 } | 251 } |
| 245 | 252 |
| 246 output.write("""$message | 253 output.write("""$message |
| 247 | 254 |
| 248 Usage: dartfmt [-n|-w] [files or directories...] | 255 Usage: dartfmt [-n|-w] [files or directories...] |
| 249 | 256 |
| 250 ${parser.usage} | 257 ${parser.usage} |
| 251 """); | 258 """); |
| 252 } | 259 } |
| OLD | NEW |