# Released under the MIT License. See LICENSE for details.#"""Util to get ballisticakit versions."""from__future__importannotationsimportosfromenumimportEnumfromtypingimportTYPE_CHECKING,assert_neverfromefro.errorimportCleanErrorifTYPE_CHECKING:pass
[docs]classMode(Enum):"""Mode we can run this command in."""INFO='info'BUILD='build'VERSION='version'API='api'
def_handle_args(args:list[str])->Mode:"""parse os args and return a mode"""mode:Mode|None=Noneiflen(args)==0:print('OPTIONS: info, build, version','api')raiseCleanError()try:mode=Mode(args[0])exceptValueErrorasexc:raiseCleanError(f"Invalid mode '{args[0]}'")fromexciflen(args)!=1:raiseCleanError('Incorrect args.')returnmode
[docs]defget_current_version()->tuple[str,int]:"""Pull current version and build_number from the project."""version=Nonebuild_number=Nonewithopen('src/ballistica/shared/ballistica.cc',encoding='utf-8')asinfile:lines=infile.readlines()forlineinlines:prefix='const char* kEngineVersion = "'suffix='";\n'ifline.startswith(prefix)andline.endswith(suffix):ifversionisnotNone:raiseRuntimeError('Found multiple version lines.')version=line.removeprefix(prefix).removesuffix(suffix)prefix='const int kEngineBuildNumber = 'suffix=';\n'ifline.startswith(prefix)andline.endswith(suffix):ifbuild_numberisnotNone:raiseRuntimeError('Found multiple build number lines.')build_number=int(line.removeprefix(prefix).removesuffix(suffix))ifversionisNone:raiseRuntimeError('Version not found.')ifbuild_numberisNone:raiseRuntimeError('Build number not found.')returnversion,build_number
[docs]defget_current_api_version()->int:"""Pull current api version from the project."""withopen('src/ballistica/shared/ballistica.cc',encoding='utf-8')asinfile:lines=infile.readlines()linestart='const int kEngineApiVersion = 'lineend=';'forlineinlines:ifline.startswith(linestart):returnint(line.strip().removeprefix(linestart).removesuffix(lineend).strip())raiseRuntimeError('Api version line not found.')
[docs]defrun(projroot:str,args:list[str])->None:"""Main entry point for this script."""mode=_handle_args(args)# We want to run from the root dir.os.chdir(projroot)version,build_number=get_current_version()ifmodeisMode.INFO:print('version = '+version)print('build = '+str(build_number))print('api = '+str(get_current_api_version()))elifmodeisMode.VERSION:print(version)elifmodeisMode.BUILD:print(build_number)elifmodeisMode.API:print(get_current_api_version())else:assert_never(mode)