analyze.dart 4.83 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
// @dart = 2.8

7
import 'package:meta/meta.dart';
8
import 'package:process/process.dart';
9

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

19
class AnalyzeCommand extends FlutterCommand {
20 21 22 23 24
  AnalyzeCommand({
    bool verboseHelp = false,
    this.workingDirectory,
    @required FileSystem fileSystem,
    @required Platform platform,
25
    @required Terminal terminal,
26 27
    @required Logger logger,
    @required ProcessManager processManager,
28 29 30
    @required Artifacts artifacts,
  }) : _artifacts = artifacts,
       _fileSystem = fileSystem,
31 32 33 34
       _processManager = processManager,
       _logger = logger,
       _terminal = terminal,
       _platform = platform {
35 36 37 38 39 40 41 42 43
    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,
44 45
        help: '(deprecated) List every public member that is lacking documentation. '
              'This command will be removed in a future version of Flutter.',
46 47 48 49 50 51
        hide: !verboseHelp);
    argParser.addFlag('watch',
        help: 'Run analysis continuously, watching the filesystem for changes.',
        negatable: false);
    argParser.addOption('write',
        valueHelp: 'file',
52
        help: 'Also output the results to a file. This is useful with "--watch" '
53
              'if you want a file to always contain the latest results.');
54 55 56 57
    argParser.addOption('dart-sdk',
        valueHelp: 'path-to-sdk',
        help: 'The path to the Dart SDK.',
        hide: !verboseHelp);
58 59 60 61 62
    argParser.addOption('protocol-traffic-log',
        valueHelp: 'path-to-protocol-traffic-log',
        help: 'The path to write the request and response protocol. This is '
              'only intended to be used for debugging the tooling.',
        hide: !verboseHelp);
63 64

    // Hidden option to enable a benchmarking mode.
65 66 67 68
    argParser.addFlag('benchmark',
        negatable: false,
        hide: !verboseHelp,
        help: 'Also output the analysis time.');
69 70 71 72

    usesPubOption();

    // Not used by analyze --watch
73
    argParser.addFlag('congratulate',
74
        help: 'Show output even when there are no errors, warnings, hints, or lints. '
75
              'Ignored if "--watch" is specified.',
76 77 78 79
        defaultsTo: true);
    argParser.addFlag('preamble',
        defaultsTo: true,
        help: 'When analyzing the flutter repository, display the number of '
80
              'files that will be analyzed.\n'
81
              'Ignored if "--watch" is specified.');
82 83 84 85 86 87 88 89
    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
90 91
  }

92 93 94
  /// The working directory for testing analysis using dartanalyzer.
  final Directory workingDirectory;

95
  final Artifacts _artifacts;
96 97
  final FileSystem _fileSystem;
  final Logger _logger;
98
  final Terminal _terminal;
99 100 101
  final ProcessManager _processManager;
  final Platform _platform;

102
  @override
Ian Hickson's avatar
Ian Hickson committed
103
  String get name => 'analyze';
104 105

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

108
  @override
109 110
  bool get shouldRunPub {
    // If they're not analyzing the current project.
111
    if (!boolArg('current-package')) {
112
      return false;
113
    }
114

115
    // Or we're not in a project directory.
116
    if (!_fileSystem.file('pubspec.yaml').existsSync()) {
117
      return false;
118
    }
119

120
    return super.shouldRunPub;
121 122
  }

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