Chromium Code Reviews| Index: pkg/analyzer_experimental/bin/formatter.dart |
| =================================================================== |
| --- pkg/analyzer_experimental/bin/formatter.dart (revision 26740) |
| +++ pkg/analyzer_experimental/bin/formatter.dart (working copy) |
| @@ -4,7 +4,6 @@ |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| -import 'dart:convert'; |
| import 'dart:io'; |
| import 'package:args/args.dart'; |
| @@ -13,6 +12,7 @@ |
| const BINARY_NAME = 'dartfmt'; |
| +const DART_EXT = '.dart'; |
| final argParser = _initArgParser(); |
| void main() { |
| @@ -24,26 +24,48 @@ |
| if (options.rest.isEmpty) { |
| _formatStdin(options); |
| } else { |
| - _formatFiles(options.rest); |
| + _formatPaths(options.rest); |
| } |
| } |
| -_formatFiles(files) { |
| - for (var file in files) { |
| - _formatFile(file); |
| +_formatPaths(paths) { |
| + paths.forEach((path) { |
| + if (FileSystemEntity.isDirectorySync(path)) { |
| + _formatDirectory(new Directory(path)); |
| + } else { |
| + _formatFile(new File(path)); |
| + } |
| + }); |
| +} |
| + |
| +_formatResource(resource) { |
| + if (resource is Directory) { |
| + _formatDirectory(resource); |
| + } else if (resource is File) { |
| + _formatFile(resource); |
| } |
| } |
| -_formatFile(path) { |
| - var buffer = new StringBuffer(); |
| - var file = new File(path); |
| - file.openRead() |
| - .transform(UTF8.decoder) |
| - .listen((data) => buffer.write(data), |
| - onError: (error) => print('Error, could not open "$path"'), |
| - onDone: () => print(_formatCU(buffer.toString()))); |
| +_formatDirectory(dir) => |
| + dir.listSync().forEach((resource) => _formatResource(resource)); |
| + |
| + |
| +void _formatFile(file) { |
| + if (_isDartFile(file)) { |
| + try { |
| + var buffer = new StringBuffer(); |
| + var rawSource = file.readAsStringSync(); |
| + var formatted = _formatCU(rawSource); |
| + print(formatted); |
| + file.writeAsStringSync(formatted); |
| + } catch (e) { |
| + _log('Error formatting "${file.path}": $e'); |
| + } |
| + } |
| } |
| +_isDartFile(file) => !file.path.startsWith('.') && file.path.endsWith(DART_EXT); |
|
scheglov
2013/08/27 23:17:20
Ignore case?
|
| + |
| _formatStdin(options) { |
| _log('not supported yet!'); |
| // stdin.transform(new StringDecoder()) |