|
| 1 | +"""Construction of Conan data""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from string import Template |
| 5 | + |
| 6 | +from pydantic import DirectoryPath |
| 7 | + |
| 8 | +from cppython.plugins.conan.schema import ConanDependency |
| 9 | + |
| 10 | + |
| 11 | +class Builder: |
| 12 | + """Aids in building the information needed for the Conan plugin""" |
| 13 | + |
| 14 | + def __init__(self) -> None: |
| 15 | + """Initialize the builder""" |
| 16 | + self._filename = 'conanfile.py' |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def _create_conanfile(conan_file: Path, dependencies: list[ConanDependency]) -> None: |
| 20 | + """Creates a conanfile.py file with the necessary content.""" |
| 21 | + template_string = """ |
| 22 | + from conan import ConanFile |
| 23 | + from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout |
| 24 | +
|
| 25 | + class MyProject(ConanFile): |
| 26 | + name = myproject |
| 27 | + version = "1.0" |
| 28 | + settings = "os", "compiler", "build_type", "arch" |
| 29 | + requires = "zlib/1.2.13" |
| 30 | + generators = "CMakeDeps" |
| 31 | +
|
| 32 | + def layout(self): |
| 33 | + cmake_layout(self) |
| 34 | +
|
| 35 | + def generate(self): |
| 36 | + tc = CMakeToolchain(self) |
| 37 | + tc.generate() |
| 38 | +
|
| 39 | + def build(self): |
| 40 | + cmake = CMake(self) |
| 41 | + cmake.configure() |
| 42 | + cmake.build()""" |
| 43 | + |
| 44 | + template = Template(template_string) |
| 45 | + |
| 46 | + values = { |
| 47 | + 'dependencies': [dependency.requires() for dependency in dependencies], |
| 48 | + } |
| 49 | + |
| 50 | + result = template.substitute(values) |
| 51 | + |
| 52 | + with open(conan_file, 'w', encoding='utf-8') as file: |
| 53 | + file.write(result) |
| 54 | + |
| 55 | + def generate_conanfile(self, directory: DirectoryPath, dependencies: list[ConanDependency]) -> None: |
| 56 | + """Generate a conanfile.py file for the project.""" |
| 57 | + conan_file = directory / self._filename |
| 58 | + |
| 59 | + # If the file exists then we need to inject our information into it |
| 60 | + if conan_file.exists(): |
| 61 | + with open(conan_file, encoding='utf-8') as file: |
| 62 | + file_contents = file.read() |
| 63 | + |
| 64 | + else: |
| 65 | + directory.mkdir(parents=True, exist_ok=True) |
| 66 | + self._create_conanfile(conan_file, dependencies) |
0 commit comments