6
0
mirror of https://github.com/ChristianLight/tutor.git synced 2024-12-12 14:17:46 +00:00

fix: save configs by enable/disable plugins.

before this, after enabling/disabling any plugins we should re-generate all files with tutor config save.
This commit is contained in:
Emad Rad 2023-11-06 20:05:01 +03:30 committed by Régis Behmo
parent 9a450730db
commit 61d37b436d
5 changed files with 28 additions and 18 deletions

View File

@ -0,0 +1 @@
- [Improvement] You don't have to run `tutor config save` every time you enable or disable a plugin anymore. (by @CodeWithEmad)

View File

@ -1,9 +1,9 @@
import base64 import base64
import os import os
import subprocess
import tempfile import tempfile
import unittest import unittest
from io import StringIO from io import StringIO
import subprocess
from typing import List, Tuple from typing import List, Tuple
from unittest.mock import MagicMock, mock_open, patch from unittest.mock import MagicMock, mock_open, patch

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import os import os
import sys
import tempfile import tempfile
import typing as t import typing as t
@ -9,6 +10,7 @@ import click.shell_completion
from tutor import config as tutor_config from tutor import config as tutor_config
from tutor import exceptions, fmt, hooks, plugins, utils 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 import indexes
from tutor.plugins.base import PLUGINS_ROOT, PLUGINS_ROOT_ENV_VAR_NAME from tutor.plugins.base import PLUGINS_ROOT, PLUGINS_ROOT_ENV_VAR_NAME
from tutor.types import Config from tutor.types import Config
@ -133,17 +135,15 @@ def list_command(show_enabled_only: bool) -> None:
@click.command(help="Enable a plugin") @click.command(help="Enable a plugin")
@click.argument("plugin_names", metavar="plugin", nargs=-1, type=PluginName()) @click.argument("plugin_names", metavar="plugin", nargs=-1, type=PluginName())
@click.pass_obj @click.pass_context
def enable(context: Context, plugin_names: list[str]) -> None: def enable(context: click.Context, plugin_names: list[str]) -> None:
config = tutor_config.load_minimal(context.root) config = tutor_config.load_minimal(context.obj.root)
for plugin in plugin_names: for plugin in plugin_names:
plugins.load(plugin) plugins.load(plugin)
fmt.echo_info(f"Plugin {plugin} enabled") fmt.echo_info(f"Plugin {plugin} enabled")
tutor_config.save_enabled_plugins(config) tutor_config.save_enabled_plugins(config)
tutor_config.save_config_file(context.root, config) tutor_config.save_config_file(context.obj.root, config)
fmt.echo_info( context.invoke(config_save_command, env_only=True)
"You should now re-generate your environment with `tutor config save`."
)
@click.command( @click.command(
@ -153,22 +153,20 @@ def enable(context: Context, plugin_names: list[str]) -> None:
@click.argument( @click.argument(
"plugin_names", metavar="plugin", nargs=-1, type=PluginName(allow_all=True) "plugin_names", metavar="plugin", nargs=-1, type=PluginName(allow_all=True)
) )
@click.pass_obj @click.pass_context
def disable(context: Context, plugin_names: list[str]) -> None: def disable(context: click.Context, plugin_names: list[str]) -> None:
config = tutor_config.load_minimal(context.root) config = tutor_config.load_minimal(context.obj.root)
disable_all = "all" in plugin_names disable_all = "all" in plugin_names
disabled: list[str] = [] disabled: list[str] = []
for plugin in tutor_config.get_enabled_plugins(config): for plugin in tutor_config.get_enabled_plugins(config):
if disable_all or plugin in plugin_names: if disable_all or plugin in plugin_names:
fmt.echo_info(f"Disabling plugin {plugin}...") 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) disabled.append(plugin)
fmt.echo_info(f"Plugin {plugin} disabled") fmt.echo_info(f"Plugin {plugin} disabled")
if disabled: if disabled:
tutor_config.save_config_file(context.root, config) tutor_config.save_config_file(context.obj.root, config)
fmt.echo_info( context.invoke(config_save_command, env_only=True)
"You should now re-generate your environment with `tutor config save`."
)
@click.command(name="update") @click.command(name="update")

View File

@ -1,8 +1,8 @@
from __future__ import annotations from __future__ import annotations
from copy import deepcopy
import typing as t
import os import os
import typing as t
from copy import deepcopy
from tutor import env, exceptions, fmt, hooks, plugins, serialize, utils from tutor import env, exceptions, fmt, hooks, plugins, serialize, utils
from tutor.types import Config, ConfigValue, cast_config, get_typed from tutor.types import Config, ConfigValue, cast_config, get_typed

View File

@ -32,6 +32,17 @@ def _convert_plugin_patches() -> None:
ENV_PATCHES_DICT[name].append(content) ENV_PATCHES_DICT[name].append(content)
@hooks.Actions.PLUGIN_UNLOADED.add()
def _clear_plugin_patches(plugin: str, root: str, _config: Config) -> None:
"""
After disabling a plugin, ENV_PATCHES_DICT should be cleared
and re-calculated.
This action is run after plugins have been unloaded.
"""
ENV_PATCHES_DICT.clear()
def is_installed(name: str) -> bool: def is_installed(name: str) -> bool:
""" """
Return true if the plugin is installed. Return true if the plugin is installed.