From be2c515d3b3ff3a478603af9c89c7540fd422110 Mon Sep 17 00:00:00 2001 From: Pietro Monticone <38562595+pitmonticone@users.noreply.github.com> Date: Tue, 6 Aug 2024 14:38:46 +0200 Subject: [PATCH] Automate template customisation --- README.md | 22 +++++++----- customize_template.py | 83 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 customize_template.py diff --git a/README.md b/README.md index 9a767fd..debac24 100644 --- a/README.md +++ b/README.md @@ -68,15 +68,19 @@ Lean projects. ## Customize this Template -To tailor this template to your specific project, you need to: - -- [`lint.yml`](.github/workflows/lint.yml): Find and replace `Project` with your actual -project name. -- [`Project`](Project): Rename the folder to match your actual project name. -- [`CONTRIBUTING.md`](CONTRIBUTING.md): Customise the guidelines for contributing to the project. -- [`lakefile.toml`](lakefile.toml): Find and replace `Project` with your actual project name. -- [`Project.lean`](Project.lean): Rename the main file and the imports -to match your actual project name. +To tailor this template to your specific project, follow these steps: + +1. Ensure your terminal is in the project directory by running the following command: + ```bash + cd path/to/your/project + ``` +2. Execute the customization script by running: + ```bash + python3 customize_template.py NewProjectName + ``` + where `NewProjectName` must be replaced by the name of your project. + +The script [`customize_template.py`](customize_template.py) will automatically rename the project folder and update the necessary files and configurations to match the new project name. ## Blueprint diff --git a/customize_template.py b/customize_template.py new file mode 100644 index 0000000..4b2b2c7 --- /dev/null +++ b/customize_template.py @@ -0,0 +1,83 @@ +import os +import shutil +import sys + +def replace_text_in_file(filepath, old_text, new_text): + """ + Replace all occurrences of old_text with new_text in the specified file. + + Arguments: + filepath (str): The path to the file where text needs to be replaced. + old_text (str): The text to be replaced. + new_text (str): The text to replace with. + """ + with open(filepath, 'r') as file: + filedata = file.read() + + filedata = filedata.replace(old_text, new_text) + + with open(filepath, 'w') as file: + file.write(filedata) + +def rename_directory(old_name, new_name): + """ + Rename a directory from old_name to new_name. + + Arguments: + old_name (str): The current name of the directory. + new_name (str): The new name for the directory. + """ + if os.path.exists(old_name): + os.rename(old_name, new_name) + +# def remove_file(filepath): +# """ +# Remove the specified file. + +# Arguments: +# filepath (str): The path to the file to be removed. +# """ +# if os.path.exists(filepath): +# os.remove(filepath) + +def main(project_name): + """ + Perform all the customization steps for the template. + + Arguments: + project_name (str): The name of the new project. + """ + # Define paths to the files and directories to be modified + lint_yml_path = '.github/workflows/lint.yml' + project_folder = 'Project' + example_file = os.path.join(project_folder, 'Example.lean') + contributing_md = 'CONTRIBUTING.md' + lakefile_toml = 'lakefile.toml' + project_lean = 'Project.lean' + + # Replace 'Project' with the actual project name in the necessary files + replace_text_in_file(lint_yml_path, 'Project', project_name) + replace_text_in_file(lakefile_toml, 'Project', project_name) + + # Rename 'Project' folder to match the project name + rename_directory(project_folder, project_name) + + # Notify the user to customize 'CONTRIBUTING.md' manually + print(f"Please customize {contributing_md} manually to set up the contribution guidelines for your project.") + + # Rename 'Project.lean' to match the project name and update imports + if os.path.exists(project_lean): + new_project_lean = f"{project_name}.lean" + os.rename(project_lean, new_project_lean) + replace_text_in_file(new_project_lean, 'Project', project_name) + +if __name__ == "__main__": + # Check if the script is executed with the correct number of command-line arguments + if len(sys.argv) != 2: + print("Usage: python customize_template.py ") + sys.exit(1) + + # Get the project name from the command-line arguments + project_name = sys.argv[1] + # Call the main function to perform the customization + main(project_name)