Skip to content

Commit

Permalink
Andrewi/264 output folder prep (#281)
Browse files Browse the repository at this point in the history
* prepared output directory in cangen Fixes #264

* formatted with ruff

* changed order of cleaning output dir

* refactored cleanig output dir

* reverting last commit

* deleted folder contents instead of folder itself

* formated with ruff

* changed prepare output dir
  • Loading branch information
AndrewI26 authored Nov 5, 2024
1 parent 4b7f411 commit 7c8d598
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions scripts/cangen/cangen/can_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,6 @@ def _generate_from_jinja2_template(
template = env.get_template(template_path)
rendered_code = template.render(**context_dict)

# Write the rendered code to a file
output_dir = os.path.dirname(output_path)
os.makedirs(output_dir, exist_ok=True)

# Create a git ignore for everything in the generated path. Ignore everything.
gitignore_path = os.path.join(output_dir, ".gitignore")
if not os.path.exists(gitignore_path):
with open(gitignore_path, "w") as f:
f.write("*")

with open(output_path, "w") as output_file:
output_file.write(rendered_code)

Expand Down Expand Up @@ -247,13 +237,22 @@ def generate_code(bus: Bus, config: Config):
logger.info("Code generation complete")


def _prepare_output_directory(output_dir):
"""Deletes previously generated files and creates a gitignore for the directory"""
if os.path.exists(output_dir):
shutil.rmtree(output_dir)
os.makedirs(output_dir, exist_ok=True)

gitignore_path = os.path.join(output_dir, ".gitignore")
with open(gitignore_path, "w") as f:
f.write("*")


def generate_can_from_dbc(project_folder_name: str):
os.chdir(project_folder_name)
config = Config.from_yaml("config.yaml")

# Deletes output path folder and files within, before creating new ones
if os.path.exists(config.output_dir):
shutil.rmtree(config.output_dir)
_prepare_output_directory(config.output_dir)

for bus in config.busses:
generate_code(bus, config)

0 comments on commit 7c8d598

Please sign in to comment.