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

@@ -18,7 +18,7 @@ def __remove_dir_if_exists(dir_path):
if os.path.isdir(dir_path): if os.path.isdir(dir_path):
shutil.rmtree(dir_path) shutil.rmtree(dir_path)
def generate(project_dir): def generate(project_dir, registry_url):
# Parsing del file di configurazione # Parsing del file di configurazione
config = __parse_yaml(f"{project_dir}/workflow.yaml") config = __parse_yaml(f"{project_dir}/workflow.yaml")
@@ -36,5 +36,17 @@ def generate(project_dir):
# for each task in the workflow # for each task in the workflow
for task in config['tasks']: for task in config['tasks']:
template_compiler.handle_task(task, output_dir)
try:
task['registry_url'] = registry_url
template_compiler.handle_task(task, output_dir)
# Copy the code file to the output folder
shutil.copy2(f"{project_dir}/tasks/{task['code']}", f"{output_dir}/{task['component_name']}/{task['code']}")
print(f" - Task {task['component_name']} generated")
except Exception as e:
logging.error(f"Error generating task {task['component_name']}: {e}")
continue

View File

@@ -1,4 +1,7 @@
import logging import logging
from jinja2 import FileSystemLoader, Environment
import os
import shutil
def handle_task(task, output_dir): def handle_task(task, output_dir):
@@ -19,10 +22,57 @@ def handle_task(task, output_dir):
pass pass
def __generate_producer(task, output_dir): 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): def __generate_processor(task, output_dir):
pass pass
def __generate_dbsync(task, output_dir): 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