diff --git a/SConscript b/SConscript index c140ea2..9bfb7d4 100644 --- a/SConscript +++ b/SConscript @@ -21,7 +21,8 @@ def _parse_lib_conf(env: Environment, lib_conf: dict) -> None: env.Append(CPPPATH = lib_conf.get('CPPPATH', []), CPPDEFINES = lib_conf.get('CPPDEFINES', []), LIBPATH = lib_conf.get('LIBPATH', []), - LIBS = lib_conf.get('LIBS', [])) + LIBS = lib_conf.get('LIBS', []), + JINJA_TEMPLATE_SEARCHPATH = lib_conf.get('JINJA_TEMPLATE_SEARCHPATH', [])) def _inject_list(kwargs: dict, dependency: dict, list_name: str) -> None: if list_name not in dependency: @@ -359,6 +360,8 @@ env.AddMethod(_wrap_builder(env.UnitySharedLibrary, is_lib = True), 'UnityShared if hasattr(env, 'Gch'): env.AddMethod(_wrap_builder(env.Gch), 'Gch') +if hasattr(env, 'Jinja'): + env = SConscript('addons/jinja.py', exports = 'env') if dump_env: print('==== Begin Environment Dump =====') diff --git a/addons/jinja.py b/addons/jinja.py new file mode 100644 index 0000000..a38d824 --- /dev/null +++ b/addons/jinja.py @@ -0,0 +1,35 @@ + +import pathlib + +Import('env') + +def _jinja_load_config(env, config_name): + searched_paths = [] + for scons_path in env['JINJA_CONFIG_SEARCHPATH']: + if hasattr(scons_path, 'abspath'): + scons_path = scons_path.abspath + path = pathlib.Path(scons_path) / f'{config_name}.yml' + if path.exists(): + with path.open('r') as file: + import yaml + return yaml.safe_load(file) + searched_paths.append(f'\n{path}') + joined_paths = ''.join(searched_paths) + raise Exception(f'Could not find Jinja config file "{config_name}.yml". Searched: {joined_paths}') +def _wrap_jinja(orig_jinja): + def _wrapped(env, target, **kwargs): + if 'source' not in kwargs: + kwargs['source'] = f'{target}.jinja' + target = orig_jinja(**kwargs) + if 'depends' in kwargs: + for dependency in kwargs['depends']: + env.Depends(target, dependency) + # env.Depends(alias_prepare, target) + return target + return _wrapped + +env.AddMethod(_wrap_jinja(env.Jinja), 'Jinja') +env.Append(JINJA_FILTERS = {'load_config': _jinja_load_config}) +env.Append(JINJA_TEMPLATE_SEARCHPATH = ['data/jinja']) +env['JINJA_CONFIG_SEARCHPATH'] = [env.Dir('#data/config')] +Return('env')