android_studio.dart 12 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import '../base/context.dart';
import '../base/file_system.dart';
7
import '../base/io.dart';
8
import '../base/process.dart';
9
import '../base/utils.dart';
10
import '../base/version.dart';
11
import '../globals.dart' as globals;
12
import '../ios/plist_parser.dart';
13

14
AndroidStudio get androidStudio => context.get<AndroidStudio>();
15 16 17 18 19 20 21 22 23 24

// Android Studio layout:

// Linux/Windows:
// $HOME/.AndroidStudioX.Y/system/.home

// macOS:
// /Applications/Android Studio.app/Contents/
// $HOME/Applications/Android Studio.app/Contents/

25
final RegExp _dotHomeStudioVersionMatcher =
26
    RegExp(r'^\.(AndroidStudio[^\d]*)([\d.]+)');
27

28 29
String get javaPath => androidStudio?.javaPath;

30
class AndroidStudio implements Comparable<AndroidStudio> {
31 32 33 34 35 36 37
  AndroidStudio(
    this.directory, {
    Version version,
    this.configured,
    this.studioAppName = 'AndroidStudio',
    this.presetPluginsPath,
  }) : version = version ?? Version.unknown {
38 39 40 41
    _init();
  }

  factory AndroidStudio.fromMacOSBundle(String bundlePath) {
42 43
    String studioPath = globals.fs.path.join(bundlePath, 'Contents');
    String plistFile = globals.fs.path.join(studioPath, 'Info.plist');
44
    Map<String, dynamic> plistValues = globals.plistParser.parseFile(plistFile);
45 46
    // As AndroidStudio managed by JetBrainsToolbox could have a wrapper pointing to the real Android Studio.
    // Check if we've found a JetBrainsToolbox wrapper and deal with it properly.
47
    final String jetBrainsToolboxAppBundlePath = plistValues['JetBrainsToolboxApp'] as String;
48
    if (jetBrainsToolboxAppBundlePath != null) {
49 50
      studioPath = globals.fs.path.join(jetBrainsToolboxAppBundlePath, 'Contents');
      plistFile = globals.fs.path.join(studioPath, 'Info.plist');
51
      plistValues = globals.plistParser.parseFile(plistFile);
52 53
    }

54
    final String versionString = plistValues[PlistParser.kCFBundleShortVersionStringKey] as String;
55

56
    Version version;
57
    if (versionString != null) {
58
      version = Version.parse(versionString);
59
    }
60

61
    String pathsSelectorValue;
62
    final Map<String, dynamic> jvmOptions = castStringKeyedMap(plistValues['JVMOptions']);
63
    if (jvmOptions != null) {
64
      final Map<String, dynamic> jvmProperties = castStringKeyedMap(jvmOptions['Properties']);
65
      if (jvmProperties != null) {
66
        pathsSelectorValue = jvmProperties['idea.paths.selector'] as String;
67 68
      }
    }
69
    final String presetPluginsPath = pathsSelectorValue == null
70 71 72 73 74 75 76
      ? null
      : globals.fs.path.join(
        globals.fsUtils.homeDirPath,
        'Library',
        'Application Support',
        pathsSelectorValue,
      );
77
    return AndroidStudio(studioPath, version: version, presetPluginsPath: presetPluginsPath);
78 79 80
  }

  factory AndroidStudio.fromHomeDot(Directory homeDotDir) {
81
    final Match versionMatch =
82 83 84 85
        _dotHomeStudioVersionMatcher.firstMatch(homeDotDir.basename);
    if (versionMatch?.groupCount != 2) {
      return null;
    }
86
    final Version version = Version.parse(versionMatch[2]);
87 88
    final String studioAppName = versionMatch[1];
    if (studioAppName == null || version == null) {
89 90
      return null;
    }
91 92
    String installPath;
    try {
93 94
      installPath = globals.fs
          .file(globals.fs.path.join(homeDotDir.path, 'system', '.home'))
95
          .readAsStringSync();
96
    } on Exception {
97
      // ignored, installPath will be null, which is handled below
98
    }
99
    if (installPath != null && globals.fs.isDirectorySync(installPath)) {
100 101 102 103 104
      return AndroidStudio(
          installPath,
          version: version,
          studioAppName: studioAppName,
      );
105 106 107 108
    }
    return null;
  }

109 110 111 112
  final String directory;
  final String studioAppName;
  final Version version;
  final String configured;
113
  final String presetPluginsPath;
114 115 116 117 118

