Skip to content

Commit

Permalink
Merge pull request #112 from callowayproject/version-refactor
Browse files Browse the repository at this point in the history
Version refactor
  • Loading branch information
coordt authored Jan 27, 2024
2 parents f4cf5b2 + 8bfa5d9 commit 4439fd2
Show file tree
Hide file tree
Showing 30 changed files with 1,904 additions and 678 deletions.
2 changes: 2 additions & 0 deletions bumpversion/autocast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Automatically detect the true Python type of a string and cast it to the correct type.
Based on https://github.com/cgreer/cgAutoCast/blob/master/cgAutoCast.py
Only used by Legacy configuration file parser.
"""

import contextlib
Expand Down
8 changes: 4 additions & 4 deletions bumpversion/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

if TYPE_CHECKING: # pragma: no-coverage
from bumpversion.files import ConfiguredFile
from bumpversion.version_part import Version
from bumpversion.versioning.models import Version

from bumpversion.config import Config
from bumpversion.config.files import update_config_file
Expand Down Expand Up @@ -42,11 +42,11 @@ def get_next_version(
elif version_part:
logger.info("Attempting to increment part '%s'", version_part)
logger.indent()
next_version = current_version.bump(version_part, config.version_config.order)
next_version = current_version.bump(version_part)
else:
raise ConfigurationError("Unable to get the next version.")

logger.info("Values are now: %s", key_val_string(next_version.values))
logger.info("Values are now: %s", key_val_string(next_version.components))
logger.dedent()
return next_version

Expand Down Expand Up @@ -113,7 +113,7 @@ def commit_and_tag(
dry_run: bool = False,
) -> None:
"""
Commit and tag the changes, if a tool is configured.
Commit and tag the changes if a tool is configured.
Args:
config: The configuration
Expand Down
2 changes: 1 addition & 1 deletion bumpversion/config/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

if TYPE_CHECKING: # pragma: no-coverage
from bumpversion.config.models import Config
from bumpversion.version_part import Version
from bumpversion.versioning.models import Version

logger = get_indented_logger(__name__)

Expand Down
20 changes: 9 additions & 11 deletions bumpversion/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,16 @@
from pydantic_settings import BaseSettings, SettingsConfigDict

from bumpversion.ui import get_indented_logger
from bumpversion.versioning.models import VersionComponentConfig # NOQA: TCH001

if TYPE_CHECKING:
from bumpversion.scm import SCMInfo
from bumpversion.version_part import VersionConfig
from bumpversion.versioning.models import VersionSpec

logger = get_indented_logger(__name__)


class VersionPartConfig(BaseModel):
"""Configuration of a part of the version."""

values: Optional[list] = None # Optional. Numeric is used if missing or no items in list
optional_value: Optional[str] = None # Optional.
# Defaults to first value. 0 in the case of numeric. Empty string means nothing is optional.
first_value: Union[str, int, None] = None # Optional. Defaults to first value in values
independent: bool = False


class FileChange(BaseModel):
"""A change to make to a file."""

Expand Down Expand Up @@ -100,7 +92,7 @@ class Config(BaseSettings):
message: str
commit_args: Optional[str]
scm_info: Optional["SCMInfo"]
parts: Dict[str, VersionPartConfig]
parts: Dict[str, VersionComponentConfig]
files: List[FileChange] = Field(default_factory=list)
included_paths: List[str] = Field(default_factory=list)
excluded_paths: List[str] = Field(default_factory=list)
Expand Down Expand Up @@ -169,3 +161,9 @@ def version_config(self) -> "VersionConfig":
from bumpversion.version_part import VersionConfig

return VersionConfig(self.parse, self.serialize, self.search, self.replace, self.parts)

def version_spec(self, version: Optional[str] = None) -> "VersionSpec":
"""Return the version specification."""
from bumpversion.versioning.models import VersionSpec

return VersionSpec(self.parts)
30 changes: 20 additions & 10 deletions bumpversion/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from __future__ import annotations

import glob
import itertools
from typing import Dict, List

from bumpversion.config.models import FileChange, VersionPartConfig
from bumpversion.utils import labels_for_format
from bumpversion.config.models import FileChange
from bumpversion.exceptions import BumpVersionError
from bumpversion.versioning.models import VersionComponentConfig


def get_all_file_configs(config_dict: dict) -> List[FileChange]:
Expand All @@ -25,15 +25,25 @@ def get_all_file_configs(config_dict: dict) -> List[FileChange]:
return [FileChange(**f) for f in files]


def get_all_part_configs(config_dict: dict) -> Dict[str, VersionPartConfig]:
def get_all_part_configs(config_dict: dict) -> Dict[str, VersionComponentConfig]:
"""Make sure all version parts are included."""
serialize = config_dict["serialize"]
import re

try:
parsing_groups = list(re.compile(config_dict["parse"]).groupindex.keys())
except re.error as e:
raise BumpVersionError(f"Could not parse regex '{config_dict['parse']}': {e}") from e
parts = config_dict["parts"]
all_labels = set(itertools.chain.from_iterable([labels_for_format(fmt) for fmt in serialize]))
return {
label: VersionPartConfig(**parts[label]) if label in parts else VersionPartConfig() # type: ignore[call-arg]
for label in all_labels
}

part_configs = {}
for label in parsing_groups:
is_independent = label.startswith("$")
part_configs[label] = (
VersionComponentConfig(**parts[label])
if label in parts
else VersionComponentConfig(independent=is_independent)
)
return part_configs


def resolve_glob_files(file_cfg: FileChange) -> List[FileChange]:
Expand Down
7 changes: 4 additions & 3 deletions bumpversion/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
from pathlib import Path
from typing import Dict, List, MutableMapping, Optional

from bumpversion.config.models import FileChange, VersionPartConfig
from bumpversion.config.models import FileChange
from bumpversion.exceptions import VersionNotFoundError
from bumpversion.ui import get_indented_logger
from bumpversion.utils import get_nested_value, set_nested_value
from bumpversion.version_part import Version, VersionConfig
from bumpversion.version_part import VersionConfig
from bumpversion.versioning.models import Version, VersionComponentConfig

logger = get_indented_logger(__name__)

Expand Down Expand Up @@ -299,7 +300,7 @@ class DataFileUpdater:
def __init__(
self,
file_change: FileChange,
version_part_configs: Dict[str, VersionPartConfig],
version_part_configs: Dict[str, VersionComponentConfig],
) -> None:
self.file_change = file_change
self.version_config = VersionConfig(
Expand Down
2 changes: 1 addition & 1 deletion bumpversion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
if TYPE_CHECKING: # pragma: no-coverage
from bumpversion.config import Config
from bumpversion.scm import SCMInfo
from bumpversion.version_part import Version
from bumpversion.versioning.models import Version


def extract_regex_flags(regex_pattern: str) -> Tuple[str, str]:
Expand Down
Loading

0 comments on commit 4439fd2

Please sign in to comment.