Skip to content

Commit 926ebff

Browse files
committed
Project Writes CMakePresets
1 parent 9d1fa31 commit 926ebff

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

cppython/project.py

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
from importlib import metadata
7+
from pathlib import Path
78
from typing import Any, Type, TypeVar
89

910
from cppython_core.core import cppython_logger
@@ -18,7 +19,7 @@
1819
)
1920
from pydantic import create_model
2021

21-
from cppython.schema import API, ProjectConfiguration
22+
from cppython.schema import API, CMakePresets, ConfigurePreset, ProjectConfiguration
2223

2324

2425
class ProjectBuilder:
@@ -186,6 +187,38 @@ def download(self):
186187
else:
187188
cppython_logger.info(f"The {generator.name()} generator is already downloaded")
188189

190+
def _write_presets_file(self, path: Path, presets: CMakePresets) -> None:
191+
"""
192+
Writing routing
193+
"""
194+
195+
with open(path / "CMakePresets.json", "w", encoding="utf8") as json_file:
196+
presets.json(json_file) # type: ignore
197+
198+
def _write_generator_presets(self, tool_path: Path, generator: Generator, toolchain_path: Path) -> Path:
199+
"""
200+
Write a generator preset.
201+
@returns - The written directory
202+
"""
203+
generator_tool_path = tool_path / generator.name()
204+
generator_tool_path.mkdir(parents=True, exist_ok=True)
205+
206+
configure_preset = ConfigurePreset(name=generator.name(), hidden=True, toolchainFile=toolchain_path)
207+
presets = CMakePresets(configurePresets=[configure_preset])
208+
209+
self._write_presets_file(generator_tool_path, presets)
210+
211+
return generator_tool_path
212+
213+
def _write_presets(self, tool_path: Path, names: list[str], includes: list[Path]) -> None:
214+
"""
215+
Write the cppython main preset
216+
"""
217+
218+
configure_preset = ConfigurePreset(name="cppython", hidden=True, inherits=names)
219+
presets = CMakePresets(configurePresets=[configure_preset], include=includes)
220+
self._write_presets_file(tool_path, presets)
221+
189222
# API Contract
190223
def install(self) -> None:
191224
"""
@@ -198,9 +231,23 @@ def install(self) -> None:
198231
cppython_logger.info("Installing project")
199232
self.download()
200233

234+
tool_path = self.pyproject.tool.cppython.tool_path
235+
tool_path.mkdir(parents=True, exist_ok=True)
236+
237+
names = []
238+
includes = []
239+
240+
# TODO: Async
201241
for generator in self._generators:
202242
cppython_logger.info(f"Installing {generator.name()} generator")
203-
generator.install()
243+
244+
toolchain_path = generator.install()
245+
246+
directory = self._write_generator_presets(tool_path, generator, toolchain_path)
247+
includes.append(directory)
248+
names.append(generator.name())
249+
250+
self._write_presets(tool_path, names, includes)
204251

205252
def update(self) -> None:
206253
"""
@@ -212,6 +259,20 @@ def update(self) -> None:
212259

213260
cppython_logger.info("Updating project")
214261

262+
tool_path = self.pyproject.tool.cppython.tool_path
263+
tool_path.mkdir(parents=True, exist_ok=True)
264+
265+
names = []
266+
includes = []
267+
268+
# TODO: Async
215269
for generator in self._generators:
216270
cppython_logger.info(f"Updating {generator.name()} generator")
217-
generator.update()
271+
272+
toolchain_path = generator.update()
273+
274+
directory = self._write_generator_presets(tool_path, generator, toolchain_path)
275+
includes.append(directory)
276+
names.append(generator.name())
277+
278+
self._write_presets(tool_path, names, includes)

0 commit comments

Comments
 (0)