Skip to content

Commit f529d28

Browse files
committed
Refactored the create subcommand
- Also organized the CLI tests
1 parent 3d0f67d commit f529d28

File tree

10 files changed

+880
-699
lines changed

10 files changed

+880
-699
lines changed

bumpversion/cli.py

+7-55
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
from pathlib import Path
33
from typing import List, Optional
44

5+
import questionary
56
import rich_click as click
67
from click.core import Context
8+
from tomlkit import dumps
79

810
from bumpversion import __version__
911
from bumpversion.aliases import AliasedGroup
1012
from bumpversion.bump import do_bump
1113
from bumpversion.config import get_configuration
14+
from bumpversion.config.create import create_configuration
1215
from bumpversion.config.files import find_config_file
1316
from bumpversion.files import ConfiguredFile, modify_files
1417
from bumpversion.show import do_show, log_list
@@ -533,65 +536,14 @@ def replace(
533536
)
534537
def sample_config(prompt: bool, destination: str) -> None:
535538
"""Print a sample configuration file."""
536-
import questionary
537-
from tomlkit import document, dumps, parse
538-
539-
from bumpversion.config import DEFAULTS
540-
541-
config = DEFAULTS.copy()
542539
if prompt:
543540
destination = questionary.select(
544541
"Destination", choices=["stdout", ".bumpversion.toml", "pyproject.toml"], default=destination
545542
).ask()
546-
destination_path = None
547-
548-
if destination != "stdout":
549-
destination_path = Path(destination)
550-
destination_path.touch(exist_ok=True)
551-
destination_config = parse(destination_path.read_text())
552-
existing_config = destination_config.get("tool", {}).get("bumpversion", {})
553-
if existing_config:
554-
logger.info("Found existing configuration in %s. Loading as defaults.", destination_path)
555-
config.update(existing_config)
556-
else:
557-
destination_config = document()
558-
destination_config.update({"tool": {"bumpversion": {}}})
559-
560-
config["current_version"] = config["current_version"] or destination_config.get("project", {}).get(
561-
"version", "0.1.0"
562-
)
563-
del config["scm_info"]
564-
del config["parts"]
565-
del config["files"]
566-
567-
if prompt:
568-
allow_dirty_default = "(Y/n)" if config["allow_dirty"] else "(y/N)"
569-
answers = questionary.form(
570-
current_version=questionary.text("What is the current version?", default=config["current_version"]),
571-
commit=questionary.confirm(
572-
"Commit changes made when bumping to version control?", default=config["commit"]
573-
),
574-
allow_dirty=questionary.confirm(
575-
"Allow dirty working directory when bumping?",
576-
default=config["allow_dirty"],
577-
instruction=(
578-
"If you are also creating or modifying other files (e.g. a CHANGELOG), say Yes. "
579-
f"{allow_dirty_default} "
580-
),
581-
),
582-
tag=questionary.confirm("Tag changes made when bumping in version control?", default=config["tag"]),
583-
commit_args=questionary.text(
584-
"Any extra arguments to pass to the commit command?",
585-
default=config["commit_args"] or "",
586-
instruction="For example, `--no-verify` is useful if you have a pre-commit hook. ",
587-
),
588-
).ask()
589-
config.update(answers)
590543

591-
for key, val in config.items():
592-
destination_config["tool"]["bumpversion"][key] = val
544+
destination_config = create_configuration(destination, prompt)
593545

594-
if destination_path:
595-
destination_path.write_text(dumps(destination_config))
596-
else:
546+
if destination == "stdout":
597547
print_info(dumps(destination_config))
548+
else:
549+
Path(destination).write_text(dumps(destination_config))

bumpversion/config/create.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Module for creating a new config file."""
2+
from pathlib import Path
3+
4+
import questionary
5+
from tomlkit import TOMLDocument
6+
7+
8+
def create_configuration(destination: str, prompt: bool) -> TOMLDocument:
9+
"""
10+
Create a new configuration as a TOMLDocument.
11+
12+
Args:
13+
destination: `stdout` or a path to a new or existing file.
14+
prompt: `True` if the user should be prompted for input.
15+
16+
Returns:
17+
The TOMLDocument structure with the updated configuration.
18+
"""
19+
config, destination_config = get_defaults_from_dest(destination)
20+
21+
if prompt:
22+
allow_dirty_default = "(Y/n)" if config["allow_dirty"] else "(y/N)"
23+
answers = questionary.form(
24+
current_version=questionary.text("What is the current version?", default=config["current_version"]),
25+
commit=questionary.confirm(
26+
"Commit changes made when bumping to version control?", default=config["commit"]
27+
),
28+
allow_dirty=questionary.confirm(
29+
"Allow dirty working directory when bumping?",
30+
default=config["allow_dirty"],
31+
instruction=(
32+
"If you are also creating or modifying other files (e.g. a CHANGELOG), say Yes. "
33+
f"{allow_dirty_default} "
34+
),
35+
),
36+
tag=questionary.confirm("Tag changes made when bumping in version control?", default=config["tag"]),
37+
commit_args=questionary.text(
38+
"Any extra arguments to pass to the commit command?",
39+
default=config["commit_args"] or "",
40+
instruction="For example, `--no-verify` is useful if you have a pre-commit hook. ",
41+
),
42+
).ask()
43+
config.update(answers)
44+
45+
for key, val in config.items():
46+
destination_config["tool"]["bumpversion"][key] = val if val is not None else ""
47+
48+
return destination_config
49+
50+
51+
def get_defaults_from_dest(destination: str) -> tuple[dict, TOMLDocument]:
52+
"""Get the default configuration and the configuration from the destination."""
53+
from tomlkit import document, parse
54+
55+
from bumpversion.config import DEFAULTS
56+
57+
config = DEFAULTS.copy()
58+
if Path(destination).exists():
59+
destination_config = parse(Path(destination).read_text())
60+
else:
61+
destination_config = document()
62+
63+
destination_config.setdefault("tool", {})
64+
destination_config["tool"].setdefault("bumpversion", {})
65+
existing_config = destination_config["tool"]["bumpversion"]
66+
if existing_config:
67+
config.update(existing_config)
68+
69+
project_config = destination_config.get("project", {}).get("version")
70+
config["current_version"] = config["current_version"] or project_config or "0.1.0"
71+
del config["scm_info"]
72+
del config["parts"]
73+
del config["files"]
74+
75+
return config, destination_config

0 commit comments

Comments
 (0)