Skip to content

Commit

Permalink
feat: add ci files by user choice
Browse files Browse the repository at this point in the history
  • Loading branch information
pppmlt committed Aug 11, 2023
1 parent 0c83168 commit 1649380
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"repo_name": "{{ cookiecutter.project_name.lower().replace(' ', '-') }}",
"repo_url": "URL to repository",
"env_name": "{{ cookiecutter.project_name.lower().replace(' ', '-') + '-env' }}",
"install_jupyter": ["yes", "no"]
"install_jupyter": ["yes", "no"],
"cicd_configuration": ["none", "gitlab"]
}
70 changes: 70 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# standard library imports
import os
import pathlib
import shutil
import subprocess
from abc import ABC, abstractmethod

Expand Down Expand Up @@ -68,6 +69,65 @@ def get_package_manager() -> CondaLikePackageManager:
raise RuntimeError("Could not determine conda-like package manager.")


def _verify_if_dir_valid(path_to_dir: pathlib.Path):
if not path_to_dir.is_dir():
raise RuntimeError(f"{path_to_dir} is not a valid directory.")


class ConditionalFileManager:
def __init__(
self,
temp_files_dir: pathlib.Path,
template_root_dir: pathlib.Path,
relevant_paths_list: list[pathlib.Path],
) -> None:
_verify_if_dir_valid(temp_files_dir)
self.temp_files_dir = temp_files_dir
_verify_if_dir_valid(template_root_dir)
self.template_root_dir = template_root_dir
for relevant_path in relevant_paths_list:
if not self.temp_files_dir.joinpath(relevant_path).exists():
raise RuntimeError(
f"A relevant path {relevant_path} does not exist in temp dir {self.temp_files_dir}."
)
self.relevant_paths_list = relevant_paths_list

def clean_temp_dir(self) -> None:
_verify_if_dir_valid(path_to_dir=self.temp_files_dir)
shutil.rmtree(self.temp_files_dir)

def copy_chosen_files(self) -> None:
for relevant_path in self.relevant_paths_list:
src = self.temp_files_dir.joinpath(relevant_path)
dst = self.template_root_dir.joinpath(relevant_path)
if src.is_dir():
shutil.copytree(src, dst)
else:
shutil.copy2(src, dst)


def get_ci_cd_file_manager(ci_cd_options: 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"
)
return manager


if __name__ == "__main__":
# actual hook starts here
# initialize new project repository
Expand All @@ -80,6 +140,16 @@ def get_package_manager() -> CondaLikePackageManager:
PACKAGE_MANAGER.create_env_from_yaml_file(yaml_file_path=pathlib.Path("environment.yaml"))
PACKAGE_MANAGER.run_subprocess_in_env(env_name="{{cookiecutter.env_name}}", cmd=["pre-commit"])

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}}")
print("copying")
CICD_FILE_MANAGER.copy_chosen_files()
print("cleaning")
CICD_FILE_MANAGER.clean_temp_dir()
print("after")

# add template files to git and create initial commit
subprocess.run(["git", "add", "."], check=True)
subprocess.run(
Expand Down
4 changes: 0 additions & 4 deletions tests/test_cookiecutter_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# local imports
from hooks.post_gen_project import get_package_manager


TEMPLATE_DIRECTORY = str(pathlib.Path(__file__).parent.parent)
PACKAGE_MANAGER = get_package_manager()

Expand All @@ -37,9 +36,6 @@ def test_default_template(default_template):
".commitlintrc.yaml",
".gitattributes",
".gitignore",
".gitlab-ci-stages.yaml",
".gitlab-ci-test.yaml",
".gitlab-ci.yml",
".pre-commit-config.yaml",
".prettierrc",
".pylintrc",
Expand Down
File renamed without changes.

0 comments on commit 1649380

Please sign in to comment.