Repository: kedro-org/kedro
File: kedro/templates/project/hooks/utils.py (Python)

Code before:
        pyproject_file_path (str): The path of the `pyproject.toml` in the template
        python_package_name (str): The name of the python package.
    """
    if "Linting" not in selected_add_ons_list:
        _remove_from_file(requirements_file_path, lint_requirements)
        _remove_from_toml(pyproject_file_path, lint_pyproject_requirements)

    if "Testing" not in selected_add_ons_list:
        _remove_from_file(requirements_file_path, test_requirements)
        _remove_from_toml(pyproject_file_path, test_pyproject_requirements)
        _remove_dir(current_dir / "tests")

    if "Logging" not in selected_add_ons_list:
        _remove_file(current_dir / "conf/logging.yml")

    if "Documentation" not in selected_add_ons_list:
        _remove_from_toml(pyproject_file_path, docs_pyproject_requirements)
        _remove_dir(current_dir / "docs")

    if "Data Structure" not in selected_add_ons_list and example_pipeline != "True":
        _remove_dir(current_dir / "data")

    if "Pyspark" in selected_add_ons_list and example_pipeline != "True":
        _handle_starter_setup(selected_add_ons_list, python_package_name)

    if "Kedro Viz" in selected_add_ons_list and example_pipeline != "True":
        _handle_starter_setup(selected_add_ons_list, python_package_name)


def sort_requirements(requirements_file_path: Path) -> None:
    """Sort the requirements.txt file alphabetically and write it back to the file.

    Args:
        requirements_file_path (Path): The path to the `requirements.txt` file.
    """
    with open(requirements_file_path, 'r') as requirements:
        lines = requirements.readlines()

    lines = [line.strip() for line in lines]
    lines.sort()
    sorted_content = '\n'.join(lines)

    with open(requirements_file_path, 'w') as requirements:
        requirements.write(sorted_content)


Review comment (suggestion):
I think the logic here is correct but it is hard to follow. I notice that `_handle_starter_setup` is highly coupled with `PySpark` and `Kedro Viz`, in fact this function only get called if these plugins get selected.

1. The only difference is if viz is selected, one extra `reporting` pipeline is removed. I think this should reflected in the argument of `_handle_starter_setup`, we don't really need `selected_add_ons_list`, a boolean flag is enough.

2. Maybe it helps to have a nested `if` here, for me it easier to map it to the diagram. 



Code after:
    """
    if "Linting" not in selected_add_ons_list:
        _remove_from_file(requirements_file_path, lint_requirements)
        _remove_from_toml(pyproject_file_path, lint_pyproject_requirements)

    if "Testing" not in selected_add_ons_list:
        _remove_from_file(requirements_file_path, test_requirements)
        _remove_from_toml(pyproject_file_path, test_pyproject_requirements)
        _remove_dir(current_dir / "tests")

    if "Logging" not in selected_add_ons_list:
        _remove_file(current_dir / "conf/logging.yml")

    if "Documentation" not in selected_add_ons_list:
        _remove_from_toml(pyproject_file_path, docs_pyproject_requirements)
        _remove_dir(current_dir / "docs")

    if "Data Structure" not in selected_add_ons_list and example_pipeline != "True":
        _remove_dir(current_dir / "data")

    if ("Pyspark" in selected_add_ons_list or "Kedro Viz" in selected_add_ons_list) and example_pipeline != "True":
        _remove_pyspark_viz_starter_files("Kedro Viz" in selected_add_ons_list, python_package_name)


def sort_requirements(requirements_file_path: Path) -> None:
    """Sort the requirements.txt file alphabetically and write it back to the file.

    Args:
        requirements_file_path (Path): The path to the `requirements.txt` file.
    """
    with open(requirements_file_path, 'r') as requirements:
        lines = requirements.readlines()

    lines = [line.strip() for line in lines]
    lines.sort()
    sorted_content = '\n'.join(lines)

    with open(requirements_file_path, 'w') as requirements:
        requirements.write(sorted_content)