analyze.dart 3.28 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
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{
69 70 71
    DevelopmentArtifact.universal,
  };

72
  @override
73 74
  bool get shouldRunPub {
    // If they're not analyzing the current project.
75
    if (!boolArg('current-package')) {
76
      return false;
77
    }
78

79
    // Or we're not in a project directory.
80
    if (!fs.file('pubspec.yaml').existsSync()) {
81
      return false;
82
    }
83

84
    return super.shouldRunPub;
85 86
  }

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