Added command line tool and removed lib folder from gitignore.

This commit is contained in:
2025-09-20 14:01:31 +02:00
parent 79366c9098
commit b9335a6247
9 changed files with 230 additions and 47 deletions

View File

@@ -0,0 +1,24 @@
"""
Scons++ Command Line Interface
"""
import argparse
import logging
from .ccjson import make_ccjson_parser
_STDOUT_LOG_FORMAT = '%(message)s'
def run_spp_cmd() -> int:
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action='store_true')
subparsers = parser.add_subparsers(required=True)
make_ccjson_parser(subparsers)
args = parser.parse_args()
logging.basicConfig(format=_STDOUT_LOG_FORMAT, level=logging.DEBUG if args.verbose else logging.INFO)
args.handler(args)
return 0

View File

@@ -0,0 +1,18 @@
import argparse
from .common import exec_spp, get_config_cache, require_project_file
def _cmd(args: argparse.Namespace) -> None:
require_project_file()
build_type = args.build_type
if build_type == 'auto':
cache = get_config_cache()
build_type = cache.get('build_type', 'debug')
exec_spp((f'--build_type={build_type}', '--unity=disable', 'compile_commands.json'))
def make_ccjson_parser(subparsers) -> None:
parser : argparse.ArgumentParser = subparsers.add_parser('ccjson', help='Generate compile_commands.json')
parser.set_defaults(handler=_cmd)
parser.add_argument('--build_type', choices=('auto', 'debug', 'release_debug', 'release', 'profile'), default='auto')

View File

@@ -0,0 +1,51 @@
import json
import logging
from pathlib import Path
import shlex
import subprocess
import sys
from typing import Sequence
_project_root = Path('.').absolute()
def get_project_root() -> Path:
return _project_root
def set_project_root(path: Path) -> None:
global _project_root
_project_root = path
def get_config_cache() -> dict:
cache_file = get_project_root() / 'cache' / 'config_cache.json'
if not cache_file.exists():
return {}
try:
with cache_file.open('r') as f:
cache = json.load(f)
if not isinstance(cache, dict):
logging.warning('Config cache is not a dictionary, ignoring it.')
return {}
return cache
except Exception as e:
logging.error(f'Error while reading config cache: {e}.')
return {}
def require_project_file() -> None:
if not (get_project_root() / 'SConstruct').exists():
logging.error('This command has to be run inside an existing S++ project folder. Exiting.')
sys.exit(1)
def exec_checked(args: Sequence[str], **kwargs) -> None:
logging.debug('exec_checked: "%s"', shlex.join(args))
subprocess.run(args, stdout=sys.stdout, stderr=sys.stderr, check=True, **kwargs)
def exec_get_output(args: Sequence[str], **kwargs) -> str:
logging.debug('exec_get_output: "%s"', shlex.join(args))
return subprocess.run(args, text=True, check=True, capture_output=True, **kwargs).stdout
def exec_spp(args: Sequence[str], **kwargs):
full_cmd = ('scons', '-s', '--disable_auto_update', *args)
exec_checked(full_cmd, **kwargs)