updatePlugin method

Future<PluginUpdateResult> updatePlugin(
  1. String plugin,
  2. PluginScope scope
)

Update a plugin to the latest version.

Fetches the current marketplace entry, compares versions, and copies the new version to the versioned cache. Returns PluginUpdateResult.alreadyUpToDate when no update is needed.

Implementation

Future<PluginUpdateResult> updatePlugin(
  String plugin,
  PluginScope scope,
) async {
  final id = parsePluginIdentifier(plugin);
  final pluginId = id.fullId;

  final entry = await getPluginByIdFn(plugin);
  if (entry == null) {
    return PluginUpdateResult(
      success: false,
      message: 'Plugin "${id.name}" not found',
      pluginId: pluginId,
      scope: scope,
    );
  }

  final installedData = loadInstalledPluginsV2();
  final installations = installedData[pluginId];

  if (installations == null || installations.isEmpty) {
    return PluginUpdateResult(
      success: false,
      message: 'Plugin "${id.name}" is not installed',
      pluginId: pluginId,
      scope: scope,
    );
  }

  final projectPath = getProjectPathForScope(scope);
  final installation = installations.cast<PluginInstallationRecord?>().firstWhere(
    (i) => i!.scope == scope && i.projectPath == projectPath,
    orElse: () => null,
  );

  if (installation == null) {
    final scopeDesc =
        projectPath != null ? '${scope.name} ($projectPath)' : scope.name;
    return PluginUpdateResult(
      success: false,
      message:
          'Plugin "${id.name}" is not installed at scope $scopeDesc',
      pluginId: pluginId,
      scope: scope,
    );
  }

  final oldVersion = installation.version;
  final newVersion = entry.version;

  if (oldVersion == newVersion) {
    return PluginUpdateResult(
      success: true,
      message:
          '${id.name} is already at the latest version ($newVersion).',
      pluginId: pluginId,
      newVersion: newVersion,
      oldVersion: oldVersion,
      alreadyUpToDate: true,
      scope: scope,
    );
  }

  // In a full implementation, this would download/copy the new version
  // to the versioned cache and update the V2 installation record.
  // For now, return a success indicating what would happen.
  final scopeDesc =
      projectPath != null ? '${scope.name} ($projectPath)' : scope.name;
  return PluginUpdateResult(
    success: true,
    message:
        'Plugin "${id.name}" updated from ${oldVersion ?? 'unknown'} '
        'to $newVersion for scope $scopeDesc. '
        'Restart to apply changes.',
    pluginId: pluginId,
    newVersion: newVersion,
    oldVersion: oldVersion,
    scope: scope,
  );
}