Skip to content

Commit

Permalink
Use rich UI
Browse files Browse the repository at this point in the history
Use rich ui to present information better to the user
Add file folder display to other places where applicable
Add flake8 settings
Remove colorama now we're using rich
  • Loading branch information
Svenito committed May 6, 2024
1 parent 4fd6fa7 commit 9a4ed50
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 56 deletions.
73 changes: 72 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ python = "^3.8"
gitpython = "^3.1"
click = "^8.0"
semver = "^2.13, <3"
colorama = "^0.4.6"
setuptools = "^65.5.3"
rich = "^13.7.1"
flake8-pyproject = "^1.2.3"


[tool.poetry.dev-dependencies]
Expand All @@ -34,6 +35,15 @@ versup = "versup.__main__:main"
[tool.poetry.group.dev.dependencies]
isort = "^5.12.0"

[tool.flake8]
max-line-length = 88
per-file-ignores = [
'default_conf.py:E501',
]

[tool.isort]
profile = "black"

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
Expand Down
40 changes: 16 additions & 24 deletions tests/test_file_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,29 @@ def test_update_files(tmpdir):


def test_return_empty_list_for_no_files():
assert file_updater.update_files("3.4.5", {}, False) == []
assert file_updater.update_files("3.4.5", {}, False) == ([], {})


def test_return_empty_list_if_file_not_found():
assert (
file_updater.update_files(
"3.4.5",
{
"doesnotexist": [
["Version ([\\d\\.]+) ", "Version [version] "],
["Version is ([\\d\\.]+)", "Version is [version]"],
]
},
False,
)
== []
)
assert file_updater.update_files(
"3.4.5",
{
"doesnotexist": [
["Version ([\\d\\.]+) ", "Version [version] "],
["Version is ([\\d\\.]+)", "Version is [version]"],
]
},
False,
) == ([], {})


def test_dry_run(tmpdir):
temp_file = tmpdir.join("testfile.txt")
filename = "testfile.txt"
temp_file = tmpdir.join(filename)
temp_file.write("this is a file to replace 1.2.3 with new version")
files = {str(temp_file.realpath()): ["1.2.3", "3.4.5"]}

capturedOutput = io.StringIO()
sys.stdout = capturedOutput

file_updater.update_files("3.4.5", files, True)

sys.stdout = sys.__stdout__
output = capturedOutput.getvalue().split("\n")
updated_files, updates = file_updater.update_files("3.4.5", files, True)

assert "with" in output[0]
assert "replace 3.4.5" in output[0]
assert filename in updates.keys()
assert "replace 3.4.5" in updates[filename]
50 changes: 38 additions & 12 deletions versup/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from typing import Any, Dict, List

import click
from rich import print
from rich.prompt import Confirm

import versup.changelog as changelog
import versup.file_updater as file_updater
Expand All @@ -16,7 +18,7 @@
parse_config_file,
)
from versup.custom_cmd_group import DefaultCommandGroup
from versup.printer import print_error, print_ok, print_warn
from versup.printer import make_file_tree, print_error, print_ok, print_warn
from versup.versioning import get_new_version

CONTEXT_SETTINGS = dict(
Expand Down Expand Up @@ -46,7 +48,7 @@ def cli(ctx, **kwargs):
"-c", "--current", is_flag=True, help="Show current configuration options"
)
def show_config(ctx, **kwargs):
import pprint
from rich.pretty import pprint

config = ctx.obj.conf
if kwargs["local"]:
Expand All @@ -55,7 +57,8 @@ def show_config(ctx, **kwargs):
config = parse_config_file("~/.config/versup.json")
if kwargs["current"]:
config = merge_configs_with_default()
pprint.pprint(config)

pprint(config)


@cli.command(
Expand All @@ -77,19 +80,31 @@ def do_versup(ctx, **kwargs):
"""
Increment or set project version
"""
print()
if kwargs["dryrun"]:
print(
"[bold green]:arrow_forward:[/bold green] Dry run set. No changes will be"
" made.\n"
)
current_branch: str = gitops.get_current_branch()
target_branch: str = get_conf_value(ctx.obj.conf, "commit/mainbranch")
if not gitops.check_current_branch_matches(target_branch):
print_warn(
f"Main branch set to '{target_branch}'. Currently on '{current_branch}'"
)
if not kwargs["dryrun"] and not click.confirm("Continue anyway?"):
if not kwargs["dryrun"] and not Confirm.ask("Continue anyway?"):
return

unstaged_files = "\n".join(gitops.get_unstaged_changes())
unstaged_files = gitops.get_unstaged_changes()
if unstaged_files:
print_warn(f"There are unstaged files\n{unstaged_files}")
if not kwargs["dryrun"] and not click.confirm("Continue with versup?"):
print_warn("There are unstaged files")

tree = make_file_tree(unstaged_files)

print(tree)
print()

if not kwargs["dryrun"] and not Confirm.ask("Continue with versup?"):
return

ctx.obj.version = kwargs["increment"]
Expand Down Expand Up @@ -149,8 +164,13 @@ def apply_bump(config, version, **kwargs):
# Update the files specified in config
if not kwargs["no_fileupdate"]:
files_to_update: Dict[str, Any] = get_conf_value(config, "files")
updated = file_updater.update_files(version, files_to_update, kwargs["dryrun"])
print_ok(f"Updated {', '.join(updated)}")
updated, updates = file_updater.update_files(
version, files_to_update, kwargs["dryrun"]
)
print_ok("Updated strings in the following files:")
tree = make_file_tree(updated, updates)
print(tree)
print()

# create changelog
if not kwargs["no_changelog"] and get_conf_value(config, "changelog/enabled"):
Expand Down Expand Up @@ -183,7 +203,7 @@ def do_changelog(config, version, **kwargs):
# If no changelog file and create is off, prompt
if not kwargs["dryrun"] and not os.path.isfile(changelog_file):
if not changelog_config["create"]:
if not click.confirm("No changelog file found. Create it?"):
if not Confirm.ask("No changelog file found. Create it?"):
return
# Ok to create/update it now
changelog.write(
Expand Down Expand Up @@ -235,11 +255,17 @@ def tag(config, version, **kwargs):
template.token_data["version"] = version
tag_name: str = template.render(tag_config["name"])
if kwargs["dryrun"]:
print(f"Create tag {version} with msg {tag_name}")
print(
f"Create tag [bold cyan]{version}[/bold cyan] with message [bold"
f" cyan]{tag_name}[/bold cyan]"
)
else:
gitops.create_new_tag(version, tag_name)

print_ok(f"Tag {tag_name} created")
print_ok(
f"Tag for [bold cyan]{version}[/bold cyan] with message [cyan"
f" bold]{tag_name}[/cyan bold] created"
)


def main():
Expand Down
8 changes: 6 additions & 2 deletions versup/default_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@
},
"tokens": {
"date": {
"format": "%Y-%m-%d" # Python datetime format to use when generating the `[date]` token
"format": ( # Python datetime format to use when generating the `[date]` token
"%Y-%m-%d"
)
},
"version_date": {
"format": "%Y-%m-%d" # Python datetime format to use when generating the `[version_date]` token
"format": ( # Python datetime format to use when generating the `[version_date]` token
"%Y-%m-%d"
)
},
},
"scripts": {
Expand Down
Loading

0 comments on commit 9a4ed50

Please sign in to comment.