intellij.dart 2.47 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2018 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 'package:archive/archive.dart';

import '../base/file_system.dart';
import '../base/version.dart';
9
import '../convert.dart';
10 11 12
import '../doctor.dart';

class IntelliJPlugins {
13
  IntelliJPlugins(this.pluginsPath);
14 15 16

  final String pluginsPath;

17
  static final Version kMinFlutterPluginVersion = Version(16, 0, 0);
18 19

  void validatePackage(
20 21 22 23 24
    List<ValidationMessage> messages,
    List<String> packageNames,
    String title, {
    Version minVersion,
  }) {
25 26 27 28 29 30
    for (String packageName in packageNames) {
      if (!_hasPackage(packageName)) {
        continue;
      }

      final String versionText = _readPackageVersion(packageName);
31
      final Version version = Version.parse(versionText);
32
      if (version != null && minVersion != null && version < minVersion) {
33
        messages.add(ValidationMessage.error(
34 35
            '$title plugin version $versionText - the recommended minimum version is $minVersion'));
      } else {
36
        messages.add(ValidationMessage(
37 38 39 40 41 42
            '$title plugin ${version != null ? "version $version" : "installed"}'));
      }

      return;
    }

43
    messages.add(ValidationMessage.error(
44 45 46 47 48 49 50 51 52 53 54 55 56 57
        '$title plugin not installed; this adds $title specific functionality.'));
  }

  bool _hasPackage(String packageName) {
    final String packagePath = fs.path.join(pluginsPath, packageName);
    if (packageName.endsWith('.jar'))
      return fs.isFileSync(packagePath);
    return fs.isDirectorySync(packagePath);
  }

  String _readPackageVersion(String packageName) {
    final String jarPath = packageName.endsWith('.jar')
        ? fs.path.join(pluginsPath, packageName)
        : fs.path.join(pluginsPath, packageName, 'lib', '$packageName.jar');
58
    // TODO(danrubel): look for a better way to extract a single 2K file from the zip
59 60 61
    // rather than reading the entire file into memory.
    try {
      final Archive archive =
62
          ZipDecoder().decodeBytes(fs.file(jarPath).readAsBytesSync());
63 64 65 66 67 68 69 70 71 72 73
      final ArchiveFile file = archive.findFile('META-INF/plugin.xml');
      final String content = utf8.decode(file.content);
      const String versionStartTag = '<version>';
      final int start = content.indexOf(versionStartTag);
      final int end = content.indexOf('</version>', start);
      return content.substring(start + versionStartTag.length, end);
    } catch (_) {
      return null;
    }
  }
}