analyze.dart 4.8 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
import 'package:process/process.dart';
6

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

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

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

    usesPubOption();

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

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

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

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

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

102 103 104
  @override
  String get category => FlutterCommandCategory.project;

105
  @override
106 107
  bool get shouldRunPub {
    // If they're not analyzing the current project.
108
    if (!boolArgDeprecated('current-package')) {
109
      return false;
110
    }
111

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

117
    return super.shouldRunPub;
118 119
  }

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