OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library dart_style.src.io; | 5 library dart_style.src.io; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:path/path.dart' as p; | 9 import 'package:path/path.dart' as p; |
10 | 10 |
11 import 'dart_formatter.dart'; | 11 import 'dart_formatter.dart'; |
12 import 'formatter_exception.dart'; | 12 import 'formatter_exception.dart'; |
13 import 'formatter_options.dart'; | 13 import 'formatter_options.dart'; |
14 import 'source_code.dart'; | 14 import 'source_code.dart'; |
15 | 15 |
16 /// Runs the formatter on every .dart file in [path] (and its subdirectories), | 16 /// Runs the formatter on every .dart file in [path] (and its subdirectories), |
17 /// and replaces them with their formatted output. | 17 /// and replaces them with their formatted output. |
18 /// | 18 /// |
19 /// Returns `true` if successful or `false` if an error occurred in any of the | 19 /// Returns `true` if successful or `false` if an error occurred in any of the |
20 /// files. | 20 /// files. |
21 bool processDirectory(FormatterOptions options, Directory directory) { | 21 bool processDirectory(FormatterOptions options, Directory directory) { |
22 options.reporter.showDirectory(directory.path); | 22 options.reporter.showDirectory(directory.path); |
23 | 23 |
24 var success = true; | 24 var success = true; |
| 25 var shownHiddenPaths = new Set<String>(); |
| 26 |
25 for (var entry in directory.listSync( | 27 for (var entry in directory.listSync( |
26 recursive: true, followLinks: options.followLinks)) { | 28 recursive: true, followLinks: options.followLinks)) { |
27 var relative = p.relative(entry.path, from: directory.path); | 29 var relative = p.relative(entry.path, from: directory.path); |
28 | 30 |
29 if (entry is Link) { | 31 if (entry is Link) { |
30 options.reporter.showSkippedLink(relative); | 32 options.reporter.showSkippedLink(relative); |
31 continue; | 33 continue; |
32 } | 34 } |
33 | 35 |
34 if (entry is! File || !entry.path.endsWith(".dart")) continue; | 36 if (entry is! File || !entry.path.endsWith(".dart")) continue; |
35 | 37 |
36 // If the path is in a subdirectory starting with ".", ignore it. | 38 // If the path is in a subdirectory starting with ".", ignore it. |
37 if (p.split(relative).any((part) => part.startsWith("."))) { | 39 var parts = p.split(relative); |
38 options.reporter.showHiddenFile(relative); | 40 var hiddenIndex; |
| 41 for (var i = 0; i < parts.length; i++) { |
| 42 if (parts[i].startsWith(".")) { |
| 43 hiddenIndex = i; |
| 44 break; |
| 45 } |
| 46 } |
| 47 |
| 48 if (hiddenIndex != null) { |
| 49 // Since we'll hide everything inside the directory starting with ".", |
| 50 // show the directory name once instead of once for each file. |
| 51 var hiddenPath = p.joinAll(parts.take(hiddenIndex + 1)); |
| 52 if (shownHiddenPaths.add(hiddenPath)) { |
| 53 options.reporter.showHiddenPath(hiddenPath); |
| 54 } |
39 continue; | 55 continue; |
40 } | 56 } |
41 | 57 |
42 if (!processFile(options, entry, label: relative)) success = false; | 58 if (!processFile(options, entry, label: relative)) success = false; |
43 } | 59 } |
44 | 60 |
45 return success; | 61 return success; |
46 } | 62 } |
47 | 63 |
48 /// Runs the formatter on [file]. | 64 /// Runs the formatter on [file]. |
49 /// | 65 /// |
50 /// Returns `true` if successful or `false` if an error occurred. | 66 /// Returns `true` if successful or `false` if an error occurred. |
51 bool processFile(FormatterOptions options, File file, {String label}) { | 67 bool processFile(FormatterOptions options, File file, {String label}) { |
52 if (label == null) label = file.path; | 68 if (label == null) label = file.path; |
53 | 69 |
54 var formatter = new DartFormatter(pageWidth: options.pageWidth); | 70 var formatter = |
| 71 new DartFormatter(indent: options.indent, pageWidth: options.pageWidth); |
55 try { | 72 try { |
56 var source = new SourceCode(file.readAsStringSync(), uri: file.path); | 73 var source = new SourceCode(file.readAsStringSync(), uri: file.path); |
| 74 options.reporter.beforeFile(file, label); |
57 var output = formatter.formatSource(source); | 75 var output = formatter.formatSource(source); |
58 options.reporter | 76 options.reporter |
59 .showFile(file, label, output, changed: source.text != output.text); | 77 .afterFile(file, label, output, changed: source.text != output.text); |
60 return true; | 78 return true; |
61 } on FormatterException catch (err) { | 79 } on FormatterException catch (err) { |
62 var color = Platform.operatingSystem != "windows" && | 80 var color = Platform.operatingSystem != "windows" && |
63 stdioType(stderr) == StdioType.TERMINAL; | 81 stdioType(stderr) == StdioType.TERMINAL; |
64 | 82 |
65 stderr.writeln(err.message(color: color)); | 83 stderr.writeln(err.message(color: color)); |
66 } catch (err, stack) { | 84 } catch (err, stack) { |
67 stderr.writeln('''Hit a bug in the formatter when formatting $label. | 85 stderr.writeln('''Hit a bug in the formatter when formatting $label. |
68 Please report at: github.com/dart-lang/dart_style/issues | 86 Please report at: github.com/dart-lang/dart_style/issues |
69 $err | 87 $err |
70 $stack'''); | 88 $stack'''); |
71 } | 89 } |
72 | 90 |
73 return false; | 91 return false; |
74 } | 92 } |
OLD | NEW |