Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suffix flag #14

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,142 @@ htmlcov/
dist/
build/
.eggs/

# Created by https://www.gitignore.io/api/osx,python
# Edit at https://www.gitignore.io/?templates=osx,python

### OSX ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# End of https://www.gitignore.io/api/osx,python
tags
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ addopts = --cov
python_files = test*.py
site_dirs = tests/
norecursedirs = .git .eggs build dist docs *.egg-info
filterwarnings =
ignore::UserWarning
10 changes: 10 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@ def test_show_config(repo):
runner = CliRunner()
result = runner.invoke(cli, [f"--repo={repo}", "--prefix=v", "show-config"])
assert result.exit_code == 0


def test_suffix(repo):
runner = CliRunner()
result = runner.invoke(
cli, [f"--repo={repo}", "--suffix=foo", "next", "patch"]
)
assert result.exit_code == 0
assert result.output == "0.0.1-foo\n"

13 changes: 13 additions & 0 deletions tests/test_vinnie_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,16 @@ def test_strip_prefix(repo):
b = BaseBackend(config=VinnieConfig(prefix="v"))
assert b.strip_prefix("v1.0.0") == "1.0.0"


def test_add_suffix(repo):
b = BaseBackend(config=VinnieConfig(suffix="foo"))
assert b.add_suffix("3.0.0") == "3.0.0foo"

b = BaseBackend(config=VinnieConfig())
assert b.add_suffix("3.0.0") == "3.0.0"


def test_strip_suffix(repo):
b = BaseBackend(config=VinnieConfig(suffix="-beta"))
assert b.strip_suffix("1.0.0-beta") == "1.0.0"

22 changes: 19 additions & 3 deletions vinnie/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,37 @@ def strip_prefix(self, value):
value = re.sub(f"^{self.config.prefix}", "", value)
return value

def strip_suffix(self, value):
if self.config.suffix:
value = re.sub(f"{self.config.suffix}$", "", value)
return value

def add_prefix(self, value):
if self.config.prefix:
value = f"{self.config.prefix}{value}"
return value

def add_suffix(self, value):
if self.config.suffix:
value = f"{value}{self.config.suffix}"
return value

def validate_version(self, value):
if value is None:
return False

# If we have a prefix and don't start with it, not a version
if self.config.prefix and not value.startswith(self.config.prefix):
# If we have (pre|suf)fix and don't (start/end) with it, not a version
if (self.config.prefix and not value.startswith(self.config.prefix)) or \
(self.config.suffix and not value.endswith(self.config.suffix)):
return False


# Strip off the prefix to apply regex
value = re.sub(f"^{self.config.prefix}", "", value)
value = re.sub(
fr'^(?:{self.config.prefix})?(.*)(?:{self.config.suffix})?$',
r'\1',
value
)

