| OLD | NEW | 
 |    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 | 
 |    3 // BSD-style license that can be found in the LICENSE file. | 
 |    4  | 
 |    5 import 'dart:convert'; | 
|    1 import 'dart:io'; |    6 import 'dart:io'; | 
|    2  |    7  | 
|    3 import 'package:args/args.dart'; |    8 import 'package:args/args.dart'; | 
 |    9 import 'package:dart_style/src/dart_formatter.dart'; | 
 |   10 import 'package:dart_style/src/formatter_exception.dart'; | 
|    4 import 'package:dart_style/src/formatter_options.dart'; |   11 import 'package:dart_style/src/formatter_options.dart'; | 
|    5 import 'package:dart_style/src/io.dart'; |   12 import 'package:dart_style/src/io.dart'; | 
|    6  |   13  | 
|    7 void main(List<String> args) { |   14 void main(List<String> args) { | 
|    8   var parser = new ArgParser(allowTrailingOptions: true); |   15   var parser = new ArgParser(allowTrailingOptions: true); | 
|    9  |   16  | 
|   10   parser.addFlag("help", abbr: "h", negatable: false, |   17   parser.addFlag("help", abbr: "h", negatable: false, | 
|   11       help: "Shows usage information."); |   18       help: "Shows usage information."); | 
|   12   parser.addOption("line-length", abbr: "l", |   19   parser.addOption("line-length", abbr: "l", | 
|   13       help: "Wrap lines longer than this.", |   20       help: "Wrap lines longer than this.", | 
|   14       defaultsTo: "80"); |   21       defaultsTo: "80"); | 
|   15   parser.addFlag("dry-run", abbr: "n", negatable: false, |   22   parser.addFlag("dry-run", abbr: "n", negatable: false, | 
|   16       help: "Show which files would be modified but make no changes."); |   23       help: "Show which files would be modified but make no changes."); | 
|   17   parser.addFlag("overwrite", abbr: "w", negatable: false, |   24   parser.addFlag("overwrite", abbr: "w", negatable: false, | 
|   18       help: "Overwrite input files with formatted output.\n" |   25       help: "Overwrite input files with formatted output."); | 
|   19             "If unset, prints results to standard output."); |   26   parser.addFlag("machine", abbr: "m", negatable: false, | 
 |   27       help: "Produce machine-readable JSON output."); | 
|   20   parser.addFlag("follow-links", negatable: false, |   28   parser.addFlag("follow-links", negatable: false, | 
|   21       help: "Follow links to files and directories.\n" |   29       help: "Follow links to files and directories.\n" | 
|   22             "If unset, links will be ignored."); |   30             "If unset, links will be ignored."); | 
|   23   parser.addFlag("machine", abbr: "m", negatable: false, |  | 
|   24       help: "Produce machine-readable JSON output."); |  | 
|   25   parser.addFlag("transform", abbr: "t", negatable: false, |   31   parser.addFlag("transform", abbr: "t", negatable: false, | 
|   26       help: "Unused flag for compability with the old formatter."); |   32       help: "Unused flag for compability with the old formatter."); | 
|   27  |   33  | 
|   28   var argResults; |   34   var argResults; | 
|   29   try { |   35   try { | 
|   30     argResults = parser.parse(args); |   36     argResults = parser.parse(args); | 
|   31   } on FormatException catch (err) { |   37   } on FormatException catch (err) { | 
|   32     printUsage(parser, err.message); |   38     printUsage(parser, err.message); | 
|   33     exitCode = 64; |   39     exitCode = 64; | 
|   34     return; |   40     return; | 
|   35   } |   41   } | 
|   36  |   42  | 
|   37   if (argResults["help"]) { |   43   if (argResults["help"]) { | 
|   38     printUsage(parser); |   44     printUsage(parser); | 
|   39     return; |   45     return; | 
|   40   } |   46   } | 
|   41  |   47  | 
|   42   if (argResults["dry-run"] && argResults["overwrite"]) { |   48   if (argResults["dry-run"] && argResults["overwrite"]) { | 
|   43     printUsage(parser, |   49     printUsage(parser, | 
|   44         "Cannot use --dry-run and --overwrite at the same time."); |   50         "Cannot use --dry-run and --overwrite at the same time."); | 
|   45     exitCode = 64; |   51     exitCode = 64; | 
|   46     return; |   52     return; | 
|   47   } |   53   } | 
|   48  |   54  | 
|   49   checkForReporterCollision(String chosen, String other) { |   55   checkForReporterCollision(String chosen, String other) { | 
|   50     if (argResults[other]) { |   56     if (!argResults[other]) return false; | 
|   51       printUsage(parser, |   57  | 
|   52           "Cannot use --$chosen and --$other at the same time."); |   58     printUsage(parser, | 
|   53       exitCode = 64; |   59         "Cannot use --$chosen and --$other at the same time."); | 
|   54       return; |   60     exitCode = 64; | 
|   55     } |   61     return true; | 
|   56   } |   62   } | 
|   57  |   63  | 
|   58   var reporter = OutputReporter.print; |   64   var reporter = OutputReporter.print; | 
|   59   if (argResults["dry-run"]) { |   65   if (argResults["dry-run"]) { | 
|   60     checkForReporterCollision("dry-run", "overwrite"); |   66     if (checkForReporterCollision("dry-run", "overwrite")) return; | 
|   61     checkForReporterCollision("dry-run", "machine"); |   67     if (checkForReporterCollision("dry-run", "machine")) return; | 
|   62  |   68  | 
|   63     reporter = OutputReporter.dryRun; |   69     reporter = OutputReporter.dryRun; | 
|   64   } else if (argResults["overwrite"]) { |   70   } else if (argResults["overwrite"]) { | 
|   65     checkForReporterCollision("overwrite", "machine"); |   71     if (checkForReporterCollision("overwrite", "machine")) return; | 
 |   72  | 
 |   73     if (argResults.rest.isEmpty) { | 
 |   74       printUsage(parser, | 
 |   75           "Cannot use --overwrite without providing any paths to format."); | 
 |   76       exitCode = 64; | 
 |   77       return; | 
 |   78     } | 
|   66  |   79  | 
|   67     reporter = OutputReporter.overwrite; |   80     reporter = OutputReporter.overwrite; | 
|   68   } else if (argResults["machine"]) { |   81   } else if (argResults["machine"]) { | 
|   69     reporter = OutputReporter.printJson; |   82     reporter = OutputReporter.printJson; | 
|   70   } |   83   } | 
|   71  |   84  | 
|   72   var pageWidth; |   85   var pageWidth; | 
|   73  |   86  | 
|   74   try { |   87   try { | 
|   75     pageWidth = int.parse(argResults["line-length"]); |   88     pageWidth = int.parse(argResults["line-length"]); | 
|   76   } on FormatException catch (_) { |   89   } on FormatException catch (_) { | 
|   77     printUsage(parser, '--line-length must be an integer, was ' |   90     printUsage(parser, '--line-length must be an integer, was ' | 
|   78                        '"${argResults['line-length']}".'); |   91                        '"${argResults['line-length']}".'); | 
|   79     exitCode = 64; |   92     exitCode = 64; | 
|   80     return; |   93     return; | 
|   81   } |   94   } | 
|   82  |   95  | 
|   83   var followLinks = argResults["follow-links"]; |   96   var followLinks = argResults["follow-links"]; | 
|   84  |   97  | 
|   85   if (argResults.rest.isEmpty) { |  | 
|   86     printUsage(parser, |  | 
|   87         "Please provide at least one directory or file to format."); |  | 
|   88     exitCode = 64; |  | 
|   89     return; |  | 
|   90   } |  | 
|   91  |  | 
|   92   var options = new FormatterOptions(reporter, |   98   var options = new FormatterOptions(reporter, | 
|   93       pageWidth: pageWidth, followLinks: followLinks); |   99       pageWidth: pageWidth, followLinks: followLinks); | 
|   94  |  100  | 
|   95   for (var path in argResults.rest) { |  101   if (argResults.rest.isEmpty) { | 
 |  102     formatStdin(options); | 
 |  103   } else { | 
 |  104     formatPaths(options, argResults.rest); | 
 |  105   } | 
 |  106 } | 
 |  107  | 
 |  108 /// Reads input from stdin until it's closed, and the formats it. | 
 |  109 void formatStdin(FormatterOptions options) { | 
 |  110   var input = new StringBuffer(); | 
 |  111   stdin.transform(new Utf8Decoder()).listen(input.write, onDone: () { | 
 |  112     var formatter = new DartFormatter(pageWidth: options.pageWidth); | 
 |  113     try { | 
 |  114       var source = input.toString(); | 
 |  115       var output = formatter.format(source, uri: "stdin"); | 
 |  116       options.reporter.showFile(null, "<stdin>", output, | 
 |  117           changed: source != output); | 
 |  118       return true; | 
 |  119     } on FormatterException catch (err) { | 
 |  120       stderr.writeln(err.message()); | 
 |  121     } catch (err, stack) { | 
 |  122       stderr.writeln('''Hit a bug in the formatter when formatting stdin. | 
 |  123 Please report at: github.com/dart-lang/dart_style/issues | 
 |  124 $err | 
 |  125 $stack'''); | 
 |  126     } | 
 |  127   }); | 
 |  128 } | 
 |  129  | 
 |  130 /// Formats all of the files and directories given by [paths]. | 
 |  131 void formatPaths(FormatterOptions options, List<String> paths) { | 
 |  132   for (var path in paths) { | 
|   96     var directory = new Directory(path); |  133     var directory = new Directory(path); | 
|   97     if (directory.existsSync()) { |  134     if (directory.existsSync()) { | 
|   98       if (!processDirectory(options, directory)) { |  135       if (!processDirectory(options, directory)) { | 
|   99         exitCode = 65; |  136         exitCode = 65; | 
|  100       } |  137       } | 
|  101       continue; |  138       continue; | 
|  102     } |  139     } | 
|  103  |  140  | 
|  104     var file = new File(path); |  141     var file = new File(path); | 
|  105     if (file.existsSync()) { |  142     if (file.existsSync()) { | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
|  116   var output = stdout; |  153   var output = stdout; | 
|  117  |  154  | 
|  118   var message = "Reformats whitespace in Dart source files."; |  155   var message = "Reformats whitespace in Dart source files."; | 
|  119   if (error != null) { |  156   if (error != null) { | 
|  120     message = error; |  157     message = error; | 
|  121     output = stdout; |  158     output = stdout; | 
|  122   } |  159   } | 
|  123  |  160  | 
|  124   output.write("""$message |  161   output.write("""$message | 
|  125  |  162  | 
|  126 Usage: dartformat [-w] <files or directories...> |  163 Usage: dartformat [-n|-w] [files or directories...] | 
|  127  |  164  | 
|  128 ${parser.usage} |  165 ${parser.usage} | 
|  129 """); |  166 """); | 
|  130 } |  167 } | 
| OLD | NEW |