From e414e769a1b377b744ca69b8c4d2a06e4f47175c Mon Sep 17 00:00:00 2001 From: Wrench56 Date: Mon, 1 Jul 2024 20:17:46 -0400 Subject: [PATCH] Enhance `feature_build.py` with NOT build sections and multifile support --- src/backend/utils/feature_build.py | 126 +++++++++++++++++++---------- 1 file changed, 83 insertions(+), 43 deletions(-) diff --git a/src/backend/utils/feature_build.py b/src/backend/utils/feature_build.py index 1e5e15b..a4f6626 100644 --- a/src/backend/utils/feature_build.py +++ b/src/backend/utils/feature_build.py @@ -1,69 +1,109 @@ -from typing import List +from typing import Dict, List, Optional, Tuple import logging from utils import config -_FILE_OF_FEATURE = { - 'STATUS_IN_SETTINGS': 'routes/setting/+page.svelte' +_FILE_OF_FEATURE: Dict[str, Tuple] = { + 'STATUS_IN_SETTINGS': ('src/routes/settings/+page.svelte', 'src/lib/components/settings/Box.svelte',), + '!STATUS_IN_SETTINGS': ('src/lib/components/settings/Box.svelte',) } -def disable_feature(feature: str) -> None: - filename = _decode_feature_file(feature) - if len(filename) == 0: +def disable_feature(feature: str, _recursive: bool = True) -> None: + unused_warn = True + if _recursive: + enable_feature(f'!{feature}', False) + filenames = _decode_feature_files(feature) + if not filenames: return - lines = _read_file(filename) - line_num = _find_feature(feature, lines) - if line_num < 0: + for filename in filenames: + lines = _read_file(filename) + for line_num in _find_html_features(feature, lines): + lines[line_num] = lines[line_num].replace('-->', '') + unused_warn = False + for line_num in _find_css_js_features(feature, lines): + lines[line_num] = lines[line_num].replace('*/', '') + unused_warn = False + if unused_warn: + logging.warning(f'Couldn\'t find feature "{feature}" in file "{filename}"') + _write_file(filename, lines) + + +def enable_feature(feature: str, _recursive: bool = True) -> None: + def _enable_feature_at(lines: list, line_num: int, feature: str, enable_str: str) -> None: + if lines[line_num].strip()[-1] != feature[-1]: + print(lines[line_num]) + print(feature[-1]) + print(lines[line_num]) + logging.error(f'Corrupted feature definition for feature "{feature}"') + return + lines[line_num] = lines[line_num].strip() + lines[line_num] += enable_str + + unused_warn = True + if _recursive: + disable_feature(f'!{feature}', False) + filenames = _decode_feature_files(feature) + if not filenames: return - lines[line_num].replace('-->', '') - _write_file(filename, lines) - - -def enable_feature(feature: str) -> None: - filename = _decode_feature_file(feature) - if len(filename) == 0: - return - lines = _read_file(filename) - line_num = _find_feature(feature, lines) - if line_num < 0: - return - if lines[-1] != feature[-1]: - logging.error(f'Corrupted feature definition for feature "{feature}"') - return - lines[line_num] += '-->' - _write_file(filename, lines) + for filename in filenames: + lines = _read_file(filename) + for line_num in _find_html_features(feature, lines): + _enable_feature_at(lines, line_num, feature, '-->\n') + unused_warn = False + for line_num in _find_css_js_features(feature, lines): + _enable_feature_at(lines, line_num, feature, '*/\n') + unused_warn = False + if unused_warn: + logging.warning(f'Couldn\'t find feature "{feature}" in file "{filename}"') + _write_file(filename, lines) def get_features() -> List[str]: return list(_FILE_OF_FEATURE.keys()) -def _decode_feature_file(feature: str) -> str: - filename = _FILE_OF_FEATURE.get(feature) - if not filename: + +def _decode_feature_files(feature: str) -> Optional[List[str]]: + filenames = _FILE_OF_FEATURE.get(feature) + if not filenames: logging.error(f'Non-existing feature: "{feature}"') - return "" + return None path = config.get('frontend').get('path') - return f'{path}/{filename}' + return [f'{path}/{filename}' for filename in filenames] + + +def _find_html_features(feature: str, lines: list) -> List[int]: + return _find_features(lines, f'