From 6e49ca9da5e311981b745ecf6b36ff1e6e77320e Mon Sep 17 00:00:00 2001 From: Carsten Igel Date: Wed, 21 Aug 2024 19:02:14 +0200 Subject: [PATCH] fix!: Configuration restructured The configuration now relies on pdm.toml This requires pdm-pfsc in at lest 0.12.2 The configuration now uses the plugin.bump prefix The environment variable for VCS_PROVIDER has also changed. Documentation will be updated accordingly Closes #284 --- src/pdm_bump/actions/base.py | 8 +- src/pdm_bump/actions/hook.py | 21 +++--- src/pdm_bump/cli.py | 64 ++-------------- src/pdm_bump/core/config.py | 139 +++++++++++++++++++++++++---------- src/pdm_bump/plugin.py | 2 +- 5 files changed, 125 insertions(+), 109 deletions(-) diff --git a/src/pdm_bump/actions/base.py b/src/pdm_bump/actions/base.py index 759bf7b..5866cf3 100644 --- a/src/pdm_bump/actions/base.py +++ b/src/pdm_bump/actions/base.py @@ -156,7 +156,7 @@ class ExecutionContext: class _VersionActions(ActionRegistry[ExecutionContext]): - def execute( # pylint: disable=R0913 + def execute( # pylint: disable=R0913,R0914 self, /, args: "Namespace", @@ -196,8 +196,10 @@ def execute( # pylint: disable=R0913 dry_run: bool = kwargs.pop("dry_run", False) selected_command: str = kwargs.pop("selected_command") - if selected_command in known_aliases: - selected_command = known_aliases[selected_command] + selected_command = known_aliases.get( + selected_command, + selected_command + ) if selected_command not in self._items: raise ValueError( diff --git a/src/pdm_bump/actions/hook.py b/src/pdm_bump/actions/hook.py index 9e18edf..1d27095 100644 --- a/src/pdm_bump/actions/hook.py +++ b/src/pdm_bump/actions/hook.py @@ -19,6 +19,13 @@ from pdm_pfsc.hook import HookBase, HookExecutorBase from pdm_pfsc.logging import logger, traced_function +from ..core.config import ( + ALLOW_DIRTY_DEFAULT, + AUTO_TAG_DEFAULT, + COMMIT_MESSAGE_TEMPLATE_DEFAULT, + PERFORM_COMMIT_DEFAULT, + TAG_ADD_PREFIX_DEFAULT, +) from ..core.version import Pep440VersionFormatter, Version if TYPE_CHECKING: @@ -157,12 +164,8 @@ def run( class CommitChanges(HookBase): """""" - default_commit_message: "ClassVar[str]" = ( - "chore: Bump version {from} to {to}\n\n" - "Created a commit with a new version {to}.\n" - "Previous version was {from}." - ) - perform_commit: "ClassVar[bool]" = False + default_commit_message: "ClassVar[str]" = COMMIT_MESSAGE_TEMPLATE_DEFAULT + perform_commit: "ClassVar[bool]" = PERFORM_COMMIT_DEFAULT @traced_function def post_action_hook( @@ -252,9 +255,9 @@ def configure(cls, parser: "ArgumentParser") -> None: class TagChanges(HookBase): """""" - do_tag: "ClassVar[bool]" = False - allow_dirty: "ClassVar[bool]" = False - prepend_to_tag: "ClassVar[bool]" = True + do_tag: "ClassVar[bool]" = AUTO_TAG_DEFAULT + allow_dirty: "ClassVar[bool]" = ALLOW_DIRTY_DEFAULT + prepend_to_tag: "ClassVar[bool]" = TAG_ADD_PREFIX_DEFAULT @traced_function def post_action_hook( diff --git a/src/pdm_bump/cli.py b/src/pdm_bump/cli.py index 6cb49a5..8217d8e 100644 --- a/src/pdm_bump/cli.py +++ b/src/pdm_bump/cli.py @@ -11,15 +11,14 @@ # """""" -from typing import Final, Optional, Protocol +from typing import Optional, Protocol, TYPE_CHECKING -# MyPy cannot resolve this during pull request -from pdm.project.config import ConfigItem as _ConfigItem # type: ignore - -from .actions.hook import CommitChanges, TagChanges from .core.config import Config from .plugin import BumpCommand as _Command +if TYPE_CHECKING: + from pdm.project.config import ConfigItem as _ConfigItem + class _CoreLike(Protocol): """""" @@ -44,7 +43,7 @@ def register_command( raise NotImplementedError() @staticmethod - def add_config(name: str, config_item: _ConfigItem) -> None: + def add_config(name: str, config_item: "_ConfigItem") -> None: """ Parameters @@ -76,53 +75,6 @@ def main(core: _CoreLike) -> None: """ core.register_command(_Command) - config_prefix: Final[str] = Config.get_plugin_config_key_prefix() - env_var_prefix: Final[str] = "PDM_BUMP_" - - core.add_config( - f"{config_prefix}.commit_msg_tpl", - _ConfigItem( - "The default commit message. Uses templates 'from' and 'to'.", - CommitChanges.default_commit_message, - ), - ) - core.add_config( - f"{config_prefix}.perform_commit", - _ConfigItem( - "If set to true, commit the bumped changes automatically", - CommitChanges.perform_commit, - ), - ) - core.add_config( - f"{config_prefix}.auto_tag", - _ConfigItem( - "Create a tag after bumping and committing the changes", - TagChanges.do_tag, - ), - ) - core.add_config( - f"{config_prefix}.tag_add_prefix", - _ConfigItem( - "Adds the prefix v to the version tag", - TagChanges.prepend_to_tag, - ), - ) - core.add_config( - f"{config_prefix}.allow_dirty", - _ConfigItem( - "Allows tagging the project, if it is dirty", - TagChanges.allow_dirty, - ), - ) - - vcs_config_namespace: Final[str] = f"{config_prefix}.vcs" - vcs_env_var_prefix: Final[str] = f"{env_var_prefix}VCS_" - - core.add_config( - f"{vcs_config_namespace}.provider", - _ConfigItem( - "Configures the VCS Provider to use.", - "git-cli", - env_var=f"{vcs_env_var_prefix}PROVIDER", - ), - ) + config_map = Config.get_config() + + config_map.propagate(core) diff --git a/src/pdm_bump/core/config.py b/src/pdm_bump/core/config.py index a912701..9ffdbea 100644 --- a/src/pdm_bump/core/config.py +++ b/src/pdm_bump/core/config.py @@ -12,11 +12,11 @@ """""" from enum import Enum from functools import cached_property -from typing import TYPE_CHECKING, Any, Final, Optional +from typing import TYPE_CHECKING, Any, Final, Optional, Protocol from pdm_pfsc.config import ( ConfigAccessor, - ConfigNamespace, + ConfigItems, ConfigSection, IsMissing, ) @@ -26,8 +26,20 @@ import argparse from collections.abc import Iterable, Mapping from pathlib import Path + from types import SimpleNamespace + + from pdm.project.config import ConfigItem from pdm_pfsc.abstractions import ConfigHolder + from pdm_pfsc.config import Config as SupportsConfigFile + + +class _SupportsAddConfigItems(Protocol): # pylint: disable=R0903 + """""" + + def add_config(self, name: str, value: "ConfigItem") -> None: + """""" + raise NotImplementedError() class _StringEnum(str, Enum): @@ -50,6 +62,17 @@ class _ConfigKeys(_StringEnum): VERSION_CONFIG_KEY_NAME: "Final[str]" = _ConfigKeys.VERSION +COMMIT_MESSAGE_TEMPLATE_DEFAULT: "Final[str]" = ( + "chore: Bump version {from} to {to}\n\n" + "Created a commit with a new version {to}.\n" + "Previous version was {from}." +) +PERFORM_COMMIT_DEFAULT: "Final[bool]" = False +AUTO_TAG_DEFAULT: "Final[bool]" = False +TAG_ADD_PREFIX_DEFAULT: "Final[bool]" = True +ALLOW_DIRTY_DEFAULT: "Final[bool]" = False +VCS_PROVIDER_DEFAULT: "Final[str]" = "git-cli" + class _ConfigValues(_StringEnum): """""" @@ -60,61 +83,47 @@ class _ConfigValues(_StringEnum): BUILD_BACKEND_PDM_BACKEND: "Final[str]" = "pdm.backend" -class _ConfigSections(_StringEnum): - """""" - - PDM_BUMP: "Final[str]" = "pdm_bump" - PDM_BUMP_VCS: "Final[str]" = "vcs" - - -class _PdmBumpVcsConfig(ConfigNamespace): - def __init__( - self, accessor: "ConfigAccessor", pdm_bump: "PdmBumpConfig" - ) -> None: - super().__init__(_ConfigSections.PDM_BUMP_VCS, accessor, pdm_bump) - - @property - def provider(self) -> str: - """""" - return self._get_value(_ConfigKeys.VCS_PROVIDER) - - -class PdmBumpConfig(ConfigNamespace): +class PdmBumpConfig: """""" - def __init__(self, accessor: "ConfigAccessor") -> None: - super().__init__(_ConfigSections.PDM_BUMP, accessor, None) - self.__vcs = _PdmBumpVcsConfig(accessor, self) + def __init__(self, accessor: "_PdmBumpConfigAccessor") -> None: + namespace = accessor.values + self.__vcs_provider: str = namespace.vcs_provider + self.__commit_msg_tpl: str = namespace.commit_msg_tpl + self.__perform_commit: bool = namespace.perform_commit + self.__auto_tag: bool = namespace.auto_tag + self.__tag_add_prefix: bool = namespace.tag_add_prefix + self.__allow_dirty: bool = namespace.allow_dirty @property - def vcs(self) -> "_PdmBumpVcsConfig": + def vcs_provider(self) -> "str": """""" - return self.__vcs + return self.__vcs_provider @property def commit_msg_tpl(self) -> str: """""" - return self._get_value("commit_msg_tpl") + return self.__commit_msg_tpl @property def perform_commit(self) -> bool: """""" - return self._get_value("perform_commit") or False + return self.__perform_commit @property def auto_tag(self) -> bool: """""" - return self._get_value("auto_tag") or False + return self.__auto_tag @property def tag_allow_dirty(self) -> bool: """""" - return self._get_value("allow_dirty") or False + return self.__allow_dirty @property def tag_add_prefix(self) -> bool: """""" - return self._get_value("tag_add_prefix") or True + return self.__tag_add_prefix def add_values_missing_in_cli( self, args: "argparse.Namespace" @@ -256,9 +265,60 @@ def is_dynamic_version(self) -> bool: class _PdmBumpConfigAccessor(ConfigAccessor): + def __init__( + self, + config: "SupportsConfigFile", + cfg_holder: "ConfigHolder", + ) -> None: + super().__init__(config, cfg_holder) + self.__items = ConfigItems(self) + self.__items.add_config_value( + "commit_msg_tpl", + description=("The default commit message. " + "Uses templates 'from' and 'to'."), + default=COMMIT_MESSAGE_TEMPLATE_DEFAULT, + ) + self.__items.add_config_value( + "perform_commit", + description=("If set to true, commit the " + "bumped changes automatically"), + default=PERFORM_COMMIT_DEFAULT, + ) + self.__items.add_config_value( + "auto_tag", + description=("Create a tag after bumping " + "and committing the changes"), + default=AUTO_TAG_DEFAULT, + ) + self.__items.add_config_value( + "tag_add_prefix", + description="Adds the prefix v to the version tag", + default=TAG_ADD_PREFIX_DEFAULT, + ) + self.__items.add_config_value( + "allow_dirty", + description="Allows tagging the project, if it is dirty", + default=ALLOW_DIRTY_DEFAULT, + ) + self.__items.add_config_value( + "vcs_provider", + description="Configures the VCS Provider to use.", + default=VCS_PROVIDER_DEFAULT, + use_env_var=True, + ) + @property def plugin_config_name(self) -> "Iterable[str]": - return {"bump-plugin"} + return {"bump"} + + @property + def values(self) -> "SimpleNamespace": + """""" + return self.__items.to_namespace() + + def propagate(self, target: "_SupportsAddConfigItems") -> None: + """""" + self.__items.propagate(target) class _EmptyConfig: @@ -305,22 +365,21 @@ class Config: def __init__(self, project: "ConfigHolder") -> None: self.__project: "ConfigHolder" = project - accessor: "ConfigAccessor" = _PdmBumpConfigAccessor(self, project) + accessor: "_PdmBumpConfigAccessor" = _PdmBumpConfigAccessor( + self, project + ) self.__pdm_bump = PdmBumpConfig(accessor) self.__meta_data = _MetaDataConfig(accessor) @staticmethod - def get_plugin_config_key_prefix() -> str: + def get_config() -> "_PdmBumpConfigAccessor": """""" config: "ConfigHolder" = _EmptyConfig() - accessor: "ConfigAccessor" = _PdmBumpConfigAccessor( + accessor: "_PdmBumpConfigAccessor" = _PdmBumpConfigAccessor( _DummyConfig(), config ) - section: "ConfigSection" = ConfigSection.PLUGIN_CONFIG - paths: "Iterable[str]" - paths = accessor.get_config_section_path(section) - return ".".join(paths) + return accessor @property def pdm_bump(self) -> "PdmBumpConfig": diff --git a/src/pdm_bump/plugin.py b/src/pdm_bump/plugin.py index e6734b6..2488696 100644 --- a/src/pdm_bump/plugin.py +++ b/src/pdm_bump/plugin.py @@ -192,7 +192,7 @@ def _get_vcs_provider(self, project: "_ProjectLike") -> VcsProvider: """ config: "Config" = Config(project) - value = config.pdm_bump.vcs.provider + value = config.pdm_bump.vcs_provider registry: "VcsProviderRegistry" = vcs_providers