analyze.dart 3.18 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
2 3 4 5 6
// 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
import '../globals.dart' as globals;
9 10 11
import '../runner/flutter_command.dart';
import 'analyze_continuously.dart';
import 'analyze_once.dart';
Hixie's avatar
Hixie committed
12

13
class AnalyzeCommand extends FlutterCommand {
14
  AnalyzeCommand({bool verboseHelp = false, this.workingDirectory}) {
15 16 17 18 19 20 21 22 23
    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,
24
        help: 'List every public member that is lacking documentation. '
25
              '(The public_member_api_docs lint must be enabled in analysis_options.yaml)',
26 27 28 29 30 31 32
        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 '
33
              'if you want a file to always contain the latest results.');
34 35 36 37
    argParser.addOption('dart-sdk',
        valueHelp: 'path-to-sdk',
        help: 'The path to the Dart SDK.',
        hide: !verboseHelp);
38 39

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

    usesPubOption();

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

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

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

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

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

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

80
    return super.shouldRunPub;
81 82
  }

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