Code generation completed

This commit is contained in:
Lorenzo Venerandi
2025-01-29 13:28:09 +01:00
parent 374570f226
commit ca036acaf3
2 changed files with 67 additions and 5 deletions

View File

@@ -1,4 +1,7 @@
import logging
from jinja2 import FileSystemLoader, Environment
import os
import shutil
def handle_task(task, output_dir):
@@ -19,10 +22,57 @@ def handle_task(task, output_dir):
pass
def __generate_producer(task, output_dir):
pass
# Copy the template folder to the output folder
__copytree("src/code_generator/templates/producer_nats", f"{output_dir}/{task['component_name']}")
# Replace each file of the output dir with the template
for filename in os.listdir(f"{output_dir}/{task['component_name']}"):
# Skip the following files
if filename in ['Dockerfile', 'go.mod', 'go.sum', 'tools.go']:
continue
# Skip if it's a directory
if os.path.isdir(f"{output_dir}/{task['component_name']}/{filename}"):
continue
__replace_file_with_template(filename, f"{output_dir}/{task['component_name']}", task)
def __generate_processor(task, output_dir):
pass
def __generate_dbsync(task, output_dir):
pass
pass
def __copytree(src, dst, symlinks=False, ignore=None):
# Create the destination directory
os.makedirs(dst, exist_ok=True)
# Copy the files
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
def __replace_file_with_template(filename, output_dir, template_vars):
try:
templateLoader = FileSystemLoader(searchpath=output_dir)
templateEnv = Environment(loader=templateLoader)
template = templateEnv.get_template(filename)
outputText = template.render(template_vars)
# Replace the file with the rendered template
with open(f"{output_dir}/{filename}", 'w') as f:
f.write(outputText)
except Exception as e:
logging.error(f"Error rendering template {filename}: {e}")
return