Last active
September 22, 2024 06:32
-
-
Save juftin/ba15d4703384748b608af767f25a213f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
semantic-release helper | |
""" | |
# /// script | |
# dependencies = [ | |
# "click~=8.1.7", | |
# "packaging~=24.1", | |
# ] | |
# /// | |
from __future__ import annotations | |
import logging | |
import sys | |
import click | |
from packaging.version import Version | |
__version__ = "0.1.0" | |
logger = logging.getLogger("semantic-release") | |
class ClickVersionType(click.ParamType): | |
""" | |
Click Version Type | |
""" | |
name = "version" | |
def convert( | |
self, value: str, param: click.Parameter | None, ctx: click.Context | None | |
) -> Version: | |
""" | |
Convert Value to Semantic Version | |
""" | |
try: | |
return Version(value) | |
except ValueError: | |
self.fail(message=f"{value!r} is not a valid version", param=param, ctx=ctx) | |
CLICK_VERSION = ClickVersionType() | |
@click.group("semantic-release") | |
@click.option( | |
"--log-level", | |
default="INFO", | |
help="Log level", | |
envvar="LOG_LEVEL", | |
type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]), | |
) | |
@click.version_option(version=__version__, prog_name="semantic-release") | |
def cli(log_level: str) -> None: | |
""" | |
Command line interface | |
""" | |
logging.basicConfig( | |
format="%(asctime)s [%(levelname)8s]: %(message)s", | |
level=logging.getLevelName(log_level.upper()), | |
) | |
@cli.command | |
@click.argument( | |
"old_version", | |
type=CLICK_VERSION, | |
required=True, | |
) | |
@click.argument("new_version", type=CLICK_VERSION, required=True) | |
def compare(old_version: Version, new_version: Version) -> None: | |
""" | |
Compare Whether A Version Change is Compatible | |
This command compares two semantic versions and determines whether | |
the change is compatible. When the new version is greater than the old | |
version, the change is compatible, otherwise it is incompatible. | |
""" | |
logger.info("Comparing Semantic Versions") | |
logger.info("Old Version: %s", old_version) | |
logger.info("New Version: %s", new_version) | |
if old_version > new_version: | |
logger.error("Incompatible Version Change") | |
logger.error("%s is greater than %s", old_version, new_version) | |
sys.exit(1) | |
elif old_version == new_version: | |
logger.error("Incompatible Version Change") | |
logger.error("%s is equal to %s", old_version, new_version) | |
sys.exit(1) | |
elif old_version < new_version: | |
logger.info("Compatible Version Change") | |
logger.info("%s is greater than %s", new_version, old_version) | |
return None | |
else: | |
logger.error("Incompatible Version Change") | |
logger.error("Invalid Version Comparison") | |
sys.exit(1) | |
if __name__ == "__main__": | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment