1// Copyright 2018 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'package:archive/archive.dart'; 6 7import '../base/file_system.dart'; 8import '../base/version.dart'; 9import '../convert.dart'; 10import '../doctor.dart'; 11 12class IntelliJPlugins { 13 IntelliJPlugins(this.pluginsPath); 14 15 final String pluginsPath; 16 17 static final Version kMinFlutterPluginVersion = Version(16, 0, 0); 18 19 void validatePackage( 20 List<ValidationMessage> messages, 21 List<String> packageNames, 22 String title, { 23 Version minVersion, 24 }) { 25 for (String packageName in packageNames) { 26 if (!_hasPackage(packageName)) { 27 continue; 28 } 29 30 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 '$title plugin version $versionText - the recommended minimum version is $minVersion')); 35 } else { 36 messages.add(ValidationMessage( 37 '$title plugin ${version != null ? "version $version" : "installed"}')); 38 } 39 40 return; 41 } 42 43 messages.add(ValidationMessage.error( 44 '$title plugin not installed; this adds $title specific functionality.')); 45 } 46 47 bool _hasPackage(String packageName) { 48 final String packagePath = fs.path.join(pluginsPath, packageName); 49 if (packageName.endsWith('.jar')) 50 return fs.isFileSync(packagePath); 51 return fs.isDirectorySync(packagePath); 52 } 53 54 String _readPackageVersion(String packageName) { 55 final String jarPath = packageName.endsWith('.jar') 56 ? fs.path.join(pluginsPath, packageName) 57 : 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 // rather than reading the entire file into memory. 60 try { 61 final Archive archive = 62 ZipDecoder().decodeBytes(fs.file(jarPath).readAsBytesSync()); 63 final ArchiveFile file = archive.findFile('META-INF/plugin.xml'); 64 final String content = utf8.decode(file.content); 65 const String versionStartTag = '<version>'; 66 final int start = content.indexOf(versionStartTag); 67 final int end = content.indexOf('</version>', start); 68 return content.substring(start + versionStartTag.length, end); 69 } catch (_) { 70 return null; 71 } 72 } 73} 74