|
| 1 | +# Copyright (c) 2024 Andi Hellmund. All rights reserved. |
| 2 | + |
| 3 | +# This work is licensed under the terms of the BSD-3-Clause license. |
| 4 | +# For a copy, see <https://opensource.org/license/bsd-3-clause>. |
| 5 | + |
| 6 | + |
| 7 | +from pathlib import Path |
| 8 | +from textwrap import dedent |
| 9 | + |
| 10 | +from cpp_dev.project.dependency.types import PackageDependency |
| 11 | +from cpp_dev.project.lockfile import create_initial_lock_file |
| 12 | + |
| 13 | +from .config import create_project_config |
| 14 | +from .constants import compose_include_file, compose_source_file |
| 15 | +from .types import DependencyType, ProjectConfig |
| 16 | + |
| 17 | +############################################################################### |
| 18 | +# Public API ### |
| 19 | +############################################################################### |
| 20 | + |
| 21 | + |
| 22 | +def setup_project( |
| 23 | + project_config: ProjectConfig, |
| 24 | + parent_dir: Path | None = None, |
| 25 | +) -> Path: |
| 26 | + """Create a new cpp-dev project in the specified parent directory. |
| 27 | +
|
| 28 | + The path to the new project directory is returned. |
| 29 | + """ |
| 30 | + project_dir = _validate_project_dir(parent_dir, project_config.name) |
| 31 | + create_project_config(project_dir, project_config) |
| 32 | + create_initial_lock_file(project_dir) |
| 33 | + _add_default_cpd_dependencies(project_dir) |
| 34 | + _create_project_files(project_dir, project_config.name) |
| 35 | + return project_dir |
| 36 | + |
| 37 | + |
| 38 | +def add_package_dependency(project_dir: Path, deps: list[PackageDependency], dep_type: DependencyType) -> None: |
| 39 | + """Add package dependencies to the project for the given type.""" |
| 40 | + |
| 41 | + |
| 42 | +############################################################################### |
| 43 | +# Implementation ### |
| 44 | +############################################################################### |
| 45 | + |
| 46 | + |
| 47 | +def _validate_project_dir(parent_dir: Path | None, name: str) -> Path: |
| 48 | + """Check and validate if the project directory does not yet exist.""" |
| 49 | + if parent_dir is None: |
| 50 | + parent_dir = Path.cwd() |
| 51 | + project_dir = parent_dir / name |
| 52 | + if project_dir.exists(): |
| 53 | + raise ValueError(f"Project directory {project_dir} already exists.") |
| 54 | + project_dir.mkdir(parents=True) |
| 55 | + return project_dir |
| 56 | + |
| 57 | + |
| 58 | +def _add_default_cpd_dependencies(project_dir: Path) -> None: |
| 59 | + add_package_dependency(project_dir, [PackageDependency("llvm"), PackageDependency("gtest")], "cpd") |
| 60 | + |
| 61 | + |
| 62 | +def _create_project_files(project_dir: Path, name: str) -> None: |
| 63 | + """Create the necessary project files for the cpp-dev package.""" |
| 64 | + _create_library_include_file(project_dir, name) |
| 65 | + _create_library_source_file(project_dir, name) |
| 66 | + _create_library_test_file(project_dir, name) |
| 67 | + |
| 68 | + |
| 69 | +def _create_library_include_file(project_dir: Path, name: str) -> None: |
| 70 | + include_file = compose_include_file(project_dir, name, f"{name}.hpp") |
| 71 | + include_file.parent.mkdir(parents=True) |
| 72 | + include_file.write_text( |
| 73 | + dedent( |
| 74 | + f"""\ |
| 75 | + #pragma once |
| 76 | +
|
| 77 | + namespace {name} {{ |
| 78 | + int api(); |
| 79 | + }} |
| 80 | + """, |
| 81 | + ), |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def _create_library_source_file(project_dir: Path, name: str) -> None: |
| 86 | + source_file = compose_source_file(project_dir, f"{name}.cpp") |
| 87 | + source_file.parent.mkdir(parents=True) |
| 88 | + source_file.write_text( |
| 89 | + dedent( |
| 90 | + f"""\ |
| 91 | + #include "{name}/{name}.hpp" |
| 92 | +
|
| 93 | + int {name}::api() {{ |
| 94 | + return 42; |
| 95 | + }} |
| 96 | + """, |
| 97 | + ), |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +def _create_library_test_file(project_dir: Path, name: str) -> None: |
| 102 | + test_file = compose_source_file(project_dir, f"{name}.test.cpp") |
| 103 | + test_file.write_text( |
| 104 | + dedent( |
| 105 | + f"""\ |
| 106 | + #include <gtest/gtest.h> |
| 107 | + #include "{name}/{name}.hpp" |
| 108 | +
|
| 109 | + TEST({name}, api) {{ |
| 110 | + EXPECT_EQ({name}::api(), 42); |
| 111 | + }} |
| 112 | + """, |
| 113 | + ), |
| 114 | + ) |
0 commit comments