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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright 2014 The Flutter 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 'package:meta/meta.dart';
import 'package:process/process.dart';

import '../artifacts.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../base/terminal.dart';
import '../project.dart';
import '../project_validator.dart';
import '../runner/flutter_command.dart';
import 'analyze_base.dart';
import 'analyze_continuously.dart';
import 'analyze_once.dart';
import 'android_analyze.dart';
import 'ios_analyze.dart';
import 'validate_project.dart';

class AnalyzeCommand extends FlutterCommand {
  AnalyzeCommand({
    bool verboseHelp = false,
    this.workingDirectory,
    required FileSystem fileSystem,
    required Platform platform,
    required Terminal terminal,
    required Logger logger,
    required ProcessManager processManager,
    required Artifacts artifacts,
    required List<ProjectValidator> allProjectValidators,
    required bool suppressAnalytics,
  }) : _artifacts = artifacts,
       _fileSystem = fileSystem,
       _processManager = processManager,
       _logger = logger,
       _terminal = terminal,
       _allProjectValidators = allProjectValidators,
       _platform = platform,
       _suppressAnalytics = suppressAnalytics {
    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,
      help:
          '(deprecated) List every public member that is lacking documentation. '
          'This command will be removed in a future version of Flutter.',
      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" '
          '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,
    );
    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,
    );
    argParser.addFlag('suggestions', help: 'Show suggestions about the current flutter project.');
    argParser.addFlag(
      'machine',
      negatable: false,
      help:
          'Dumps a JSON with a subset of relevant data about the tool, project, '
          'and environment.',
      hide: !verboseHelp,
    );

    // Hidden option to enable a benchmarking mode.
    argParser.addFlag(
      'benchmark',
      negatable: false,
      hide: !verboseHelp,
      help: 'Also output the analysis time.',
    );

    usesPubOption();

    // Not used by analyze --watch
    argParser.addFlag(
      'congratulate',
      help:
          'Show output even when there are no errors, warnings, hints, or lints. '
          'Ignored if "--watch" is specified.',
      defaultsTo: true,
    );
    argParser.addFlag(
      'preamble',
      defaultsTo: true,
      help:
          'When analyzing the flutter repository, display the number of '
          'files that will be analyzed.\n'
          'Ignored if "--watch" is specified.',
    );
    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,
    );

    argParser.addFlag(
      'android',
      negatable: false,
      help: 'Analyze Android sub-project. Used by internal tools only.',
      hide: !verboseHelp,
    );

    argParser.addFlag(
      'ios',
      negatable: false,
      help: 'Analyze iOS Xcode sub-project. Used by internal tools only.',
      hide: !verboseHelp,
    );

    if (verboseHelp) {
      argParser.addSeparator('Usage: flutter analyze --android [arguments]');
    }

    argParser.addFlag(
      'list-build-variants',
      negatable: false,
      help:
          'Print out a list of available build variants for the '
          'Android sub-project.',
      hide: !verboseHelp,
    );

    argParser.addFlag(
      'output-app-link-settings',
      negatable: false,
      help:
          'Output a JSON with Android app link settings into a file. '
          'The "--build-variant" must also be set.',
      hide: !verboseHelp,
    );

    argParser.addOption(
      'build-variant',
      help: 'Sets the Android build variant to be analyzed.',
      valueHelp: 'build variant',
      hide: !verboseHelp,
    );

    if (verboseHelp) {
      argParser.addSeparator('Usage: flutter analyze --ios [arguments]');
    }

    argParser.addFlag(
      'list-build-options',
      help:
          'Print out a list of available build options for the '
          'iOS Xcode sub-project.',
      hide: !verboseHelp,
    );

    argParser.addFlag(
      'output-universal-link-settings',
      negatable: false,
      help:
          'Output a JSON with iOS Xcode universal link settings into a file. '
          'The "--configuration" and "--target" must be set.',
      hide: !verboseHelp,
    );

    argParser.addOption(
      'configuration',
      help: 'Sets the iOS build configuration to be analyzed.',
      valueHelp: 'configuration',
      hide: !verboseHelp,
    );

    argParser.addOption(
      'target',
      help: 'Sets the iOS build target to be analyzed.',
      valueHelp: 'target',
      hide: !verboseHelp,
    );
  }

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

  final Artifacts _artifacts;
  final FileSystem _fileSystem;
  final Logger _logger;
  final Terminal _terminal;
  final ProcessManager _processManager;
  final Platform _platform;
  final List<ProjectValidator> _allProjectValidators;
  final bool _suppressAnalytics;

  @override
  String get name => 'analyze';

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

  @override
  String get category => FlutterCommandCategory.project;

  @visibleForTesting
  List<ProjectValidator> allProjectValidators() => _allProjectValidators;

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

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

    // Don't run pub if asking for machine output.
    if (boolArg('machine')) {
      return false;
    }

    // Android analyze needs to process resource, i.e. evaluating build
    // settings and assets, and thus needs to run pub.
    if (boolArg('android')) {
      return true;
    }

    return super.shouldRunPub;
  }

  @override
  Future<FlutterCommandResult> runCommand() async {
    if (boolArg('android')) {
      final AndroidAnalyzeOption option;
      final String? buildVariant;
      if (argResults!['list-build-variants'] as bool &&
          argResults!['output-app-link-settings'] as bool) {
        throwToolExit(
          'Only one of "--list-build-variants" or "--output-app-link-settings" can be provided',
        );
      }
      if (argResults!['list-build-variants'] as bool) {
        option = AndroidAnalyzeOption.listBuildVariant;
        buildVariant = null;
      } else if (argResults!['output-app-link-settings'] as bool) {
        option = AndroidAnalyzeOption.outputAppLinkSettings;
        buildVariant = argResults!['build-variant'] as String?;
        if (buildVariant == null) {
          throwToolExit('"--build-variant" must be provided');
        }
      } else {
        throwToolExit('No argument is provided to analyze. Use -h to see available commands.');
      }
      final Set<String> items = findDirectories(argResults!, _fileSystem);
      final String directoryPath;
      if (items.isEmpty) {
        // user did not specify any path
        directoryPath = _fileSystem.currentDirectory.path;
      } else if (items.length > 1) {
        // if the user sends more than one path
        throwToolExit('The Android analyze can process only one directory path');
      } else {
        directoryPath = items.first;
      }
      await AndroidAnalyze(
        fileSystem: _fileSystem,
        option: option,
        userPath: directoryPath,
        buildVariant: buildVariant,
        logger: _logger,
      ).analyze();
    } else if (boolArg('ios')) {
      final IOSAnalyzeOption option;
      final String? configuration;
      final String? target;
      if (argResults!['list-build-options'] as bool &&
          argResults!['output-universal-link-settings'] as bool) {
        throwToolExit(
          'Only one of "--list-build-options" or "--output-universal-link-settings" can be provided',
        );
      }
      if (argResults!['list-build-options'] as bool) {
        option = IOSAnalyzeOption.listBuildOptions;
        configuration = null;
        target = null;
      } else if (argResults!['output-universal-link-settings'] as bool) {
        option = IOSAnalyzeOption.outputUniversalLinkSettings;
        configuration = argResults!['configuration'] as String?;
        if (configuration == null) {
          throwToolExit('"--configuration" must be provided');
        }
        target = argResults!['target'] as String?;
        if (target == null) {
          throwToolExit('"--target" must be provided');
        }
      } else {
        throwToolExit('No argument is provided to analyze. Use -h to see available commands.');
      }
      final Set<String> items = findDirectories(argResults!, _fileSystem);
      final String directoryPath;
      if (items.isEmpty) {
        // user did not specify any path
        directoryPath = _fileSystem.currentDirectory.path;
      } else if (items.length > 1) {
        // if the user sends more than one path
        throwToolExit('The iOS analyze can process only one directory path');
      } else {
        directoryPath = items.first;
      }
      await IOSAnalyze(
        project: FlutterProject.fromDirectory(_fileSystem.directory(directoryPath)),
        option: option,
        configuration: configuration,
        target: target,
        logger: _logger,
      ).analyze();
    } else if (boolArg('suggestions')) {
      final String directoryPath;
      if (boolArg('watch')) {
        throwToolExit('flag --watch is not compatible with --suggestions');
      }
      if (workingDirectory == null) {
        final Set<String> items = findDirectories(argResults!, _fileSystem);
        if (items.isEmpty) {
          // user did not specify any path
          directoryPath = _fileSystem.currentDirectory.path;
          _logger.printTrace('Showing suggestions for current directory: $directoryPath');
        } else if (items.length > 1) {
          // if the user sends more than one path
          throwToolExit('The suggestions flag can process only one directory path');
        } else {
          directoryPath = items.first;
        }
      } else {
        directoryPath = workingDirectory!.path;
      }
      return ValidateProject(
        fileSystem: _fileSystem,
        logger: _logger,
        allProjectValidators: _allProjectValidators,
        userPath: directoryPath,
        processManager: _processManager,
        machine: boolArg('machine'),
      ).run();
    } else if (boolArg('watch')) {
      await AnalyzeContinuously(
        argResults!,
        runner!.getRepoPackages(),
        fileSystem: _fileSystem,
        logger: _logger,
        platform: _platform,
        processManager: _processManager,
        terminal: _terminal,
        artifacts: _artifacts,
        suppressAnalytics: _suppressAnalytics,
      ).analyze();
    } else {
      await AnalyzeOnce(
        argResults!,
        runner!.getRepoPackages(),
        workingDirectory: workingDirectory,
        fileSystem: _fileSystem,
        logger: _logger,
        platform: _platform,
        processManager: _processManager,
        terminal: _terminal,
        artifacts: _artifacts,
        suppressAnalytics: _suppressAnalytics,
      ).analyze();
    }
    return FlutterCommandResult.success();
  }
}