getPluginInstallationFromV2 method

({String? projectPath, PluginScope scope}) getPluginInstallationFromV2(
  1. String pluginId
)

Get the most relevant installation for a plugin from V2 data.

Implementation

({PluginScope scope, String? projectPath}) getPluginInstallationFromV2(
  String pluginId,
) {
  final data = loadInstalledPluginsV2();
  final installations = data.plugins[pluginId];
  if (installations == null || installations.isEmpty) {
    return (scope: PluginScope.user, projectPath: null);
  }

  final currentProjectPath = getOriginalCwd();

  final localInstall = installations
      .cast<PluginInstallationRecord?>()
      .firstWhere(
        (i) =>
            i!.scope == PluginScope.local &&
            i.projectPath == currentProjectPath,
        orElse: () => null,
      );
  if (localInstall != null) {
    return (scope: localInstall.scope, projectPath: localInstall.projectPath);
  }

  final projectInstall = installations
      .cast<PluginInstallationRecord?>()
      .firstWhere(
        (i) =>
            i!.scope == PluginScope.project &&
            i.projectPath == currentProjectPath,
        orElse: () => null,
      );
  if (projectInstall != null) {
    return (
      scope: projectInstall.scope,
      projectPath: projectInstall.projectPath,
    );
  }

  final userInstall = installations
      .cast<PluginInstallationRecord?>()
      .firstWhere((i) => i!.scope == PluginScope.user, orElse: () => null);
  if (userInstall != null) {
    return (scope: userInstall.scope, projectPath: null);
  }

  return (
    scope: installations.first.scope,
    projectPath: installations.first.projectPath,
  );
}