analyze.dart 4.54 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7
import 'package:meta/meta.dart';
import 'package:process/process.dart';

8
import '../artifacts.dart';
9
import '../base/file_system.dart';
10
import '../base/logger.dart';
11
import '../base/platform.dart';
12
import '../base/terminal.dart';
13 14 15
import '../runner/flutter_command.dart';
import 'analyze_continuously.dart';
import 'analyze_once.dart';
Hixie's avatar
Hixie committed
16

17
class AnalyzeCommand extends FlutterCommand {
18 19 20 21 22
  AnalyzeCommand({
    bool verboseHelp = false,
    this.workingDirectory,
    @required FileSystem fileSystem,
    @required Platform platform,
23
    @required Terminal terminal,
24 25
    @required Logger logger,
    @required ProcessManager processManager,
26 27 28
    @required Artifacts artifacts,
  }) : _artifacts = artifacts,
       _fileSystem = fileSystem,
29 30 31 32
       _processManager = processManager,
       _logger = logger,
       _terminal = terminal,
       _platform = platform {
33 34 35 36 37 38 39 40 41
    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,
42
        help: 'List every public member that is lacking documentation. '
43
              '(The public_member_api_docs lint must be enabled in analysis_options.yaml)',
44 45 46 47 48 49 50
        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 '
51
              'if you want a file to always contain the latest results.');
52 53 54 55
    argParser.addOption('dart-sdk',
        valueHelp: 'path-to-sdk',
        help: 'The path to the Dart SDK.',
        hide: !verboseHelp);
56 57

    // Hidden option to enable a benchmarking mode.
58 59 60 61
    argParser.addFlag('benchmark',
        negatable: false,
        hide: !verboseHelp,
        help: 'Also output the analysis time.');
62 63 64 65

    usesPubOption();

    // Not used by analyze --watch
66
    argParser.addFlag('congratulate',
67
        help: 'Show output even when there are no errors, warnings, hints, or lints. '
68
              'Ignored if --watch is specified.',
69 70 71 72
        defaultsTo: true);
    argParser.addFlag('preamble',
        defaultsTo: true,
        help: 'When analyzing the flutter repository, display the number of '
73 74
              'files that will be analyzed.\n'
              'Ignored if --watch is specified.');
75 76 77 78 79 80 81 82
    argParser.addFlag('fatal-infos',
        negatable: true,
        help: 'Treat info level issues as fatal.',
        defaultsTo: true);
    argParser.addFlag('fatal-warnings',
        negatable: true,
        help: 'Treat warning level issues as fatal.',
        defaultsTo: true);
Hixie's avatar
Hixie committed
83 84
  }

85 86 87
  /// The working directory for testing analysis using dartanalyzer.
  final Directory workingDirectory;

88
  final Artifacts _artifacts;
89 90
  final FileSystem _fileSystem;
  final Logger _logger;
91
  final Terminal _terminal;
92 93 94
  final ProcessManager _processManager;
  final Platform _platform;

95
  @override
Ian Hickson's avatar
Ian Hickson committed
96
  String get name => 'analyze';
97 98

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

101
  @override
102 103
  bool get shouldRunPub {
    // If they're not analyzing the current project.
104
    if (!boolArg('current-package')) {
105
      return false;
106
    }
107

108
    // Or we're not in a project directory.
109
    if (!_fileSystem.file('pubspec.yaml').existsSync()) {
110
      return false;
111
    }
112

113
    return super.shouldRunPub;
114 115
  }

116
  @override
117
  Future<FlutterCommandResult> runCommand() async {
118
    if (boolArg('watch')) {
119
      await AnalyzeContinuously(
120 121 122
        argResults,
        runner.getRepoRoots(),
        runner.getRepoPackages(),
123
        fileSystem: _fileSystem,
124
        logger: _logger,
125 126 127
        platform: _platform,
        processManager: _processManager,
        terminal: _terminal,
128
        artifacts: _artifacts,
129
      ).analyze();
130
    } else {
131
      await AnalyzeOnce(
132
        argResults,
133
        runner.getRepoRoots(),
134 135
        runner.getRepoPackages(),
        workingDirectory: workingDirectory,
136
        fileSystem: _fileSystem,
137
        logger: _logger,
138 139 140
        platform: _platform,
        processManager: _processManager,
        terminal: _terminal,
141
        artifacts: _artifacts,
142
      ).analyze();
143
    }
144
    return FlutterCommandResult.success();
145 146
  }
}