|
1 | 1 | """bump-my-version Command line interface."""
|
| 2 | +from pathlib import Path |
2 | 3 | from typing import List, Optional
|
3 | 4 |
|
4 | 5 | import rich_click as click
|
|
11 | 12 | from bumpversion.config.files import find_config_file
|
12 | 13 | from bumpversion.files import ConfiguredFile, modify_files
|
13 | 14 | from bumpversion.show import do_show, log_list
|
14 |
| -from bumpversion.ui import get_indented_logger, print_warning, setup_logging |
| 15 | +from bumpversion.ui import get_indented_logger, print_info, print_warning, setup_logging |
15 | 16 | from bumpversion.utils import get_context, get_overrides
|
16 | 17 |
|
17 | 18 | logger = get_indented_logger(__name__)
|
@@ -516,3 +517,81 @@ def replace(
|
516 | 517 | ctx = get_context(config, version, next_version)
|
517 | 518 |
|
518 | 519 | modify_files(configured_files, version, next_version, ctx, dry_run)
|
| 520 | + |
| 521 | + |
| 522 | +@cli.command() |
| 523 | +@click.option( |
| 524 | + "--prompt/--no-prompt", |
| 525 | + default=True, |
| 526 | + help="Ask the user questions about the configuration.", |
| 527 | +) |
| 528 | +@click.option( |
| 529 | + "--destination", |
| 530 | + default="stdout", |
| 531 | + help="Where to write the sample configuration.", |
| 532 | + type=click.Choice(["stdout", ".bumpversion.toml", "pyproject.toml"]), |
| 533 | +) |
| 534 | +def sample_config(prompt: bool, destination: str) -> None: |
| 535 | + """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 | + if prompt: |
| 543 | + destination = questionary.select( |
| 544 | + "Destination", choices=["stdout", ".bumpversion.toml", "pyproject.toml"], default=destination |
| 545 | + ).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 | + |
| 591 | + for key, val in config.items(): |
| 592 | + destination_config["tool"]["bumpversion"][key] = val |
| 593 | + |
| 594 | + if destination_path: |
| 595 | + destination_path.write_text(dumps(destination_config)) |
| 596 | + else: |
| 597 | + print_info(dumps(destination_config)) |
0 commit comments