Coverage report for lib/src/io.dart

Line coverage: 27 / 36 (75.0%)

All files > lib/src/io.dart

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.
211
bool processDirectory(FormatterOptions options, Directory directory) {
223
  options.reporter.showDirectory(directory.path);
23
24
  var success = true;
251
  var shownHiddenPaths = new Set<String>();
26
271
  for (var entry in directory.listSync(
281
      recursive: true, followLinks: options.followLinks)) {
293
    var relative = p.relative(entry.path, from: directory.path);
30
311
    if (entry is Link) {
322
      options.reporter.showSkippedLink(relative);
33
      continue;
34
    }
35
363
    if (entry is! File || !entry.path.endsWith(".dart")) continue;
37
38
    // If the path is in a subdirectory starting with ".", ignore it.
391
    var parts = p.split(relative);
40
    var hiddenIndex;
413
    for (var i = 0; i < parts.length; i++) {
422
      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.
513
      var hiddenPath = p.joinAll(parts.take(hiddenIndex + 1));
521
      if (shownHiddenPaths.add(hiddenPath)) {
532
        options.reporter.showHiddenPath(hiddenPath);
54
      }
55
      continue;
56
    }
57
581
    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.
671
bool processFile(FormatterOptions options, File file, {String label}) {
681
  if (label == null) label = file.path;
69
701
  var formatter = new DartFormatter(
711
      indent: options.indent,
721
      pageWidth: options.pageWidth,
731
      fixes: options.fixes);
74
  try {
753
    var source = new SourceCode(file.readAsStringSync(), uri: file.path);
762
    options.reporter.beforeFile(file, label);
771
    var output = formatter.formatSource(source);
781
    options.reporter
794
        .afterFile(file, label, output, changed: source.text != output.text);
80
    return true;
810
  } on FormatterException catch (err) {
820
    var color = Platform.operatingSystem != "windows" &&
830
        stdioType(stderr) == StdioType.terminal;
84
850
    stderr.writeln(err.message(color: color));
860
  } on UnexpectedOutputException catch (err) {
870
    stderr.writeln('''Hit a bug in the formatter when formatting $label.
88
$err
890
Please report at github.com/dart-lang/dart_style/issues.''');
90
  } catch (err, stack) {
910
    stderr.writeln('''Hit a bug in the formatter when formatting $label.
92
Please report at github.com/dart-lang/dart_style/issues.
93
$err
940
$stack''');
95
  }
96
97
  return false;
98
}