doctor.dart 7.54 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

Devon Carew's avatar
Devon Carew committed
5 6
import 'dart:io';

7 8
import 'android/android_workflow.dart';
import 'base/context.dart';
Devon Carew's avatar
Devon Carew committed
9
import 'base/process.dart';
10
import 'globals.dart';
11 12
import 'ios/ios_workflow.dart';

Devon Carew's avatar
Devon Carew committed
13 14
// TODO(devoncarew): Make it easy to add version information to the `doctor` printout.

15 16 17 18
class Doctor {
  Doctor() {
    _iosWorkflow = new IOSWorkflow();
    if (_iosWorkflow.appliesToHostPlatform)
Devon Carew's avatar
Devon Carew committed
19
      _validators.add(_iosWorkflow);
20 21 22

    _androidWorkflow = new AndroidWorkflow();
    if (_androidWorkflow.appliesToHostPlatform)
Devon Carew's avatar
Devon Carew committed
23 24 25
      _validators.add(_androidWorkflow);

    _validators.add(new _AtomValidator());
26 27 28 29 30 31 32 33 34 35 36 37 38 39
  }

  static void initGlobal() {
    context[Doctor] = new Doctor();
  }

  IOSWorkflow _iosWorkflow;
  AndroidWorkflow _androidWorkflow;

  /// This can return null for platforms that don't support developing for iOS.
  IOSWorkflow get iosWorkflow => _iosWorkflow;

  AndroidWorkflow get androidWorkflow => _androidWorkflow;

Devon Carew's avatar
Devon Carew committed
40
  List<DoctorValidator> _validators = <DoctorValidator>[];
41

42 43 44
  List<Workflow> get workflows {
    return new List<Workflow>.from(_validators.where((DoctorValidator validator) => validator is Workflow));
  }
45

46 47 48 49 50 51 52 53
  /// Print a summary of the state of the tooling, as well as how to get more info.
  void summary() => printStatus(summaryText);

  String get summaryText {
    StringBuffer buffer = new StringBuffer();

    bool allGood = true;

Devon Carew's avatar
Devon Carew committed
54 55 56
    for (DoctorValidator validator in _validators) {
      ValidationResult result = validator.validate();
      buffer.write('${result.leadingBox} The ${validator.label} is ');
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
      if (result.type == ValidationType.missing)
        buffer.writeln('not installed.');
      else if (result.type == ValidationType.partial)
        buffer.writeln('partially installed; more components are available.');
      else
        buffer.writeln('fully installed.');
      if (result.type != ValidationType.installed)
        allGood = false;
    }

    if (!allGood) {
      buffer.writeln();
      buffer.write('Run "flutter doctor" for information about installing additional components.');
    }

    return buffer.toString();
  }

75 76
  /// Print verbose information about the state of installed tooling.
  void diagnose() {
Devon Carew's avatar
Devon Carew committed
77 78 79
    bool firstLine = true;
    for (DoctorValidator validator in _validators) {
      if (!firstLine)
80
        printStatus('');
Devon Carew's avatar
Devon Carew committed
81 82
      firstLine = false;
      validator.diagnose();
83 84 85 86 87 88 89 90
    }
  }

  bool get canListAnything => workflows.any((Workflow workflow) => workflow.canListDevices);

  bool get canLaunchAnything => workflows.any((Workflow workflow) => workflow.canLaunchDevices);
}

Devon Carew's avatar
Devon Carew committed
91 92
abstract class DoctorValidator {
  String get label;
93

Devon Carew's avatar
Devon Carew committed
94
  ValidationResult validate();
95

Devon Carew's avatar
Devon Carew committed
96 97 98 99 100 101
  /// Print verbose information about the state of the workflow.
  void diagnose();
}

/// A series of tools and required install steps for a target platform (iOS or Android).
abstract class Workflow extends DoctorValidator {
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
  /// Whether the workflow applies to this platform (as in, should we ever try and use it).
  bool get appliesToHostPlatform;

  /// Are we functional enough to list devices?
  bool get canListDevices;

  /// Could this thing launch *something*? It may still have minor issues.
  bool get canLaunchDevices;
}

