Skip to content

Commit

Permalink
fix: improved conditional file handling for pylint/ruff + cicd
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSchischke committed Sep 26, 2024
1 parent b5218e9 commit 48240de
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 39 deletions.
60 changes: 30 additions & 30 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import subprocess
from abc import ABC, abstractmethod

import yaml


# as cookiecutter is currently (v2.1.1) unable to support local imports in hooks
# the environment management code has to be included here
Expand Down Expand Up @@ -107,34 +105,34 @@ def copy_chosen_files(self) -> None:
else:
shutil.copy2(src, dst)

def remove_unused_linter_files(self):
with open(f"{self.temp_files_dir}/.manifest.yaml", "r", encoding="utf-8") as f:
manifest = yaml.safe_load(f)
for feature in manifest["features"]:
if not feature["enabled"]:
for resource in feature["resources"]:
os.remove(resource)


def get_ci_cd_file_manager(ci_cd_options: str) -> ConditionalFileManager:
def get_conditional_file_manager(ci_cd_option: str, linter_option: str) -> ConditionalFileManager:
template_root_dir = pathlib.Path.cwd()
temp_files_dir = template_root_dir.joinpath(".temp_ci_cd")
if ci_cd_options == "none":
manager = ConditionalFileManager(
temp_files_dir=temp_files_dir,
template_root_dir=template_root_dir,
relevant_paths_list=[],
)
elif ci_cd_options == "gitlab":
manager = ConditionalFileManager(
temp_files_dir=temp_files_dir,
template_root_dir=template_root_dir,
relevant_paths_list=[".gitlab-ci.yml"],
)
else:
raise NotImplementedError(
f"Option {ci_cd_options} is not implemented as ci_cd_file_manager"
)
temp_files_dir = template_root_dir.joinpath(".conditional_files")
relevant_paths = []

match ci_cd_option:
case "gitlab":
relevant_paths.append(".gitlab-ci.yml")
case "none":
pass
case _:
raise NotImplementedError(f"Option {ci_cd_option} is not implemented as CI/CD pipeline")

match linter_option:
case "pylint":
relevant_paths.append(".pylintrc")
case "ruff":
relevant_paths.append("ruff.toml")
case _:
raise NotImplementedError(f"Option {linter_option} is not implemented as Linters")

manager = ConditionalFileManager(
temp_files_dir=temp_files_dir,
template_root_dir=template_root_dir,
relevant_paths_list=relevant_paths,
)

return manager


Expand All @@ -153,11 +151,13 @@ def get_ci_cd_file_manager(ci_cd_options: str) -> ConditionalFileManager:
print("before")
# setup ci/cd related files (if any)
print("initializing")
CICD_FILE_MANAGER = get_ci_cd_file_manager(ci_cd_options="{{cookiecutter.cicd_configuration}}")
CICD_FILE_MANAGER = get_conditional_file_manager(
ci_cd_option="{{cookiecutter.cicd_configuration}}",
linter_option="{{cookiecutter.linter_name}}",
)
print("copying")
CICD_FILE_MANAGER.copy_chosen_files()
print("cleaning")
CICD_FILE_MANAGER.remove_unused_linter_files()
CICD_FILE_MANAGER.clean_temp_dir()
print("after")

Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 0 additions & 9 deletions {{cookiecutter.repo_name}}/.temp_ci_cd/.manifest.yaml

This file was deleted.

0 comments on commit 48240de

Please sign in to comment.