installPlugin method

Future<PluginOperationResult> installPlugin(
  1. String plugin, [
  2. PluginScope scope = PluginScope.user
])

Install a plugin (settings-first).

Implementation

Future<PluginOperationResult> installPlugin(
  String plugin, [
  PluginScope scope = PluginScope.user,
]) async {
  assertInstallableScope(scope);
  final id = parsePluginIdentifier(plugin);

  final entry = await getPluginById(plugin);
  if (entry == null) {
    final location = id.marketplace != null
        ? 'marketplace "${id.marketplace}"'
        : 'any configured marketplace';
    return PluginOperationResult(
      success: false,
      message: 'Plugin "${id.name}" not found in $location',
    );
  }

  final pluginId = '${entry.name}@${id.marketplace ?? 'default'}';

  if (isPluginBlockedByPolicy(pluginId)) {
    return PluginOperationResult(
      success: false,
      message:
          'Plugin "${entry.name}" is blocked by your organization\'s policy '
          'and cannot be installed',
    );
  }

  // Write settings (the intent).
  final settingSource = scopeToSettingSource(scope);
  final current = getSettingsEnabledPlugins(settingSource) ?? {};
  updateSettings(settingSource, {
    'enabledPlugins': {...current, pluginId: true},
  });
  clearAllCaches();

  return PluginOperationResult(
    success: true,
    message:
        'Successfully installed plugin: $pluginId (scope: ${scope.name})',
    pluginId: pluginId,
    pluginName: entry.name,
    scope: scope,
  );
}