Skip to content

Commit

Permalink
fix!: Configuration restructured
Browse files Browse the repository at this point in the history
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
  • Loading branch information
carstencodes committed Aug 21, 2024
1 parent 371f519 commit 6e49ca9
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 109 deletions.
8 changes: 5 additions & 3 deletions src/pdm_bump/actions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class ExecutionContext:


class _VersionActions(ActionRegistry[ExecutionContext]):
def execute( # pylint: disable=R0913
def execute( # pylint: disable=R0913,R0914
self,
/,
args: "Namespace",
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 12 additions & 9 deletions src/pdm_bump/actions/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
64 changes: 8 additions & 56 deletions src/pdm_bump/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
""""""
Expand All @@ -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
Expand Down Expand Up @@ -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)
139 changes: 99 additions & 40 deletions src/pdm_bump/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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):
Expand All @@ -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):
""""""
Expand All @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion src/pdm_bump/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 6e49ca9

Please sign in to comment.