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 'exceptions.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 | 1 | bool processDirectory(FormatterOptions options, Directory directory) {
|
22 | 3 | options.reporter.showDirectory(directory.path);
|
23 | |
|
24 | | var success = true;
|
25 | 1 | var shownHiddenPaths = new Set<String>();
|
26 | |
|
27 | 1 | for (var entry in directory.listSync(
|
28 | 1 | recursive: true, followLinks: options.followLinks)) {
|
29 | 3 | var relative = p.relative(entry.path, from: directory.path);
|
30 | |
|
31 | 1 | if (entry is Link) {
|
32 | 2 | options.reporter.showSkippedLink(relative);
|
33 | | continue;
|
34 | | }
|
35 | |
|
36 | 3 | if (entry is! File || !entry.path.endsWith(".dart")) continue;
|
37 | |
|
38 | | // If the path is in a subdirectory starting with ".", ignore it.
|
39 | 1 | var parts = p.split(relative);
|
40 | | var hiddenIndex;
|
41 | 3 | for (var i = 0; i < parts.length; i++) {
|
42 | 2 | 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 | 3 | var hiddenPath = p.joinAll(parts.take(hiddenIndex + 1));
|
52 | 1 | if (shownHiddenPaths.add(hiddenPath)) {
|
53 | 2 | options.reporter.showHiddenPath(hiddenPath);
|
54 | | }
|
55 | | continue;
|
56 | | }
|
57 | |
|
58 | 1 | if (!processFile(options, entry, label: relative)) success = false;
|
59 | | }
|
60 | |
|
61 | | return success;
|
62 | | }
|
63 | |
|
64 | | /// Runs the formatter on [file].
|
65 | | ///
|
66 | | /// Returns `true` if successful or `false` if an error occurred.
|
67 | 1 | bool processFile(FormatterOptions options, File file, {String label}) {
|
68 | 1 | if (label == null) label = file.path;
|
69 | |
|
70 | 1 | var formatter = new DartFormatter(
|
71 | 1 | indent: options.indent,
|
72 | 1 | pageWidth: options.pageWidth,
|
73 | 1 | fixes: options.fixes);
|
74 | | try {
|
75 | 3 | var source = new SourceCode(file.readAsStringSync(), uri: file.path);
|
76 | 2 | options.reporter.beforeFile(file, label);
|
77 | 1 | var output = formatter.formatSource(source);
|
78 | 1 | options.reporter
|
79 | 4 | .afterFile(file, label, output, changed: source.text != output.text);
|
80 | | return true;
|
81 | 0 | } on FormatterException catch (err) {
|
82 | 0 | var color = Platform.operatingSystem != "windows" &&
|
83 | 0 | stdioType(stderr) == StdioType.terminal;
|
84 | |
|
85 | 0 | stderr.writeln(err.message(color: color));
|
86 | 0 | } on UnexpectedOutputException catch (err) {
|
87 | 0 | stderr.writeln('''Hit a bug in the formatter when formatting $label.
|
88 | | $err
|
89 | 0 | Please report at github.com/dart-lang/dart_style/issues.''');
|
90 | | } catch (err, stack) {
|
91 | 0 | stderr.writeln('''Hit a bug in the formatter when formatting $label.
|
92 | | Please report at github.com/dart-lang/dart_style/issues.
|
93 | | $err
|
94 | 0 | $stack''');
|
95 | | }
|
96 | |
|
97 | | return false;
|
98 | | }
|