From e441033d6332300d7b40ee88cedd427b6c49e06d Mon Sep 17 00:00:00 2001 From: Arash Hatami Date: Sun, 26 Jan 2025 12:43:51 +0330 Subject: [PATCH 1/2] Improve template bootstraper script --- scripts/bootstrap-template.py | 121 ++++++++++++++++++++++++++++++---- 1 file changed, 107 insertions(+), 14 deletions(-) diff --git a/scripts/bootstrap-template.py b/scripts/bootstrap-template.py index 8f78fec..8ca54eb 100755 --- a/scripts/bootstrap-template.py +++ b/scripts/bootstrap-template.py @@ -1,33 +1,85 @@ #!/usr/bin/python3 -import sys, os, shutil, re +import os +import re +import shutil +import sys from pathlib import Path -def is_snake_case(s): - # Define the regex pattern for snake case with numbers +def is_snake_case(s: str) -> bool: + """ + Check if the provided string is in snake_case format. + Snake case is lower case with words separated by underscores, and it can contain digits. + + Args: + s (str): String to check. + + Returns: + bool: True if the string is in snake_case, False otherwise. + """ pattern = r"^[a-z0-9]+(_[a-z0-9]+)*$" + return bool(re.match(pattern, s)) - # Use re.match to check if the string matches the pattern - if re.match(pattern, s): - return True - else: - return False +def to_camel_case(snake_str: str) -> str: + """ + Convert a snake_case string to camelCase. -def to_camel_case(snake_str): + Args: + snake_str (str): String in snake_case to convert. + + Returns: + str: Converted string in camelCase. + """ return "".join(x.capitalize() for x in snake_str.lower().split("_")) -def replace(file_name, to_find, to_replace): +def replace(file_name: str, to_find: str, to_replace: str) -> None: + """ + Replace occurrences of a string within a file, ensuring placeholders are handled. + The function replaces the `to_find` string with `to_replace`, adds a placeholder, + and skips lines with placeholders already in place. + + Args: + file_name (str): Path to the file to perform replacement in. + to_find (str): String to search for in the file. + to_replace (str): String to replace `to_find` with. + + Returns: + None + """ with open(file_name, "r", encoding="utf8") as file: - filedata = file.read() - filedata = filedata.replace(to_find, to_replace) + filedata = file.readlines() + + new_filedata = [] + for line in filedata: + # Skip lines that have already been replaced by checking for placeholder + if "__REPLACEMENT_DONE__" in line: + new_filedata.append(line) + continue + + # Replace the target string, and add a placeholder for the next step + new_line = line.replace(to_find, to_replace) + if to_find in line: # Only add placeholder if the replace actually happened + new_line = new_line.replace(to_replace, to_replace + "__REPLACEMENT_DONE__") + new_filedata.append(new_line) + with open(file_name, "w", encoding="utf8") as file: - file.write(filedata) + file.writelines(new_filedata) + +def replace_everywhere(to_find: str, to_replace: str) -> None: + """ + Replace a string in all files in the project. -def replace_everywhere(to_find, to_replace): + Args: + to_find (str): String to search for in the file. + to_replace (str): String to replace `to_find` with. + + Returns: + None + """ for path in files_to_search: replace(path, to_find, to_replace) replace(path, to_find.capitalize(), to_camel_case(to_replace)) @@ -42,6 +94,45 @@ def replace_everywhere(to_find, to_replace): replace("./scripts/setup-custom-toolchain.sh", to_find, to_replace) +def remove_placeholder() -> None: + """ + Remove the placeholder from all files. + + Returns: + None + """ + for path in files_to_search: + replace_placeholders(path) + + replace_placeholders("./CMakeLists.txt") + replace_placeholders("./Makefile") + replace_placeholders("./Makefile") + replace_placeholders("./Makefile") + replace_placeholders("./README.md") + replace_placeholders("./extension_config.cmake") + replace_placeholders("./scripts/setup-custom-toolchain.sh") + + +def replace_placeholders(file_name: str) -> None: + """ + Remove the placeholder from a file. + + Args: + file_name (str): Path to the file to remove the placeholder from. + + Returns: + None + """ + with open(file_name, "r", encoding="utf8") as file: + filedata = file.read() + + # Remove all placeholders + filedata = filedata.replace("__REPLACEMENT_DONE__", "") + + with open(file_name, "w", encoding="utf8") as file: + file.write(filedata) + + if __name__ == "__main__": if len(sys.argv) != 2: raise Exception( @@ -74,6 +165,8 @@ def replace_everywhere(to_find, to_replace): replace_everywhere("Quack", name_extension.capitalize()) replace_everywhere("", name_extension) + remove_placeholder() + string_to_replace = name_extension string_to_find = "quack" From 7e2c6e13d120bdc732e940d3e513871911775ac5 Mon Sep 17 00:00:00 2001 From: Arash Hatami Date: Fri, 31 Jan 2025 11:46:44 +0330 Subject: [PATCH 2/2] Improve template bootstraper script --- scripts/bootstrap-template.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/scripts/bootstrap-template.py b/scripts/bootstrap-template.py index 8ca54eb..9baf974 100755 --- a/scripts/bootstrap-template.py +++ b/scripts/bootstrap-template.py @@ -59,11 +59,23 @@ def replace(file_name: str, to_find: str, to_replace: str) -> None: new_filedata.append(line) continue - # Replace the target string, and add a placeholder for the next step - new_line = line.replace(to_find, to_replace) - if to_find in line: # Only add placeholder if the replace actually happened - new_line = new_line.replace(to_replace, to_replace + "__REPLACEMENT_DONE__") - new_filedata.append(new_line) + modified_line = line.replace( + to_find, + to_replace, + ) + modified_line = modified_line.replace( + to_find.capitalize(), to_camel_case(to_replace) + ) + modified_line = modified_line.replace( + to_find.upper(), + to_replace.upper(), + ) + + # Add placeholder once after all replacements + if to_find in line or to_find.capitalize() in line or to_find.upper() in line: + modified_line += "__REPLACEMENT_DONE__" + + new_filedata.append(modified_line) with open(file_name, "w", encoding="utf8") as file: file.writelines(new_filedata)