Skip to content

Commit

Permalink
Merge pull request #885 from cookiejar/feature/suggest_version
Browse files Browse the repository at this point in the history
Feature: Suggest version for bump-version
  • Loading branch information
Imipenem committed Feb 21, 2022
2 parents 66a6663 + 9c56cca commit ff3dedb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 35 deletions.
54 changes: 26 additions & 28 deletions cookietemple/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,37 +296,35 @@ def bump_version(ctx, new_version, project_dir, downgrade) -> None:
Unless the user uses downgrade mode via the -d flag, a downgrade of a version is never allowed. Note that bump-version with the new version
equals the current version is never allowed, either with or without -d.
"""
version_bumper = VersionBumper(project_dir, downgrade)
# suggest valid version if none was given
if not new_version:
HelpErrorHandling.args_not_provided(ctx, "bump-version")
else:
# if the path entered ends with a trailing slash remove it for consistent output
if str(project_dir).endswith("/"):
project_dir = Path(str(project_dir).replace(str(project_dir)[len(str(project_dir)) - 1 :], ""))

version_bumper = VersionBumper(project_dir, downgrade)
# lint before run bump-version
version_bumper.lint_before_bump()
# only run bump-version if conditions are met
if version_bumper.can_run_bump_version(new_version):
# only run "sanity" checker when the downgrade flag is not set
if not downgrade:
# if the check fails, ask the user for confirmation
if version_bumper.check_bump_range(
version_bumper.CURRENT_VERSION.split("-")[0], new_version.split("-")[0]
):
version_bumper.bump_template_version(new_version, project_dir)
elif cookietemple_questionary_or_dot_cookietemple(
function="confirm",
question=f"Bumping from {version_bumper.CURRENT_VERSION} to {new_version} seems not reasonable.\n"
f"Do you really want to bump the project version?",
default="n",
):
console.print("\n")
version_bumper.bump_template_version(new_version, project_dir)
else:
new_version = version_bumper.choose_valid_version()
# if the path entered ends with a trailing slash remove it for consistent output
if str(project_dir).endswith("/"):
project_dir = Path(str(project_dir).replace(str(project_dir)[len(str(project_dir)) - 1 :], ""))

# lint before run bump-version
version_bumper.lint_before_bump()
# only run bump-version if conditions are met
if version_bumper.can_run_bump_version(new_version):
# only run "sanity" checker when the downgrade flag is not set
if not downgrade:
# if the check fails, ask the user for confirmation
if version_bumper.check_bump_range(version_bumper.CURRENT_VERSION.split("-")[0], new_version.split("-")[0]):
version_bumper.bump_template_version(new_version, project_dir)
elif cookietemple_questionary_or_dot_cookietemple(
function="confirm",
question=f"Bumping from {version_bumper.CURRENT_VERSION} to {new_version} seems not reasonable.\n"
f"Do you really want to bump the project version?",
default="n",
):
console.print("\n")
version_bumper.bump_template_version(new_version, project_dir)
else:
sys.exit(1)
version_bumper.bump_template_version(new_version, project_dir)
else:
sys.exit(1)


@cookietemple_cli.command(short_help="Create a self contained executable.", cls=CustomHelpSubcommand)
Expand Down
19 changes: 19 additions & 0 deletions cookietemple/bump_version/bump_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,22 @@ def lint_before_bump(self) -> None:
function="confirm", question="Do you really want to continue?", default="n"
):
sys.exit(1)

def choose_valid_version(self) -> str:
version_split = self.CURRENT_VERSION.split(".")
new_patch, new_minor, new_major = (
str(int(version_split[2]) + 1),
str(int(version_split[1]) + 1),
str(int(version_split[0]) + 1),
)
version_split_patch = [version_split[0], version_split[1], new_patch]
version_split_minor = [version_split[0], new_minor, "0"]
version_split_major = [new_major, "0", "0"]

new_version = cookietemple_questionary_or_dot_cookietemple(
function="select",
question="Choose the new project version:",
choices=[".".join(version_split_patch), ".".join(version_split_minor), ".".join(version_split_major)],
default="cli",
)
return new_version # type:ignore
7 changes: 0 additions & 7 deletions cookietemple/custom_cli/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,6 @@ def args_not_provided(ctx, cmd: str) -> None:
)
sys.exit(1)

elif cmd == "bump-version":
print(
f"[bold red]Failed to execute [bold green]{cmd}.\n[bold blue]Please provide a new version like [bold green]1.2.3 "
"[bold blue]as first argument."
)
sys.exit(1)

elif cmd == "config":
print(
f"[bold red]Failed to execute [bold green]{cmd}.\n[bold blue]Please provide a valid argument. You can choose general, pat or all."
Expand Down
3 changes: 3 additions & 0 deletions docs/bump_version.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ The :code:`bump-version` command follows the syntax

bump-version applied to a fresh cli-python project

Note that you can use ``bump-version`` without passing any parameters. This way, cookietemple will let you choose from three valid options
to bump your projects version. Note that this will only work in the main directory of your project due to some cli constraints.

Flags
-------

Expand Down

0 comments on commit ff3dedb

Please sign in to comment.