enum ValidationType {
  missing,
  partial,
  installed
}

typedef ValidationType ValidationFunction();

class Validator {
  Validator(this.name, { this.description, this.resolution, this.validatorFunction });

  final String name;
  final String description;
  final String resolution;
  final ValidationFunction validatorFunction;

128
  List<Validator> _children = <Validator>[];
129 130

  ValidationResult validate() {
131 132
    List<ValidationResult> childResults;
    ValidationType type;
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147
    if (validatorFunction != null)
      type = validatorFunction();

    childResults = _children.map((Validator child) => child.validate()).toList();

    // If there's no immediate validator, the result we return is synthesized
    // from the sub-tree of children. This is so we can show that the branch is
    // not fully installed.
    if (type == null) {
      type = _combine(childResults
        .expand((ValidationResult child) => child._allResults)
        .map((ValidationResult result) => result.type)
      );
    }
148

149
    return new ValidationResult(type, this, childResults);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  }

  ValidationType _combine(Iterable<ValidationType> types) {
    if (types.contains(ValidationType.missing) && types.contains(ValidationType.installed))
      return ValidationType.partial;
    if (types.contains(ValidationType.missing))
      return ValidationType.missing;
    return ValidationType.installed;
  }

  void addValidator(Validator validator) => _children.add(validator);
}

class ValidationResult {
  ValidationResult(this.type, this.validator, [this.childResults = const <ValidationResult>[]]);

  final ValidationType type;
  final Validator validator;
  final List<ValidationResult> childResults;

170 171 172 173 174 175 176 177 178
  String get leadingBox {
    if (type == ValidationType.missing)
      return '[ ]';
    else if (type == ValidationType.installed)
      return '[✓]';
    else
      return '[-]';
  }

179 180 181 182 183 184 185
  void print([String indent = '']) {
    printSelf(indent);

    for (ValidationResult child in childResults)
      child.print(indent + '  ');
  }

186
  void printSelf([String indent = '']) {
187 188 189
    String result = indent;

    if (type == ValidationType.missing)
190
      result += '$leadingBox ';
191
    else if (type == ValidationType.installed)
192
      result += '$leadingBox ';
193
    else
194
      result += '$leadingBox ';
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

    result += '${validator.name} ';

    if (validator.description != null)
      result += '- ${validator.description} ';

    if (type == ValidationType.missing)
      result += '(missing)';
    else if (type == ValidationType.installed)
      result += '(installed)';

    printStatus(result);

    if (type == ValidationType.missing && validator.resolution != null)
      printStatus('$indent    ${validator.resolution}');
  }
211 212 213 214 215 216

  List<ValidationResult> get _allResults {
    List<ValidationResult> results = <ValidationResult>[this];
    results.addAll(childResults);
    return results;
  }
217
}
Devon Carew's avatar
Devon Carew committed
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

class _AtomValidator extends DoctorValidator {
  String get label => 'Atom development environment';

  ValidationResult validate() {
    Validator atomValidator = new Validator(
      label,
      description: 'a lightweight development environment for Flutter'
    );

    ValidationType atomExists() {
      return exitsHappy(<String>['atom', '--version']) ? ValidationType.installed : ValidationType.missing;
    };

    ValidationType flutterPluginExists() {
      try {
        // apm list -b -p -i
        ProcessResult result = Process.runSync('apm', <String>['list', '-b', '-p', '-i']);
        if (result.exitCode != 0)
          return ValidationType.missing;
        bool available = (result.stdout as String).split('\n').any((String line) {
          return line.startsWith('flutter@');
        });
        return available ? ValidationType.installed : ValidationType.missing;
      } catch (error) {
        return ValidationType.missing;
      }
    };

    atomValidator.addValidator(new Validator(
      'Atom editor',
      resolution: 'Download at https://atom.io',
      validatorFunction: atomExists
    ));

    atomValidator.addValidator(new Validator(
      'Flutter plugin',
      description: 'adds Flutter specific functionality to Atom',
      resolution: "Install the 'flutter' plugin in Atom or run 'apm install flutter'",
      validatorFunction: flutterPluginExists
    ));

    return atomValidator.validate();
  }

  void diagnose() => validate().print();
}