analyze_base.dart 2.26 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2015 The Chromium Authors. 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:async';

import 'package:args/args.dart';

9
import '../base/file_system.dart';
10 11 12 13 14 15 16 17 18 19 20 21
import '../base/utils.dart';
import '../cache.dart';
import '../globals.dart';

/// Common behavior for `flutter analyze` and `flutter analyze --watch`
abstract class AnalyzeBase {
  /// The parsed argument results for execution.
  final ArgResults argResults;

  AnalyzeBase(this.argResults);

  /// Called by [AnalyzeCommand] to start the analysis process.
22
  Future<Null> analyze();
23 24 25 26

  void dumpErrors(Iterable<String> errors) {
    if (argResults['write'] != null) {
      try {
27
        final RandomAccessFile resultsFile = fs.file(argResults['write']).openSync(mode: FileMode.WRITE);
28 29 30 31 32 33 34 35 36 37 38 39 40 41
        try {
          resultsFile.lockSync();
          resultsFile.writeStringSync(errors.join('\n'));
        } finally {
          resultsFile.close();
        }
      } catch (e) {
        printError('Failed to save output to "${argResults['write']}": $e');
      }
    }
  }

  void writeBenchmark(Stopwatch stopwatch, int errorCount, int membersMissingDocumentation) {
    final String benchmarkOut = 'analysis_benchmark.json';
42
    final Map<String, dynamic> data = <String, dynamic>{
43 44 45 46
      'time': (stopwatch.elapsedMilliseconds / 1000.0),
      'issues': errorCount,
      'missingDartDocs': membersMissingDocumentation
    };
47
    fs.file(benchmarkOut).writeAsStringSync(toPrettyJson(data));
48 49 50 51 52 53 54 55 56 57
    printStatus('Analysis benchmark written to $benchmarkOut ($data).');
  }

  bool get isBenchmarking => argResults['benchmark'];
}

/// Return `true` if [fileList] contains a path that resides inside the Flutter repository.
/// If [fileList] is empty, then return `true` if the current directory resides inside the Flutter repository.
bool inRepo(List<String> fileList) {
  if (fileList == null || fileList.isEmpty)
58
    fileList = <String>[fs.path.current];
59 60
  final String root = fs.path.normalize(fs.path.absolute(Cache.flutterRoot));
  final String prefix = root + fs.path.separator;
61
  for (String file in fileList) {
62
    file = fs.path.normalize(fs.path.absolute(file));
63 64 65 66
    if (file == root || file.startsWith(prefix))
      return true;
  }
  return false;
67
}