analyze.dart 3.21 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 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  AnalyzeCommand({bool verboseHelp: false, this.workingDirectory}) {
    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,
        help: 'List every public member that is lacking documentation '
            '(only works with --flutter-repo).',
        hide: !verboseHelp);
    argParser.addFlag('watch',
        help: 'Run analysis continuously, watching the filesystem for changes.',
        negatable: false);
    argParser.addFlag('preview-dart-2',
        defaultsTo: true, help: 'Preview Dart 2.0 functionality.');
    argParser.addOption('write',
        valueHelp: 'file',
        help: 'Also output the results to a file. This is useful with --watch '
            'if you want a file to always contain the latest results.');
    argParser.addOption('dart-sdk',
        valueHelp: 'path-to-sdk',
        help: 'The path to the Dart SDK.',
        hide: !verboseHelp);
39 40

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

    usesPubOption();

    // Not used by analyze --watch
49 50 51 52 53 54 55 56
    argParser.addFlag('congratulate',
        help: 'When analyzing the flutter repository, show output even when '
            'there are no errors, warnings, hints, or lints.',
        defaultsTo: true);
    argParser.addFlag('preamble',
        defaultsTo: true,
        help: 'When analyzing the flutter repository, display the number of '
            'files that will be analyzed.');
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 (!argResults['current-package']) {
72
      return false;
73
    }
74

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

80
    return super.shouldRunPub;
81 82
  }

83
  @override
84
  Future<Null> runCommand() {
85
    if (argResults['watch']) {
86
      return new AnalyzeContinuously(
87 88 89 90
        argResults,
        runner.getRepoRoots(),
        runner.getRepoPackages(),
        previewDart2: argResults['preview-dart-2'],
91
      ).analyze();
92
    } else {
93 94
      return new AnalyzeOnce(
        argResults,
95
        runner.getRepoRoots(),
96 97 98 99
        runner.getRepoPackages(),
        workingDirectory: workingDirectory,
        previewDart2: argResults['preview-dart-2'],
      ).analyze();
100 101 102
    }
  }
}