From fd600375ae174c238f4af2be48b25b46f90947b1 Mon Sep 17 00:00:00 2001 From: Samuel Duhaime-Morissette <78976679+samuel-duhaime@users.noreply.github.com> Date: Thu, 7 Mar 2024 09:28:08 -0500 Subject: [PATCH] Generate widgetsConfig and sections with Generator (#391) --- .../src/examples/generatorExampleConfigs.yaml | 7 +++ .../src/scripts/generate_sections.py | 38 ++++++++++++ .../src/scripts/generate_survey.py | 9 +++ .../src/scripts/generate_widgets.py | 4 +- .../src/scripts/generate_widgets_config.py | 60 +++++++++++++++++++ 5 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 packages/evolution-generator/src/scripts/generate_sections.py create mode 100644 packages/evolution-generator/src/scripts/generate_widgets_config.py diff --git a/packages/evolution-generator/src/examples/generatorExampleConfigs.yaml b/packages/evolution-generator/src/examples/generatorExampleConfigs.yaml index 6ddc038f..22ab55da 100644 --- a/packages/evolution-generator/src/examples/generatorExampleConfigs.yaml +++ b/packages/evolution-generator/src/examples/generatorExampleConfigs.yaml @@ -4,7 +4,14 @@ survey: excel: active_script: false +sections: + output_file: ../../../survey/src/survey/sections.ts + sections: + - home + - end + widgets: + output_file: ../../../survey/src/survey/widgetsConfig.tsx output_info_list: - output_folder: ../../example/demo_survey_generator/sections/home section: home diff --git a/packages/evolution-generator/src/scripts/generate_sections.py b/packages/evolution-generator/src/scripts/generate_sections.py new file mode 100644 index 00000000..a712b66d --- /dev/null +++ b/packages/evolution-generator/src/scripts/generate_sections.py @@ -0,0 +1,38 @@ +# Copyright 2024, Polytechnique Montreal and contributors +# This file is licensed under the MIT License. +# License text available at https://opensource.org/licenses/MIT + +# Note: This script includes functions that generate the sections.ts file. +# These functions are intended to be invoked from the generate_survey.py script. + +from typing import List + +# Function to generate sections.ts +def generate_sections(output_file: str, sections: List[str]): + try: + ts_code: str = "" # TypeScript code to be written to file + indentation: str = " " # 4-space indentation + + # Generate the import statements + # Loop through each section and generate an import statement + for section in sections: + ts_code += f"import {section}Configs from './sections/{section}/configs';\n" + + # Generate the export statement + ts_code += "\n// Export all the sections configs\n" + ts_code += "export default {\n" + # Loop through each section and generate an export statement + for section in sections: + ts_code += f"{indentation}{section}: {section}Configs,\n" + ts_code += "};\n" + + # Write TypeScript code to a file + with open(output_file, mode="w", encoding="utf-8", newline="\n") as ts_file: + ts_file.write(ts_code) + + print(f"Generate {output_file} successfully") + + except Exception as e: + # Handle any other exceptions that might occur during script execution + print(f"Error with sections.ts: {e}") + raise e diff --git a/packages/evolution-generator/src/scripts/generate_survey.py b/packages/evolution-generator/src/scripts/generate_survey.py index 3fed3aed..4eb68869 100644 --- a/packages/evolution-generator/src/scripts/generate_survey.py +++ b/packages/evolution-generator/src/scripts/generate_survey.py @@ -9,6 +9,8 @@ import os # For environment variables import yaml # For reading the yaml file from scripts.generate_excel import generate_excel +from scripts.generate_sections import generate_sections +from scripts.generate_widgets_config import generate_widgets_config from scripts.generate_widgets import generate_widgets from scripts.generate_conditionals import generate_conditionals from scripts.generate_choices import generate_choices @@ -29,6 +31,7 @@ def generate_survey(config_path): # Get the data from the YAML file survey = surveyGenerator["survey"] excel = surveyGenerator["excel"] + sections = surveyGenerator["sections"] widgets = surveyGenerator["widgets"] conditionals = surveyGenerator["conditionals"] choices = surveyGenerator["choices"] @@ -45,6 +48,12 @@ def generate_survey(config_path): os.getenv("OFFICE365_PASSWORD"), ) + # Call the generate_sections function to generate sections.tsx + generate_sections(sections["output_file"], sections["sections"]) + + # Call the generate_widgets_config function to generate widgetsConfig.tsx + generate_widgets_config(widgets["output_file"], sections["sections"]) + # Call the generate_widgets function to generate widgets.tsx for each section generate_widgets(survey["excel_file"], widgets["output_info_list"]) diff --git a/packages/evolution-generator/src/scripts/generate_widgets.py b/packages/evolution-generator/src/scripts/generate_widgets.py index b5ee6cd0..005814cd 100644 --- a/packages/evolution-generator/src/scripts/generate_widgets.py +++ b/packages/evolution-generator/src/scripts/generate_widgets.py @@ -71,12 +71,12 @@ def convert_excel_to_typescript(section): # Write the transformed content to the widgets output file with open(output_info['output_folder'] + '/widgets.tsx', mode='w', encoding='utf-8', newline='\n') as f: f.write(transformed_content['widgetsStatements']) - print(f"Generate {output_info['output_folder']}widgets.tsx successfully") + print(f"Generate {output_info['output_folder']}/widgets.tsx successfully") # Write the transformed content to the widgetsNames output file with open(output_info['output_folder'] + '/widgetsNames.ts', mode='w', encoding='utf-8', newline='\n') as f: f.write(transformed_content['widgetsNamesStatements']) - print(f"Generate {output_info['output_folder']}widgetsNames.ts successfully") + print(f"Generate {output_info['output_folder']}/widgetsNames.ts successfully") except Exception as e: print(f"Error with widgets: {e}") diff --git a/packages/evolution-generator/src/scripts/generate_widgets_config.py b/packages/evolution-generator/src/scripts/generate_widgets_config.py new file mode 100644 index 00000000..5012bce8 --- /dev/null +++ b/packages/evolution-generator/src/scripts/generate_widgets_config.py @@ -0,0 +1,60 @@ +# Copyright 2024, Polytechnique Montreal and contributors +# This file is licensed under the MIT License. +# License text available at https://opensource.org/licenses/MIT + +# Note: This script includes functions that generate the widgetsConfig.tsx file. +# These functions are intended to be invoked from the generate_survey.py script. + +from typing import List + + +# Function to generate widgetsConfig.tsx +def generate_widgets_config(output_file: str, sections: List[str]): + try: + ts_code: str = "" # TypeScript code to be written to file + indentation: str = " " # 4-space indentation + + # Generate the import statements + # Loop through each section and generate an import statement + for section in sections: + ts_code += f"import * as {section}Widgets from './sections/{section}/widgets';\n" + + # Generate the widgets + ts_code += "\n// Define all the widgets\n" + ts_code += "const widgets: { [key: string]: any } = {};\n" + ts_code += "\n// Define all the sections widgets\n" + ts_code += "const sectionsWidgets = [\n" + # Loop through each section and generate a sectionWidgets array + for section in sections: + ts_code += f"{indentation}{section}Widgets,\n" + ts_code += "];\n" + + # Generate the loop to add all the widgets to the widgets object + ts_code += "\n// Loop all sections and add their widgets to the widgets object\n" + ts_code += "sectionsWidgets.forEach((section) => {\n" + ts_code += f"{indentation}for (const widget in section) {{\n" + ts_code += f"{indentation}{indentation}widgets[widget] = section[widget];\n" + ts_code += f"{indentation}}}\n" + ts_code += "});\n" + + # // Loop all sections and add their widgets to the widgets object + # sectionsWidgets.forEach((section) => { + # for (const widget in section) { + # widgets[widget] = section[widget]; + # } + # }); + + # Generate the export statement + ts_code += "\n// Export all the widgets\n" + ts_code += "export { widgets };\n" + + # Write TypeScript code to a file + with open(output_file, mode="w", encoding="utf-8", newline="\n") as ts_file: + ts_file.write(ts_code) + + print(f"Generate {output_file} successfully") + + except Exception as e: + # Handle any other exceptions that might occur during script execution + print(f"Error with widgetsConfig.tsx: {e}") + raise e