diff --git a/scons/versioning.py b/scons/versioning.py index 1fecf51f6..059f310a0 100644 --- a/scons/versioning.py +++ b/scons/versioning.py @@ -5,7 +5,8 @@ """ import sys -from typing import Optional, Tuple +from dataclasses import dataclass +from typing import Optional VERSION_FILE = '.version' @@ -14,6 +15,31 @@ VERSION_FILE_ENCODING = 'utf-8' +@dataclass +class Version: + """ + Represents a semantic version. + + Attributes: + major: The major version number + minor: The minor version number + patch: The patch version number + dev: The development version number + """ + major: int + minor: int + patch: int + dev: Optional[int] = None + + def __str__(self) -> str: + version = str(self.major) + '.' + str(self.minor) + '.' + str(self.patch) + + if self.dev: + version += '.dev' + str(self.dev) + + return version + + def __read_version_file(version_file) -> str: with open(version_file, mode='r', encoding=VERSION_FILE_ENCODING) as file: lines = file.readlines() @@ -55,7 +81,7 @@ def __update_development_version(dev: int): __write_version_file(DEV_VERSION_FILE, updated_version) -def __parse_version(version: str) -> Tuple[int, int, int]: +def __parse_version(version: str) -> Version: parts = version.split('.') if len(parts) != 3: @@ -65,26 +91,17 @@ def __parse_version(version: str) -> Tuple[int, int, int]: major = __parse_version_number(parts[0]) minor = __parse_version_number(parts[1]) patch = __parse_version_number(parts[2]) - return major, minor, patch - - -def __format_version(major: int, minor: int, patch: int, dev: Optional[int] = None) -> str: - version = str(major) + '.' + str(minor) + '.' + str(patch) + return Version(major=major, minor=minor, patch=patch) - if dev is not None: - version += '.dev' + str(dev) - return version - - -def __get_current_version() -> Tuple[int, int, int]: +def __get_current_version() -> Version: current_version = __read_version_file(VERSION_FILE) print('Current version is "' + current_version + '"') return __parse_version(current_version) -def __update_version(major: int, minor: int, patch: int, dev: Optional[int] = None): - updated_version = __format_version(major, minor, patch, dev) +def __update_version(version: Version): + updated_version = str(version) print('Updated version to "' + updated_version + '"') __write_version_file(VERSION_FILE, updated_version) @@ -110,36 +127,36 @@ def apply_development_version(**_): """ Appends the development version to the current semantic version. """ - major, minor, patch = __get_current_version() - dev = __get_current_development_version() - __update_version(major, minor, patch, dev) + version = __get_current_version() + version.dev = __get_current_development_version() + __update_version(version) def increment_patch_version(**_): """ Increments the patch version. """ - major, minor, patch = __get_current_version() - patch += 1 - __update_version(major, minor, patch) + version = __get_current_version() + version.patch += 1 + __update_version(version) def increment_minor_version(**_): """ Increments the minor version. """ - major, minor, patch = __get_current_version() - minor += 1 - patch = 0 - __update_version(major, minor, patch) + version = __get_current_version() + version.minor += 1 + version.patch = 0 + __update_version(version) def increment_major_version(**_): """ Increments the major version. """ - major, minor, patch = __get_current_version() - major += 1 - minor = 0 - patch = 0 - __update_version(major, minor, patch) + version = __get_current_version() + version.major += 1 + version.minor = 0 + version.patch = 0 + __update_version(version)