validate_project.dart 3.65 KB
Newer Older
1 2 3 4
// 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.

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

7 8 9 10 11 12 13
import '../base/file_system.dart';
import '../base/logger.dart';
import '../project.dart';
import '../project_validator.dart';
import '../project_validator_result.dart';
import '../runner/flutter_command.dart';

14 15
class ValidateProject {
  ValidateProject({
16 17 18
    required this.fileSystem,
    required this.logger,
    required this.allProjectValidators,
19
    required this.userPath,
20 21
    required this.processManager,
    this.verbose = false,
22
    this.machine = false,
23 24 25 26 27
  });

  final FileSystem fileSystem;
  final Logger logger;
  final bool verbose;
28
  final bool machine;
29
  final String userPath;
30
  final List<ProjectValidator> allProjectValidators;
31
  final ProcessManager processManager;
32

33
  Future<FlutterCommandResult> run() async {
34 35 36 37 38 39 40
    final Directory workingDirectory = userPath.isEmpty ? fileSystem.currentDirectory : fileSystem.directory(userPath);

    final FlutterProject project =  FlutterProject.fromDirectory(workingDirectory);
    final Map<ProjectValidator, Future<List<ProjectValidatorResult>>> results = <ProjectValidator, Future<List<ProjectValidatorResult>>>{};

    bool hasCrash = false;
    for (final ProjectValidator validator in allProjectValidators) {
41 42 43
      if (validator.machineOutput != machine) {
        continue;
      }
44 45 46 47 48 49 50 51 52
      if (!results.containsKey(validator) && validator.supportsProject(project)) {
        results[validator] = validator.start(project).catchError((Object exception, StackTrace trace) {
          hasCrash = true;
          return <ProjectValidatorResult>[ProjectValidatorResult.crash(exception, trace)];
        });
      }
    }

    final StringBuffer buffer = StringBuffer();
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    if (machine) {
      // Print properties
      buffer.write('{\n');
      for (final Future<List<ProjectValidatorResult>> resultListFuture in results.values) {
        final List<ProjectValidatorResult> resultList = await resultListFuture;
        int count = 0;
        for (final ProjectValidatorResult result in resultList) {
          count++;
          buffer.write('  "${result.name}": ${result.value}${count < resultList.length ? ',' : ''}\n');
        }
      }
      buffer.write('}');
      logger.printStatus(buffer.toString());
    } else {
      final List<String> resultsString = <String>[];
      for (final ProjectValidator validator in results.keys) {
        if (results[validator] != null) {
          resultsString.add(validator.title);
          addResultString(validator.title, await results[validator], resultsString);
        }
73
      }
74 75
      buffer.writeAll(resultsString, '\n');
      logger.printBox(buffer.toString());
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    }

    if (hasCrash) {
      return const FlutterCommandResult(ExitStatus.fail);
    }
    return const FlutterCommandResult(ExitStatus.success);
  }


  void addResultString(final String title, final List<ProjectValidatorResult>? results, final List<String> resultsString) {
    if (results != null) {
      for (final ProjectValidatorResult result in results) {
        resultsString.add(getStringResult(result));
      }
    }
  }

  String getStringResult(ProjectValidatorResult result) {
    final String icon;
    switch(result.status) {
      case StatusProjectValidator.error:
        icon = '[✗]';
        break;
99
      case StatusProjectValidator.info:
100 101 102 103 104 105 106 107 108 109 110 111 112 113
      case StatusProjectValidator.success:
        icon = '[✓]';
        break;
      case StatusProjectValidator.warning:
        icon = '[!]';
        break;
      case StatusProjectValidator.crash:
        icon = '[☠]';
        break;
    }

    return '$icon $result';
  }
}