  String _javaPath;
  bool _isValid = false;
  final List<String> _validationMessages = <String>[];

119 120
  String get javaPath => _javaPath;

121 122
  bool get isValid => _isValid;

123
  String get pluginsPath {
124 125 126 127 128
    if (presetPluginsPath != null) {
      return presetPluginsPath;
    }
    final int major = version?.major;
    final int minor = version?.minor;
129 130
    if (globals.platform.isMacOS) {
      return globals.fs.path.join(
131 132 133 134 135
        globals.fsUtils.homeDirPath,
        'Library',
        'Application Support',
        'AndroidStudio$major.$minor',
      );
136
    } else {
137 138 139 140 141 142
      return globals.fs.path.join(
        globals.fsUtils.homeDirPath,
        '.$studioAppName$major.$minor',
        'config',
        'plugins',
      );
143 144 145
    }
  }

146 147 148 149
  List<String> get validationMessages => _validationMessages;

  @override
  int compareTo(AndroidStudio other) {
150
    final int result = version.compareTo(other.version);
151
    if (result == 0) {
152
      return directory.compareTo(other.directory);
153
    }
154 155 156 157 158
    return result;
  }

  /// Locates the newest, valid version of Android Studio.
  static AndroidStudio latestValid() {
159
    final String configuredStudio = globals.config.getValue('android-studio-dir') as String;
160 161
    if (configuredStudio != null) {
      String configuredStudioPath = configuredStudio;
162 163
      if (globals.platform.isMacOS && !configuredStudioPath.endsWith('Contents')) {
        configuredStudioPath = globals.fs.path.join(configuredStudioPath, 'Contents');
164
      }
165
      return AndroidStudio(configuredStudioPath,
166 167 168 169
          configured: configuredStudio);
    }

    // Find all available Studio installations.
170
    final List<AndroidStudio> studios = allInstalled();
171 172 173 174 175 176 177 178 179
    if (studios.isEmpty) {
      return null;
    }
    studios.sort();
    return studios.lastWhere((AndroidStudio s) => s.isValid,
        orElse: () => null);
  }

  static List<AndroidStudio> allInstalled() =>
180
      globals.platform.isMacOS ? _allMacOS() : _allLinuxOrWindows();
181 182

  static List<AndroidStudio> _allMacOS() {
183
    final List<FileSystemEntity> candidatePaths = <FileSystemEntity>[];
184 185

    void _checkForStudio(String path) {
186
      if (!globals.fs.isDirectorySync(path)) {
187
        return;
188
      }
189
      try {
190
        final Iterable<Directory> directories = globals.fs
191
            .directory(path)
192
            .listSync(followLinks: false)
193
            .whereType<Directory>();
194
        for (final Directory directory in directories) {
195 196 197
          final String name = directory.basename;
          // An exact match, or something like 'Android Studio 3.0 Preview.app'.
          if (name.startsWith('Android Studio') && name.endsWith('.app')) {
198 199 200 201
            candidatePaths.add(directory);
          } else if (!directory.path.endsWith('.app')) {
            _checkForStudio(directory.path);
          }
202
        }
203
      } on Exception catch (e) {
204
        globals.printTrace('Exception while looking for Android Studio: $e');
205 206 207 208
      }
    }

    _checkForStudio('/Applications');
209 210 211 212
    _checkForStudio(globals.fs.path.join(
      globals.fsUtils.homeDirPath,
      'Applications',
    ));
213

214
    final String configuredStudioDir = globals.config.getValue('android-studio-dir') as String;
215
    if (configuredStudioDir != null) {
216
      FileSystemEntity configuredStudio = globals.fs.file(configuredStudioDir);
217 218 219 220 221 222 223 224 225 226
      if (configuredStudio.basename == 'Contents') {
        configuredStudio = configuredStudio.parent;
      }
      if (!candidatePaths
          .any((FileSystemEntity e) => e.path == configuredStudio.path)) {
        candidatePaths.add(configuredStudio);
      }
    }

    return candidatePaths
227
        .map<AndroidStudio>((FileSystemEntity e) => AndroidStudio.fromMacOSBundle(e.path))
228 229 230 231 232
        .where((AndroidStudio s) => s != null)
        .toList();
  }

