|
2 | 2 | from pathlib import Path
|
3 | 3 | from typing import List, Optional
|
4 | 4 |
|
| 5 | +import questionary |
5 | 6 | import rich_click as click
|
6 | 7 | from click.core import Context
|
| 8 | +from tomlkit import dumps |
7 | 9 |
|
8 | 10 | from bumpversion import __version__
|
9 | 11 | from bumpversion.aliases import AliasedGroup
|
10 | 12 | from bumpversion.bump import do_bump
|
11 | 13 | from bumpversion.config import get_configuration
|
| 14 | +from bumpversion.config.create import create_configuration |
12 | 15 | from bumpversion.config.files import find_config_file
|
13 | 16 | from bumpversion.files import ConfiguredFile, modify_files
|
14 | 17 | from bumpversion.show import do_show, log_list
|
@@ -533,65 +536,14 @@ def replace(
|
533 | 536 | )
|
534 | 537 | def sample_config(prompt: bool, destination: str) -> None:
|
535 | 538 | """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() |
542 | 539 | if prompt:
|
543 | 540 | destination = questionary.select(
|
544 | 541 | "Destination", choices=["stdout", ".bumpversion.toml", "pyproject.toml"], default=destination
|
545 | 542 | ).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) |
590 | 543 |
|
591 |
| - for key, val in config.items(): |
592 |
| - destination_config["tool"]["bumpversion"][key] = val |
| 544 | + destination_config = create_configuration(destination, prompt) |
593 | 545 |
|
594 |
| - if destination_path: |
595 |
| - destination_path.write_text(dumps(destination_config)) |
596 |
| - else: |
| 546 | + if destination == "stdout": |
597 | 547 | print_info(dumps(destination_config))
|
| 548 | + else: |
| 549 | + Path(destination).write_text(dumps(destination_config)) |
0 commit comments