OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library dart_style.src.io; | |
6 | |
7 import 'dart:io'; | |
8 | |
9 import 'package:path/path.dart' as p; | |
10 | |
11 import 'dart_formatter.dart'; | |
12 import 'formatter_exception.dart'; | |
13 import 'formatter_options.dart'; | |
14 import 'source_code.dart'; | |
15 | |
16 /// Runs the formatter on every .dart file in [path] (and its subdirectories), | |
17 /// and replaces them with their formatted output. | |
18 /// | |
19 /// Returns `true` if successful or `false` if an error occurred in any of the | |
20 /// files. | |
21 bool processDirectory(FormatterOptions options, Directory directory) { | |
22 options.reporter.showDirectory(directory.path); | |
23 | |
24 var success = true; | |
25 for (var entry in directory.listSync( | |
26 recursive: true, followLinks: options.followLinks)) { | |
27 var relative = p.relative(entry.path, from: directory.path); | |
28 | |
29 if (entry is Link) { | |
30 options.reporter.showSkippedLink(relative); | |
31 continue; | |
32 } | |
33 | |
34 if (entry is! File || !entry.path.endsWith(".dart")) continue; | |
35 | |
36 // If the path is in a subdirectory starting with ".", ignore it. | |
37 if (p.split(relative).any((part) => part.startsWith("."))) { | |
38 options.reporter.showHiddenFile(relative); | |
39 continue; | |
40 } | |
41 | |
42 if (!processFile(options, entry, label: relative)) success = false; | |
43 } | |
44 | |
45 return success; | |
46 } | |
47 | |
48 /// Runs the formatter on [file]. | |
49 /// | |
50 /// Returns `true` if successful or `false` if an error occurred. | |
51 bool processFile(FormatterOptions options, File file, {String label}) { | |
52 if (label == null) label = file.path; | |
53 | |
54 var formatter = new DartFormatter(pageWidth: options.pageWidth); | |
55 try { | |
56 var source = new SourceCode(file.readAsStringSync(), uri: file.path); | |
57 var output = formatter.formatSource(source); | |
58 options.reporter | |
59 .showFile(file, label, output, changed: source.text != output.text); | |
60 return true; | |
61 } on FormatterException catch (err) { | |
62 var color = Platform.operatingSystem != "windows" && | |
63 stdioType(stderr) == StdioType.TERMINAL; | |
64 | |
65 stderr.writeln(err.message(color: color)); | |
66 } catch (err, stack) { | |
67 stderr.writeln('''Hit a bug in the formatter when formatting $label. | |
68 Please report at: github.com/dart-lang/dart_style/issues | |
69 $err | |
70 $stack'''); | |
71 } | |
72 | |
73 return false; | |
74 } | |
OLD | NEW |