doctor.dart 7.25 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.

5
import 'dart:convert' show JSON;
Devon Carew's avatar
Devon Carew committed
6 7
import 'dart:io';

8 9
import 'package:path/path.dart' as path;

10 11
import 'android/android_workflow.dart';
import 'base/context.dart';
12
import 'base/os.dart';
13
import 'globals.dart';
14
import 'ios/ios_workflow.dart';
15
import 'runner/version.dart';
Devon Carew's avatar
Devon Carew committed
16

17 18 19 20 21 22 23 24 25 26 27 28

const Map<String, String> _osNames = const <String, String>{
  'macos': 'Mac OS',
  'linux': 'Linux',
  'windows': 'Windows'
};

String osName() {
  String os = Platform.operatingSystem;
  return _osNames.containsKey(os) ? _osNames[os] : os;
}

29 30
class Doctor {
  Doctor() {
31
    _validators.add(new _FlutterValidator());
32 33 34

    _androidWorkflow = new AndroidWorkflow();
    if (_androidWorkflow.appliesToHostPlatform)
Devon Carew's avatar
Devon Carew committed
35 36
      _validators.add(_androidWorkflow);

37 38 39 40
    _iosWorkflow = new IOSWorkflow();
    if (_iosWorkflow.appliesToHostPlatform)
      _validators.add(_iosWorkflow);

Devon Carew's avatar
Devon Carew committed
41
    _validators.add(new _AtomValidator());
42 43 44 45 46 47 48 49 50 51 52 53 54 55
  }

  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
56
  List<DoctorValidator> _validators = <DoctorValidator>[];
57

58 59 60
  List<Workflow> get workflows {
    return new List<Workflow>.from(_validators.where((DoctorValidator validator) => validator is Workflow));
  }
61

62 63 64 65 66 67 68 69
  /// 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
70 71
    for (DoctorValidator validator in _validators) {
      ValidationResult result = validator.validate();
72
      buffer.write('${result.leadingBox} ${validator.title} is ');
73
      if (result.type == ValidationType.missing)
74
        buffer.write('not installed.');
75
      else if (result.type == ValidationType.partial)
76
        buffer.write('partially installed; more components are available.');
77
      else
78 79 80 81 82 83 84
        buffer.write('fully installed.');

      if (result.statusInfo != null)
        buffer.write(' (${result.statusInfo})');

      buffer.writeln();

85 86 87 88 89 90 91 92 93 94 95 96
      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();
  }

97 98
  /// Print verbose information about the state of installed tooling.
  void diagnose() {
Devon Carew's avatar
Devon Carew committed
99
    bool firstLine = true;
100

Devon Carew's avatar
Devon Carew committed
101 102
    for (DoctorValidator validator in _validators) {
      if (!firstLine)
103
        printStatus('');
Devon Carew's avatar
Devon Carew committed
104
      firstLine = false;
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

      ValidationResult result = validator.validate();

      if (result.statusInfo != null)
        printStatus('${result.leadingBox} ${validator.title} (${result.statusInfo})');
      else
        printStatus('${result.leadingBox} ${validator.title}');

      for (ValidationMessage message in result.messages) {
        if (message.isError) {
          printStatus('    x ${message.message.replaceAll('\n', '\n      ')}');
        } else {
          printStatus('    • ${message.message.replaceAll('\n', '\n      ')}');
        }
      }
120 121 122 123 124 125 126 127
    }
  }

  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
128
/// A series of tools and required install steps for a target platform (iOS or Android).
129
abstract class Workflow {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  /// 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
}

146 147
abstract class DoctorValidator {
  DoctorValidator(this.title);
148

149
  final String title;
150

151
  ValidationResult validate();
152 153 154
}

class ValidationResult {
155
  ValidationResult(this.type, this.messages, { this.statusInfo });
156 157

  final ValidationType type;
158 159 160
  // A short message about the status.
  final String statusInfo;
  final List<ValidationMessage> messages;
161

162 163 164 165 166 167 168 169
  String get leadingBox {
    if (type == ValidationType.missing)
      return '[ ]';
    else if (type == ValidationType.installed)
      return '[✓]';
    else
      return '[-]';
  }
170
}
171

172 173 174
class ValidationMessage {
  ValidationMessage(this.message) : isError = false;
  ValidationMessage.error(this.message) : isError = true;
175

176 177
  final bool isError;
  final String message;
178

179 180 181
  @override
  String toString() => message;
}
182

183 184
class _FlutterValidator extends DoctorValidator {
  _FlutterValidator() : super('Flutter');
185

186 187 188
  @override
  ValidationResult validate() {
    List<ValidationMessage> messages = <ValidationMessage>[];
189

190
    FlutterVersion version = FlutterVersion.getVersion();
191

192 193
    messages.add(new ValidationMessage('Flutter root at ${version.flutterRoot}'));
    messages.add(new ValidationMessage('Framework revision ${version.frameworkRevisionShort} '
194
      '(${version.frameworkAge}, channel ${version.channel})'));
195
    messages.add(new ValidationMessage('Engine revision ${version.engineRevisionShort}'));
196

197
    return new ValidationResult(ValidationType.installed, messages, statusInfo: osName());
198
  }
199
}
Devon Carew's avatar
Devon Carew committed
200 201

class _AtomValidator extends DoctorValidator {
202 203 204
  _AtomValidator() : super('Atom - a lightweight development environment for Flutter');

  static String _getAtomHomePath() {
205 206 207 208 209 210 211 212
    final Map<String, String> env = Platform.environment;
    if (env['ATOM_HOME'] != null)
      return env['ATOM_HOME'];
    return os.isWindows
      ? path.join(env['USERPROFILE'], '.atom')
      : path.join(env['HOME'], '.atom');
  }

213
  @override
Devon Carew's avatar
Devon Carew committed
214
  ValidationResult validate() {
215
    List<ValidationMessage> messages = <ValidationMessage>[];
Devon Carew's avatar
Devon Carew committed
216

217
    int installCount = 0;
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
    bool atomDirExists = FileSystemEntity.isDirectorySync(_getAtomHomePath());
    if (!atomDirExists) {
      messages.add(new ValidationMessage.error(
        'Atom not installed; download at https://atom.io.'
      ));
    } else {
      installCount++;
    }

    String flutterPluginPath = path.join(_getAtomHomePath(), 'packages', 'flutter');
    if (!FileSystemEntity.isDirectorySync(flutterPluginPath)) {
      messages.add(new ValidationMessage.error(
        'Flutter plugin not installed; this adds Flutter specific functionality to Atom.\n'
        'Install the \'flutter\' plugin in Atom or run \'apm install flutter\'.'
      ));
    } else {
      installCount++;

      try {
        File packageFile = new File(path.join(flutterPluginPath, 'package.json'));
        dynamic packageInfo = JSON.decode(packageFile.readAsStringSync());
        String version = packageInfo['version'];
        messages.add(new ValidationMessage('Atom installed; flutter plugin version $version'));
      } catch (error) {
        printTrace('Unable to read flutter plugin version: $error');
      }
    }

    return new ValidationResult(
      installCount == 2 ? ValidationType.installed : installCount == 1 ? ValidationType.partial : ValidationType.missing,
      messages
    );
  }
Devon Carew's avatar
Devon Carew committed
252
}