android_studio_validator.dart 4.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2017 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';

import '../base/file_system.dart';
import '../base/io.dart';
import '../base/platform.dart';
import '../base/process_manager.dart';
11
import '../base/version.dart';
12 13
import '../doctor.dart';
import '../globals.dart';
14 15 16 17 18 19 20 21
import 'android_studio.dart';

class AndroidStudioValidator extends DoctorValidator {
  final AndroidStudio _studio;

  AndroidStudioValidator(this._studio) : super('Android Studio');

  static List<DoctorValidator> get allValidators {
22 23
    final List<DoctorValidator> validators = <DoctorValidator>[];
    final List<AndroidStudio> studios = AndroidStudio.allInstalled();
24 25 26 27 28 29
    if (studios.isEmpty) {
      validators.add(new NoAndroidStudioValidator());
    } else {
      validators.addAll(studios
          .map((AndroidStudio studio) => new AndroidStudioValidator(studio)));
    }
30
    final String cfgGradleDir = config.getValue('gradle-dir');
31 32 33 34 35 36 37 38
    if (cfgGradleDir != null) {
      validators.add(new ConfiguredGradleValidator(cfgGradleDir));
    }
    return validators;
  }

  @override
  Future<ValidationResult> validate() async {
39
    final List<ValidationMessage> messages = <ValidationMessage>[];
40
    ValidationType type = ValidationType.missing;
41
    final String studioVersionText = 'version ${_studio.version}';
42 43 44 45 46 47 48 49 50 51 52 53 54 55
    messages
        .add(new ValidationMessage('Android Studio at ${_studio.directory}'));
    if (_studio.isValid) {
      type = ValidationType.installed;
      messages.addAll(_studio.validationMessages
          .map((String m) => new ValidationMessage(m)));
    } else {
      type = ValidationType.partial;
      messages.addAll(_studio.validationMessages
          .map((String m) => new ValidationMessage.error(m)));
      messages.add(new ValidationMessage(
          'Try updating or re-installing Android Studio.'));
      if (_studio.configured != null) {
        messages.add(new ValidationMessage(
56
            'Consider removing your android-studio-dir setting by running:\nflutter config --android-studio-dir='));
57 58 59 60 61 62 63 64 65 66 67 68
      }
    }

    return new ValidationResult(type, messages, statusInfo: studioVersionText);
  }
}

class NoAndroidStudioValidator extends DoctorValidator {
  NoAndroidStudioValidator() : super('Android Studio');

  @override
  Future<ValidationResult> validate() async {
69
    final List<ValidationMessage> messages = <ValidationMessage>[];
70

71
    final String cfgAndroidStudio = config.getValue('android-studio-dir');
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    if (cfgAndroidStudio != null) {
      messages.add(
          new ValidationMessage.error('android-studio-dir = $cfgAndroidStudio\n'
              'but Android Studio not found at this location.'));
    }
    messages.add(new ValidationMessage(
        'Android Studio not found. Download from https://developer.android.com/studio/index.html\n'
        '(or visit https://flutter.io/setup/#android-setup for detailed instructions).'));

    return new ValidationResult(ValidationType.missing, messages,
        statusInfo: 'not installed');
  }
}

class ConfiguredGradleValidator extends DoctorValidator {
  final String cfgGradleDir;

  ConfiguredGradleValidator(this.cfgGradleDir) : super('Gradle');

  @override
  Future<ValidationResult> validate() async {
    ValidationType type = ValidationType.missing;
94
    final List<ValidationMessage> messages = <ValidationMessage>[];
95 96 97 98 99 100 101 102

    messages.add(new ValidationMessage('gradle-dir = $cfgGradleDir'));

    String gradleExecutable = cfgGradleDir;
    if (!fs.isFileSync(cfgGradleDir)) {
      gradleExecutable = fs.path.join(
          cfgGradleDir, 'bin', platform.isWindows ? 'gradle.bat' : 'gradle');
    }
103
    String versionString;
104 105
    if (processManager.canRun(gradleExecutable)) {
      type = ValidationType.partial;
106
      final ProcessResult result =
107 108
          processManager.runSync(<String>[gradleExecutable, '--version']);
      if (result.exitCode == 0) {
109
        versionString = result.stdout
110 111 112 113
            .toString()
            .split('\n')
            .firstWhere((String s) => s.startsWith('Gradle '))
            .substring('Gradle '.length);
114
        final Version version = new Version.parse(versionString) ?? Version.unknown;
115
        if (version >= minGradleVersion) {
116 117 118
          type = ValidationType.installed;
        } else {
          messages.add(new ValidationMessage.error(
119
              'Gradle version $minGradleVersion required. Found version $versionString.'));
120 121 122 123 124 125 126 127 128 129 130
        }
      } else {
        messages
            .add(new ValidationMessage('Unable to determine Gradle version.'));
      }
    } else {
      messages
          .add(new ValidationMessage('Gradle not found at $gradleExecutable'));
    }

    messages.add(new ValidationMessage(
131 132
        'Flutter supports building with Gradle from Android Studio.\n'
        'Consider removing your gradle-dir setting by running:\nflutter config --gradle-dir='));
133
    return new ValidationResult(type, messages, statusInfo: versionString);
134 135
  }
}