-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from callowayproject/sample-config
Added sample-config feature
- Loading branch information
Showing
47 changed files
with
2,845 additions
and
1,355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"""Module for creating a new config file.""" | ||
from pathlib import Path | ||
from typing import Tuple | ||
|
||
import questionary | ||
from tomlkit import TOMLDocument | ||
|
||
|
||
def create_configuration(destination: str, prompt: bool) -> TOMLDocument: | ||
""" | ||
Create a new configuration as a TOMLDocument. | ||
Args: | ||
destination: `stdout` or a path to a new or existing file. | ||
prompt: `True` if the user should be prompted for input. | ||
Returns: | ||
The TOMLDocument structure with the updated configuration. | ||
""" | ||
config, destination_config = get_defaults_from_dest(destination) | ||
|
||
if prompt: | ||
allow_dirty_default = "(Y/n)" if config["allow_dirty"] else "(y/N)" | ||
answers = questionary.form( | ||
current_version=questionary.text("What is the current version?", default=config["current_version"]), | ||
commit=questionary.confirm( | ||
"Commit changes made when bumping to version control?", default=config["commit"] | ||
), | ||
allow_dirty=questionary.confirm( | ||
"Allow dirty working directory when bumping?", | ||
default=config["allow_dirty"], | ||
instruction=( | ||
"If you are also creating or modifying other files (e.g. a CHANGELOG), say Yes. " | ||
f"{allow_dirty_default} " | ||
), | ||
), | ||
tag=questionary.confirm("Tag changes made when bumping in version control?", default=config["tag"]), | ||
commit_args=questionary.text( | ||
"Any extra arguments to pass to the commit command?", | ||
default=config["commit_args"] or "", | ||
instruction="For example, `--no-verify` is useful if you have a pre-commit hook. ", | ||
), | ||
).ask() | ||
config.update(answers) | ||
|
||
for key, val in config.items(): | ||
destination_config["tool"]["bumpversion"][key] = val if val is not None else "" | ||
|
||
return destination_config | ||
|
||
|
||
def get_defaults_from_dest(destination: str) -> Tuple[dict, TOMLDocument]: | ||
"""Get the default configuration and the configuration from the destination.""" | ||
from tomlkit import document, parse | ||
|
||
from bumpversion.config import DEFAULTS | ||
|
||
config = DEFAULTS.copy() | ||
if Path(destination).exists(): | ||
destination_config = parse(Path(destination).read_text()) | ||
else: | ||
destination_config = document() | ||
|
||
destination_config.setdefault("tool", {}) | ||
destination_config["tool"].setdefault("bumpversion", {}) | ||
existing_config = destination_config["tool"]["bumpversion"] | ||
if existing_config: | ||
config.update(existing_config) | ||
|
||
project_config = destination_config.get("project", {}).get("version") | ||
config["current_version"] = config["current_version"] or project_config or "0.1.0" | ||
del config["scm_info"] | ||
del config["parts"] | ||
del config["files"] | ||
|
||
return config, destination_config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
"""Visualize the bumpversion process.""" | ||
from dataclasses import dataclass | ||
from typing import List, Optional | ||
|
||
from bumpversion.bump import get_next_version | ||
from bumpversion.config import Config | ||
from bumpversion.exceptions import BumpVersionError | ||
from bumpversion.ui import print_info | ||
from bumpversion.utils import base_context, get_context | ||
|
||
BOX_CHARS = { | ||
"ascii": ["+", "+", "+", "+", "+", "+", "+", "+", "-", "|", "+"], | ||
"light": ["╯", "╮", "╭", "╰", "┤", "┴", "┬", "├", "─", "│", "┼"], | ||
} | ||
|
||
|
||
@dataclass | ||
class Border: | ||
"""A border definition.""" | ||
|
||
corner_bottom_right: str | ||
corner_top_right: str | ||
corner_top_left: str | ||
corner_bottom_left: str | ||
divider_left: str | ||
divider_up: str | ||
divider_down: str | ||
divider_right: str | ||
line: str | ||
pipe: str | ||
cross: str | ||
|
||
|
||
def lead_string(version_str: str, border: Border, blank: bool = False) -> str: | ||
""" | ||
Return the first part of a string with the bump character or spaces of the correct amount. | ||
Examples: | ||
>>> lead_string("1.0.0", Border(*BOX_CHARS["light"])) | ||
'1.0.0 ── bump ─' | ||
>>> lead_string("1.0.0", Border(*BOX_CHARS["light"]), blank=True) | ||
' ' | ||
Args: | ||
version_str: The string to render as the starting point | ||
border: The border definition to draw the lines | ||
blank: If `True`, return a blank string the same length as the version bump string | ||
Returns: | ||
The version bump string or a blank string | ||
""" | ||
version_bump = f"{version_str} {border.line * 2} bump {border.line}" | ||
return " " * len(version_bump) if blank else version_bump | ||
|
||
|
||
def connection_str(border: Border, has_next: bool = False, has_previous: bool = False) -> str: | ||
""" | ||
Return the correct connection string based on the next and previous. | ||
Args: | ||
border: The border definition to draw the lines | ||
has_next: If `True`, there is a next line | ||
has_previous: If `True`, there is a previous line | ||
Returns: | ||
A string that connects left-to-right and top-to-bottom based on the next and previous | ||
""" | ||
if has_next and has_previous: | ||
return border.divider_right + border.line | ||
elif has_next: | ||
return border.divider_down + border.line | ||
elif has_previous: | ||
return border.corner_bottom_left + border.line | ||
else: | ||
return border.line * 2 | ||
|
||
|
||
def labeled_line(label: str, border: Border, fit_length: Optional[int] = None) -> str: | ||
""" | ||
Return the version part string with the correct padding. | ||
Args: | ||
label: The label to render | ||
border: The border definition to draw the lines | ||
fit_length: The length to fit the label to | ||
Returns: | ||
A labeled line with leading and trailing spaces | ||
""" | ||
if fit_length is None: | ||
fit_length = len(label) | ||
return f" {label} {border.line * (fit_length - len(label))}{border.line} " | ||
|
||
|
||
def filter_version_parts(config: Config) -> List[str]: | ||
""" | ||
Return the version parts that are in the configuration. | ||
Args: | ||
config: The configuration to check against | ||
Returns: | ||
The version parts that are in the configuration | ||
""" | ||
version_parts = [part for part in config.version_config.order if not part.startswith("$")] | ||
default_context = base_context(config.scm_info) | ||
return [part for part in version_parts if part not in default_context] | ||
|
||
|
||
def visualize(config: Config, version_str: str, box_style: str = "light") -> None: | ||
"""Output a visualization of the bump-my-version bump process.""" | ||
version = config.version_config.parse(version_str) | ||
version_parts = filter_version_parts(config) | ||
num_parts = len(version_parts) | ||
|
||
box_style = box_style if box_style in BOX_CHARS else "light" | ||
border = Border(*BOX_CHARS[box_style]) | ||
|
||
version_lead = lead_string(version_str, border) | ||
blank_lead = lead_string(version_str, border, blank=True) | ||
version_part_length = max(len(part) for part in version_parts) | ||
|
||
for i, part in enumerate(version_parts): | ||
line = [version_lead] if i == 0 else [blank_lead] | ||
|
||
try: | ||
next_version = get_next_version(version, config, part, None) | ||
next_version_str = config.version_config.serialize(next_version, get_context(config)) | ||
except (BumpVersionError, ValueError) as e: | ||
next_version_str = f"invalid: {e}" | ||
|
||
has_next = i < num_parts - 1 | ||
has_previous = i > 0 | ||
line.append(connection_str(border, has_next=has_next, has_previous=has_previous)) | ||
line.append(labeled_line(part, border, version_part_length)) | ||
line.append(next_version_str) | ||
print_info("".join(line)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
```{include} ../LICENSE | ||
``` |
Oops, something went wrong.