Skip to content

Commit

Permalink
Generate widgetsConfig and sections with Generator (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel-duhaime authored Mar 7, 2024
1 parent 3602ea8 commit fd60037
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions packages/evolution-generator/src/scripts/generate_sections.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions packages/evolution-generator/src/scripts/generate_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
Expand All @@ -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"])

Expand Down
4 changes: 2 additions & 2 deletions packages/evolution-generator/src/scripts/generate_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fd60037

Please sign in to comment.