Skip to content

Commit

Permalink
fix: save configs by enable/disable plugins.
Browse files Browse the repository at this point in the history
before this, after enabling/disabling any plugins we should re-generate all files with tutor config save.
  • Loading branch information
CodeWithEmad committed Nov 6, 2023
1 parent 0a73b88 commit 466ddec
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog.d/20231011_000434_codewithemad_save_configs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [Improvement] You don't have to run `tutor config save` every time you enable or disable a plugin anymore. (by @CodeWithEmad)
10 changes: 10 additions & 0 deletions tests/commands/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ def test_plugins_disable_not_installed_plugin(self) -> None:
self.assertEqual(0, result.exit_code)
self.assertFalse(result.exception)

def test_plugins_enable_with_no_plugin_passed(self) -> None:
result = self.invoke(["plugins", "enable"])
self.assertEqual(1, result.exit_code)
self.assertTrue(result.exception)

def test_plugins_disable_with_no_plugin_passed(self) -> None:
result = self.invoke(["plugins", "disable"])
self.assertEqual(1, result.exit_code)
self.assertTrue(result.exception)

@patch.object(
plugins,
"iter_info",
Expand Down
34 changes: 19 additions & 15 deletions tutor/commands/plugins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import sys
import tempfile
import typing as t

Expand All @@ -9,6 +10,7 @@

from tutor import config as tutor_config
from tutor import exceptions, fmt, hooks, plugins, utils
from tutor.commands.config import save as config_save_command
from tutor.plugins import indexes
from tutor.plugins.base import PLUGINS_ROOT, PLUGINS_ROOT_ENV_VAR_NAME
from tutor.types import Config
Expand Down Expand Up @@ -133,17 +135,18 @@ def list_command(show_enabled_only: bool) -> None:

@click.command(help="Enable a plugin")
@click.argument("plugin_names", metavar="plugin", nargs=-1, type=PluginName())
@click.pass_obj
def enable(context: Context, plugin_names: list[str]) -> None:
config = tutor_config.load_minimal(context.root)
@click.pass_context
def enable(context: click.Context, plugin_names: list[str]) -> None:
if not plugin_names:
fmt.echo_error("No plugin names provided.")
sys.exit(1)
config = tutor_config.load_minimal(context.obj.root)
for plugin in plugin_names:
plugins.load(plugin)
fmt.echo_info(f"Plugin {plugin} enabled")
tutor_config.save_enabled_plugins(config)
tutor_config.save_config_file(context.root, config)
fmt.echo_info(
"You should now re-generate your environment with `tutor config save`."
)
tutor_config.save_config_file(context.obj.root, config)
context.invoke(config_save_command, env_only=True)


@click.command(
Expand All @@ -153,22 +156,23 @@ def enable(context: Context, plugin_names: list[str]) -> None:
@click.argument(
"plugin_names", metavar="plugin", nargs=-1, type=PluginName(allow_all=True)
)
@click.pass_obj
def disable(context: Context, plugin_names: list[str]) -> None:
config = tutor_config.load_minimal(context.root)
@click.pass_context
def disable(context: click.Context, plugin_names: list[str]) -> None:
if not plugin_names:
fmt.echo_error("No plugin names provided.")
sys.exit(1)
config = tutor_config.load_minimal(context.obj.root)
disable_all = "all" in plugin_names
disabled: list[str] = []
for plugin in tutor_config.get_enabled_plugins(config):
if disable_all or plugin in plugin_names:
fmt.echo_info(f"Disabling plugin {plugin}...")
hooks.Actions.PLUGIN_UNLOADED.do(plugin, context.root, config)
hooks.Actions.PLUGIN_UNLOADED.do(plugin, context.obj.root, config)
disabled.append(plugin)
fmt.echo_info(f"Plugin {plugin} disabled")
if disabled:
tutor_config.save_config_file(context.root, config)
fmt.echo_info(
"You should now re-generate your environment with `tutor config save`."
)
tutor_config.save_config_file(context.obj.root, config)
context.invoke(config_save_command, env_only=True)


@click.command(name="update")
Expand Down

0 comments on commit 466ddec

Please sign in to comment.