  static List<AndroidStudio> _allLinuxOrWindows() {
233
    final List<AndroidStudio> studios = <AndroidStudio>[];
234

235
    bool _hasStudioAt(String path, { Version newerThan }) {
236
      return studios.any((AndroidStudio studio) {
237
        if (studio.directory != path) {
238
          return false;
239
        }
240 241 242 243 244 245 246
        if (newerThan != null) {
          return studio.version.compareTo(newerThan) >= 0;
        }
        return true;
      });
    }

247
    // Read all $HOME/.AndroidStudio*/system/.home files. There may be several
248
    // pointing to the same installation, so we grab only the latest one.
249 250 251 252 253 254 255 256 257 258 259
    if (globals.fsUtils.homeDirPath != null &&
        globals.fs.directory(globals.fsUtils.homeDirPath).existsSync()) {
      final Directory homeDir = globals.fs.directory(globals.fsUtils.homeDirPath);
      for (final Directory entity in homeDir.listSync(followLinks: false).whereType<Directory>()) {
        if (!entity.basename.startsWith('.AndroidStudio')) {
          continue;
        }
        final AndroidStudio studio = AndroidStudio.fromHomeDot(entity);
        if (studio != null && !_hasStudioAt(studio.directory, newerThan: studio.version)) {
          studios.removeWhere((AndroidStudio other) => other.directory == studio.directory);
          studios.add(studio);
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
    // 4.1 has a different location for AndroidStudio installs on Windows.
    if (globals.platform.isWindows) {
      final File homeDot = globals.fs.file(globals.fs.path.join(
        globals.platform.environment['LOCALAPPDATA'],
        'Google',
        'AndroidStudio4.1',
        '.home',
      ));
      if (homeDot.existsSync()) {
        final String installPath = homeDot.readAsStringSync();
        if (globals.fs.isDirectorySync(installPath)) {
          final AndroidStudio studio = AndroidStudio(
            installPath,
            version: Version(4, 1, 0),
            studioAppName: 'Android Studio 4.1',
          );
          if (studio != null && !_hasStudioAt(studio.directory, newerThan: studio.version)) {
            studios.removeWhere((AndroidStudio other) => other.directory == studio.directory);
            studios.add(studio);
          }
        }
      }
    }
286

287
    final String configuredStudioDir = globals.config.getValue('android-studio-dir') as String;
288
    if (configuredStudioDir != null && !_hasStudioAt(configuredStudioDir)) {
289
      studios.add(AndroidStudio(configuredStudioDir,
290 291 292
          configured: configuredStudioDir));
    }

293
    if (globals.platform.isLinux) {
294
      void _checkWellKnownPath(String path) {
295
        if (globals.fs.isDirectorySync(path) && !_hasStudioAt(path)) {
296
          studios.add(AndroidStudio(path));
297 298 299 300 301
        }
      }

      // Add /opt/android-studio and $HOME/android-studio, if they exist.
      _checkWellKnownPath('/opt/android-studio');
302
      _checkWellKnownPath('${globals.fsUtils.homeDirPath}/android-studio');
303 304 305 306
    }
    return studios;
  }

307 308 309 310 311 312 313
  static String extractStudioPlistValueWithMatcher(String plistValue, RegExp keyMatcher) {
    if (plistValue == null || keyMatcher == null) {
      return null;
    }
    return keyMatcher?.stringMatch(plistValue)?.split('=')?.last?.trim()?.replaceAll('"', '');
  }

314 315 316 317 318 319 320 321
  void _init() {
    _isValid = false;
    _validationMessages.clear();

    if (configured != null) {
      _validationMessages.add('android-studio-dir = $configured');
    }

322
    if (!globals.fs.isDirectorySync(directory)) {
323 324 325 326
      _validationMessages.add('Android Studio not found at $directory');
      return;
    }

327 328 329 330 331
    final String javaPath = globals.platform.isMacOS ?
        globals.fs.path.join(directory, 'jre', 'jdk', 'Contents', 'Home') :
        globals.fs.path.join(directory, 'jre');
    final String javaExecutable = globals.fs.path.join(javaPath, 'bin', 'java');
    if (!globals.processManager.canRun(javaExecutable)) {
332
      _validationMessages.add('Unable to find bundled Java version.');
333
    } else {
334 335
      RunResult result;
      try {
336
        result = globals.processUtils.runSync(<String>[javaExecutable, '-version']);
337 338 339 340
      } on ProcessException catch (e) {
        _validationMessages.add('Failed to run Java: $e');
      }
      if (result != null && result.exitCode == 0) {
341 342
        final List<String> versionLines = result.stderr.split('\n');
        final String javaVersion = versionLines.length >= 2 ? versionLines[1] : versionLines[0];
343
        _validationMessages.add('Java version $javaVersion');
344
        _javaPath = javaPath;
345
        _isValid = true;
346 347 348
      } else {
        _validationMessages.add('Unable to determine bundled Java version.');
      }
349
    }
350 351 352
  }

  @override
353
  String toString() => 'Android Studio ($version)';
354
}