m = re.fullmatch(
r"""^
Expand Down
47 changes: 39 additions & 8 deletions vinnie/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,49 @@ def setup_backend(self):
def dump(self):
self.config.dump()


def version_only(self, value):
return self.strip_bread(value)

def strip_prefix(self, value):
if self.config.prefix:
value = re.sub(f"^{self.config.prefix}", "", value)
return value

def strip_suffix(self, value):
if self.config.suffix:
value = re.sub(f"-{self.config.suffix}$", "", value)
return value

def omit_prefix(self, value):
if self.config.omit_prefix:
value = self.strip_prefix(value)
return value

def omit_bread(self, value):
return self.omit_prefix(self.omit_suffix(value))

def omit_suffix(self, value):
if self.config.omit_suffix:
value = self.strip_suffix(value)
return value

def add_prefix(self, value):
if self.config.prefix:
value = f"{self.config.prefix}{value}"
return value

def add_suffix(self, value):
if self.config.suffix:
value = f"{value}-{self.config.suffix}"
return value

def add_bread(self, value):
return self.add_prefix(self.add_suffix(value))

def strip_bread(self, value):
return self.strip_prefix(self.strip_suffix(value))

def version(self):
""" Return the current version """
if self.config.current_version is not None:
Expand All @@ -68,21 +96,24 @@ def version(self):
return self.backend.get_current_version()

def get_next_bump(self):
current = self.strip_prefix(self.version())
current = self.strip_bread(self.version())
next_integer = str(int(current) + 1)
return self.add_prefix(next_integer)
return self.add_bread(next_integer)

def get_next_patch(self):
current = self.strip_prefix(self.version())
return self.add_prefix(semver.bump_patch(current))
current = self.version_only(self.version())
new = semver.bump_patch(current)
return self.add_bread(new)

def get_next_minor(self):
current = self.strip_prefix(self.version())
return self.add_prefix(semver.bump_minor(current))
current = self.strip_bread(self.version())
new = semver.bump_minor(current)
return self.add_bread(new)

def get_next_major(self):
current = self.strip_prefix(self.version())
return self.add_prefix(semver.bump_major(current))
current = self.strip_bread(self.version())
new = semver.bump_major(current)
return self.add_bread(new)

def push(self, remote):
if self.config.push:
Expand Down
17 changes: 10 additions & 7 deletions vinnie/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
@click.option("--github-token", envvar="VINNIE_GITHUB_TOKEN", default=None)
@click.option("--gitlab-token", envvar="VINNIE_GITLAB_TOKEN", default=None)
@click.option("--prefix", envvar="VINNIE_TAG_PREFIX", default="")
@click.option("--suffix", envvar="VINNIE_TAG_SUFFIX", default="")
@click.option("--omit-prefix", envvar="VINNIE_OMIT_PREFIX", default=False, is_flag=True)
@click.option("--omit-suffix", envvar="VINNIE_OMIT_SUFFIX", default=False, is_flag=True)
@click.option("--semver", envvar="VINNIE_SEMVER", default=True)
@click.option("--s3-access-key", envvar="VINNIE_S3_ACCESS_KEY", default=None)
@click.option("--s3-secret-key", envvar="VINNIE_S3_SECRET_KEY", default=None)
Expand All @@ -39,7 +41,7 @@ def bump(ctx, v):
""" Bump incrementing integer version """
try:
new_value = v.next_bump()
new_value = v.omit_prefix(new_value)
new_value = v.omit_bread(new_value)
click.echo(new_value)
except ValueError:
click.echo("version was not an integer; could not bump.")
Expand All @@ -51,9 +53,11 @@ def bump(ctx, v):
@click.pass_context
def patch(ctx, v):
""" Patch version number, tag and push"""
click.echo(str(v.__dict__))

try:
new_value = v.next_patch()
new_value = v.omit_prefix(new_value)
new_value = v.omit_bread(new_value)
click.echo(new_value)
except GitCommandError as e:
click.echo(str(e))
Expand All @@ -67,7 +71,7 @@ def minor(ctx, v):
""" Increase minor version, tag and push """
try:
new_value = v.next_minor()
new_value = v.omit_prefix(new_value)
new_value = v.omit_bread(new_value)
click.echo(new_value)
except GitCommandError as e:
click.echo(str(e))
Expand All @@ -81,7 +85,7 @@ def major(ctx, v):
""" Increase major version, tag and push """
try:
new_value = v.next_major()
new_value = v.omit_prefix(new_value)
new_value = v.omit_bread(new_value)
click.echo(new_value)
except GitCommandError as e:
click.echo(str(e))
Expand All @@ -105,7 +109,7 @@ def next(ctx, v, part):
raise RuntimeError(
f"Unknown next value '{part}'. Must be patch, minor, or major"
)
next_value = v.omit_prefix(next_value)
next_value = v.omit_bread(next_value)
click.echo(next_value)
except RuntimeError as e:
click.echo(str(e))
Expand All @@ -118,7 +122,6 @@ def next(ctx, v, part):
def version(v):
""" Print current version to stdout """
version = v.version()
version = v.omit_prefix(version)
click.echo(version)


Expand All @@ -141,7 +144,7 @@ def validate(v):
def replace(v, filename):
""" Replace placeholder with current version """
version = v.version()
version = v.omit_prefix(version)
version = v.omit_bread(version)

temp_filename = f"{filename}.tmp"
f = open(temp_filename, "w+")
Expand Down
Loading