From b1c461387f8d8926481995ddb85e84d772005c5a Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Thu, 19 Jun 2025 16:48:28 +0200 Subject: [PATCH] Updated new_source tool to respect a modules set C++ namespace. Fixed generation of modules with templates without config.json. --- external/scons-plus-plus | 2 +- tools/common/__init__.py | 10 ++++++++ tools/new_module.py | 5 +++- tools/new_source.py | 25 ++++++++++++++++--- .../module/app/private/SModule.jinja | 6 +++++ 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/external/scons-plus-plus b/external/scons-plus-plus index c3b5244..e583c5e 160000 --- a/external/scons-plus-plus +++ b/external/scons-plus-plus @@ -1 +1 @@ -Subproject commit c3b5244eac5187a64b6f46a1a4dc171416fff313 +Subproject commit e583c5ef6c1a8dbce941014aa3ce995f2d53d05b diff --git a/tools/common/__init__.py b/tools/common/__init__.py index 2e5fc76..8244150 100644 --- a/tools/common/__init__.py +++ b/tools/common/__init__.py @@ -43,6 +43,16 @@ def get_project_config() -> dict: raw = exec_get_output(('scons', '-s', '--disable_auto_update', '--dump=config', '--dump_format=json')) return json.loads(raw) +def get_module_config() -> dict: + raw = exec_get_output(('scons', '-s', '--disable_auto_update', '--dump=modules', '--dump_format=json')) + return json.loads(raw) + +def find_module_folder(subpath: Path) -> Optional[Path]: + for parent in subpath.parents: + if (parent / 'SModule').exists(): + return parent + return None + def prompt(prefix: str = '> ') -> str: sys.stdout.write(prefix) sys.stdout.flush() diff --git a/tools/new_module.py b/tools/new_module.py index c7976fb..f2097e5 100644 --- a/tools/new_module.py +++ b/tools/new_module.py @@ -112,7 +112,10 @@ def load_template_config() -> None: global _template_config config_file = _template_folder / 'config.json' if not config_file.exists(): - _template_config = {} + _template_config = _TemplateConfig( + options=[], + new_sources=[] + ) return with config_file.open('r') as f: try: diff --git a/tools/new_source.py b/tools/new_source.py index 67dcf13..6c64747 100644 --- a/tools/new_source.py +++ b/tools/new_source.py @@ -8,7 +8,7 @@ from typing import Optional, Literal sys.path.append(os.path.dirname(__file__)) -from common import prompt_choices, prompt_path, prompt_yesno, run_script +from common import find_module_folder, get_module_config, prompt_choices, prompt_path, prompt_yesno, run_script from common.jinja import is_jinja_installed, generate_file @@ -44,12 +44,31 @@ def _make_source_path(base_path: Path) -> Path: def _apply_params(header_folder: Path, do_create_source: bool, base_path: Path) -> None: global _header_path, _source_path, _namespace - _namespace = base_path.parts[0] + source_path = _make_source_path(base_path) + module_folder = find_module_folder(source_path) + + namespace = None + if module_folder is not None: + module_subfolder = module_folder.relative_to(_PRIVATE_PATH) + module_key = str(module_subfolder) + if os.path.sep != '/': + module_key = module_key.replace(os.pathsep, '/') + + all_module_conf = get_module_config() + module_conf = all_module_conf.get(module_key) + if module_conf is not None: + namespace = module_conf.get('cxx_namespace') + if namespace is None: + namespace = module_key.replace('/', '_') + + if namespace is None: + namespace = base_path.parts[0] + _namespace = namespace if header_folder is not None: _header_path = _make_header_path(header_folder, base_path) if do_create_source: - _source_path = _make_source_path(base_path) + _source_path = source_path def query_params() -> None: header_folder = prompt_choices('Create Header?', _HEADER_CHOICES) diff --git a/tools/templates/module/app/private/SModule.jinja b/tools/templates/module/app/private/SModule.jinja index 89539ab..d458177 100644 --- a/tools/templates/module/app/private/SModule.jinja +++ b/tools/templates/module/app/private/SModule.jinja @@ -1,6 +1,12 @@ Import('env') +env.ModuleConfig( + name = '{{name}}', + description = '', + cxx_namespace = '{{folder_name}}' +) + src_files = Split(""" main.cpp """)