analyze.dart 3.15 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4 5 6
// 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';

7
import '../base/file_system.dart';
8 9 10
import '../runner/flutter_command.dart';
import 'analyze_continuously.dart';
import 'analyze_once.dart';
Hixie's avatar
Hixie committed
11

12
class AnalyzeCommand extends FlutterCommand {
13
  AnalyzeCommand({bool verboseHelp = false, this.workingDirectory}) {
14 15 16 17 18 19 20 21 22
    argParser.addFlag('flutter-repo',
        negatable: false,
        help: 'Include all the examples and tests from the Flutter repository.',
        defaultsTo: false,
        hide: !verboseHelp);
    argParser.addFlag('current-package',
        help: 'Analyze the current project, if applicable.', defaultsTo: true);
    argParser.addFlag('dartdocs',
        negatable: false,
23
        help: 'List every public member that is lacking documentation. '
24
              '(The public_member_api_docs lint must be enabled in analysis_options.yaml)',
25 26 27 28 29 30 31
        hide: !verboseHelp);
    argParser.addFlag('watch',
        help: 'Run analysis continuously, watching the filesystem for changes.',
        negatable: false);
    argParser.addOption('write',
        valueHelp: 'file',
        help: 'Also output the results to a file. This is useful with --watch '
32
              'if you want a file to always contain the latest results.');
33 34 35 36
    argParser.addOption('dart-sdk',
        valueHelp: 'path-to-sdk',
        help: 'The path to the Dart SDK.',
        hide: !verboseHelp);
37 38

    // Hidden option to enable a benchmarking mode.
39 40 41 42
    argParser.addFlag('benchmark',
        negatable: false,
        hide: !verboseHelp,
        help: 'Also output the analysis time.');
43 44 45 46

    usesPubOption();

    // Not used by analyze --watch
47
    argParser.addFlag('congratulate',
48
        help: 'Show output even when there are no errors, warnings, hints, or lints. '
49
              'Ignored if --watch is specified.',
50 51 52 53
        defaultsTo: true);
    argParser.addFlag('preamble',
        defaultsTo: true,
        help: 'When analyzing the flutter repository, display the number of '
54 55
              'files that will be analyzed.\n'
              'Ignored if --watch is specified.');
Hixie's avatar
Hixie committed
56 57
  }

58 59 60
  /// The working directory for testing analysis using dartanalyzer.
  final Directory workingDirectory;

61
  @override
Ian Hickson's avatar
Ian Hickson committed
62
  String get name => 'analyze';
63 64

  @override
65
  String get description => "Analyze the project's Dart code.";
66

67
  @override
68 69
  bool get shouldRunPub {
    // If they're not analyzing the current project.
70
    if (!argResults['current-package']) {
71
      return false;
72
    }
73

74
    // Or we're not in a project directory.
75
    if (!fs.file('pubspec.yaml').existsSync()) {
76
      return false;
77
    }
78

79
    return super.shouldRunPub;
80 81
  }

82
  @override
83
  Future<FlutterCommandResult> runCommand() async {
84
    if (argResults['watch']) {
85
      await AnalyzeContinuously(
86 87 88
        argResults,
        runner.getRepoRoots(),
        runner.getRepoPackages(),
89
      ).analyze();
90
      return null;
91
    } else {
92
      await AnalyzeOnce(
93
        argResults,
94
        runner.getRepoRoots(),
95 96 97
        runner.getRepoPackages(),
        workingDirectory: workingDirectory,
      ).analyze();
98
      return null;
99 100 101
    }
  }
}