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:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'package:args/args.dart'; | 9 import 'package:args/args.dart'; |
| 10 import 'package:path/path.dart' as path; | |
|
Bob Nystrom
2013/08/28 18:18:55
Don't forget to add path to your pubspec.yaml too
pquitslund
2013/08/28 20:29:13
Already there! :)
| |
| 10 | 11 |
| 11 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; | 12 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; |
| 12 | 13 |
| 13 | 14 |
| 14 const BINARY_NAME = 'dartfmt'; | 15 const BINARY_NAME = 'dartfmt'; |
| 15 const DART_EXT = '.dart'; | 16 final DART_FILE_REGEXP = new RegExp(r'^[^.].*\.dart$', caseSensitive: false); |
|
Bob Nystrom
2013/08/28 18:18:55
Should be "dartFileRegExp" now since it isn't cons
pquitslund
2013/08/28 20:29:13
Done.
| |
| 16 final argParser = _initArgParser(); | 17 final argParser = _initArgParser(); |
| 17 | 18 |
| 18 void main() { | 19 void main() { |
| 19 var options = argParser.parse(new Options().arguments); | 20 var options = argParser.parse(new Options().arguments); |
| 20 if (options['help']) { | 21 if (options['help']) { |
| 21 _printUsage(); | 22 _printUsage(); |
| 22 return; | 23 return; |
| 23 } | 24 } |
| 24 if (options.rest.isEmpty) { | 25 if (options.rest.isEmpty) { |
| 25 _formatStdin(options); | 26 _formatStdin(options); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 44 } else if (resource is File) { | 45 } else if (resource is File) { |
| 45 _formatFile(resource); | 46 _formatFile(resource); |
| 46 } | 47 } |
| 47 } | 48 } |
| 48 | 49 |
| 49 _formatDirectory(dir) => | 50 _formatDirectory(dir) => |
| 50 dir.listSync().forEach((resource) => _formatResource(resource)); | 51 dir.listSync().forEach((resource) => _formatResource(resource)); |
| 51 | 52 |
| 52 | 53 |
| 53 void _formatFile(file) { | 54 void _formatFile(file) { |
| 54 Uri.parse(file).pathSegments.last; | |
| 55 print(file.path); | |
| 56 if (_isDartFile(file)) { | 55 if (_isDartFile(file)) { |
| 57 try { | 56 try { |
| 58 var buffer = new StringBuffer(); | 57 var buffer = new StringBuffer(); |
| 59 var rawSource = file.readAsStringSync(); | 58 var rawSource = file.readAsStringSync(); |
| 60 var formatted = _formatCU(rawSource); | 59 var formatted = _formatCU(rawSource); |
| 61 print(formatted); | 60 print(formatted); |
| 62 file.writeAsStringSync(formatted); | 61 file.writeAsStringSync(formatted); |
| 63 } catch (e) { | 62 } catch (e) { |
| 64 _log('Error formatting "${file.path}": $e'); | 63 _log('Error formatting "${file.path}": $e'); |
| 65 } | 64 } |
| 66 } | 65 } |
| 67 } | 66 } |
| 68 | 67 |
| 69 //TODO(pquitslund): fix this using 'path' | 68 _isDartFile(file) => DART_FILE_REGEXP.hasMatch(path.basename(file.path)); |
| 70 _isDartFile(file) => !file.path.startsWith('.') && file.path.endsWith(DART_EXT); | |
| 71 | 69 |
| 72 _formatStdin(options) { | 70 _formatStdin(options) { |
| 73 _log('not supported yet!'); | 71 _log('not supported yet!'); |
| 74 // stdin.transform(new StringDecoder()) | 72 // stdin.transform(new StringDecoder()) |
| 75 // .listen((String data) => print(data), | 73 // .listen((String data) => print(data), |
| 76 // onError: (error) => print('Error reading from stdin'), | 74 // onError: (error) => print('Error reading from stdin'), |
| 77 // onDone: () => print('Finished reading data')); | 75 // onDone: () => print('Finished reading data')); |
| 78 } | 76 } |
| 79 | 77 |
| 80 /// Initialize the arg parser instance. | 78 /// Initialize the arg parser instance. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 110 | 108 |
| 111 /// Format the given [src] as a compilation unit. | 109 /// Format the given [src] as a compilation unit. |
| 112 String _formatCU(src, {options: const FormatterOptions()}) => | 110 String _formatCU(src, {options: const FormatterOptions()}) => |
| 113 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); | 111 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); |
| 114 | 112 |
| 115 /// Log the given [msg]. | 113 /// Log the given [msg]. |
| 116 _log(String msg) { | 114 _log(String msg) { |
| 117 //TODO(pquitslund): add proper log support | 115 //TODO(pquitslund): add proper log support |
| 118 print(msg); | 116 print(msg); |
| 119 } | 117 } |
| OLD | NEW |