diff --git a/unifree/free.py b/unifree/free.py index 4dad3b2..2154b8d 100644 --- a/unifree/free.py +++ b/unifree/free.py @@ -11,6 +11,7 @@ import unifree from unifree import utils, log +from unifree.log import ExitCode def run_migration( @@ -32,15 +33,15 @@ def run_migration( except Exception as e: log.error(f"Unable to start: {config} is invalid: {e}", exc_info=e) - return 78 # os.EX_CONFIG + return ExitCode.EX_CONFIG.value if not os.path.exists(source): log.error(f"Unable to start: source folder does not exist ('{source}')") - return 78 # os.EX_CONFIG + return ExitCode.EX_CONFIG.value if not os.path.isdir(source): log.error(f"Unable to start: source folder is not a folder ('{source}')") - return 78 # os.EX_CONFIG + return ExitCode.EX_CONFIG.value if os.path.exists(destination): log.info(f"Using existing destination folder '{destination}'") @@ -51,7 +52,7 @@ def run_migration( os.makedirs(destination) except Exception as e: log.error(f"Unable to create destination folder '{destination}': {e}", exc_info=e) - return 74 # os.EX_IOERR + return ExitCode.EX_IOERR.value from unifree.project_migration_strategies import CreateMigrations, ExecuteMigrations @@ -67,7 +68,7 @@ def run_migration( return os.EX_OK except Exception as e: log.error(f"Unable to migrate: {e}", exc_info=e) - return 70 # os.EX_SOFTWARE + return ExitCode.EX_SOFTWARE.value def migrate(): @@ -113,7 +114,7 @@ def migrate(): args, _ = args_parser.parse_known_args() except SystemExit: args_parser.print_usage() - sys.exit(78) # os.EX_CONFIG + sys.exit(ExitCode.EX_CONFIG.value) args = vars(args) diff --git a/unifree/log.py b/unifree/log.py index 3157f98..81c6436 100644 --- a/unifree/log.py +++ b/unifree/log.py @@ -8,6 +8,12 @@ import traceback import unifree +from enum import Enum + +class ExitCode(Enum): + EX_SOFTWARE = 70 + EX_IOERR = 74 + EX_CONFIG = 78 py_logger = logging.getLogger("lion") diff --git a/unifree/source_code_parsers.py b/unifree/source_code_parsers.py index 494136a..01067aa 100644 --- a/unifree/source_code_parsers.py +++ b/unifree/source_code_parsers.py @@ -14,7 +14,7 @@ class CSharpCodeParser: _parser: tree_sitter.Parser - _config: Dict + _config: Dict[str, any] def __init__(self, config: Dict) -> None: self._config = config @@ -68,13 +68,26 @@ def _c_sharp_library_path(cls) -> str: c_sharp_library_folder_path = os.path.join(unifree.project_root, 'vendor', 'build', 'libraries') return os.path.join(c_sharp_library_folder_path, 'c-sharp.so') - def _replace_macros_with_comments(self, source_str): - result = '' - for line in source_str.split("\n"): - stripped_line = line.rstrip().lstrip() - if stripped_line.startswith("#"): - result += "// " + line + "\n" + @classmethod + def _find_vendor_folder(cls, current_folder_path: str, remaining_depth: int) -> str: + if remaining_depth <= 0: + log.error("Unable to locate 'vendor' folder that is necessary for reading the code. Please make sure your project is setup correctly") + raise RuntimeError(f"Can't locate 'vendor' folder") + + if os.path.isdir(current_folder_path): + for file_name in os.listdir(current_folder_path): + if os.path.isdir(os.path.join(current_folder_path, file_name)) and file_name == 'vendor': + return current_folder_path + + return cls._find_vendor_folder(os.path.dirname(current_folder_path), remaining_depth - 1) + + def _replace_macros_with_comments(self, source_str : str) -> str: + result = [] + lines = [line.strip() for line in source_str.split("\n") if line.strip() != ''] + for line in lines: + if line.startswith("#"): + result.append("// " + line) else: - result += line + "\n" + result.append(line) - return result + return "\n".join(result)