vxSDK/vxsdk/__main__.py

103 lines
2.8 KiB
Python

"""
vxSDK is a suitable of tools used in conjunction with the Vhex Operating System
to develop game, add-in, abstract build step with dependencies resolver and
more.
"""
import sys
import os
from core.logger import log
# Program version (inserted at compile-time)
__VXSDK_VERSION__ = "%VERSION%"
__VXSDK_BUILD_HASH__ = "%BUILD_HASH%"
__VXSDK_BUILD_DATE__ = "%BUILD_DATE%"
__VXSDK_HELP__ = r"""
Vhex' Software Developement Kit
USAGE:
vxsdk [+toolchain] [OPTIONS] [SUBCOMMAND]
OPTIONS:
-v, --version Print version info and exit
--list List installed command
--update Try to update the ymtools
-h, --help Print helps information
Default sub-commands used:
project Project abstraction
pkg Package manager
build Build abstraction
See `vxsdk <sub-command> --help` for more information on a specific command
""".strip()
def _list_modules():
for name in os.listdir(f"{os.path.dirname(__file__)}/cli"):
try:
mod = __import__(f"cli.{name.split('.')[0]}", fromlist=[
'__VXSDK_MODULE_META__',
'cli_parse'
])
if not hasattr(mod, 'cli_validate'):
continue
if not hasattr(mod, 'cli_parse'):
continue
if not hasattr(mod, '__VXSDK_MODULE_META__'):
continue
yield mod
except ImportError as err:
log.warn(f"[vxsdk] module '{name}' cannot be imported")
log.warn(f"{err}")
def _subcommand_parse(argv):
for mod in _list_modules():
if mod.cli_validate(argv[0]):
return mod.cli_parse(argv)
log.emergency(f"vxsdk: '{argv[0]}' command not found :(")
return 84
def _subcommand_list():
for mod in _list_modules():
mod_info = mod.__VXSDK_MODULE_META__[1]
args = str(mod.__VXSDK_MODULE_META__[0]).strip('[]')
log.user(f" {args}".ljust(32) + f"{mod_info}")
def _main(argv):
if not argv:
log.error(__VXSDK_HELP__)
sys.exit(84)
if argv[0] in ['-h', '--help']:
log.user(__VXSDK_HELP__)
sys.exit(0)
if argv[0] == '--version':
_ver = __VXSDK_VERSION__
_hash = __VXSDK_BUILD_HASH__
_date = __VXSDK_BUILD_DATE__
log.user(f"vxSDK {_ver} ({_hash} {_date})")
sys.exit(0)
if argv[0] == '--update':
return os.system(
os.path.dirname(__file__) + '/../install.sh update'
)
if argv[0] in ['-v', '-vv', '-vvv']:
log.level += len(argv[0]) - 1
argv = argv[1:]
if not argv:
log.error(__VXSDK_HELP__)
sys.exit(84)
if len(argv) == 1 and argv[0] == '--list':
return _subcommand_list()
return _subcommand_parse(argv)
_main(sys.argv[1:])