Skip to content

CMakePreset Root Writing #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions cppython/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pydantic import create_model

from cppython.schema import API, CMakePresets, ConfigurePreset, ProjectConfiguration
from cppython.utility import write_model_json
from cppython.utility import read_model_json, write_model_json


class ProjectBuilder:
Expand Down Expand Up @@ -111,17 +111,19 @@ def generate_modified(self, original: CPPythonDataT) -> CPPythonDataT:

return modified

def write_presets(self, tool_path: Path, generator_output: list[tuple[str, Path]]) -> None:
def write_presets(self, path: Path, generator_output: list[tuple[str, Path]]) -> None:
"""
Write the cppython presets
"""

def write_generator_presets(tool_path: Path, generator_name: str, toolchain_path: Path) -> Path:
path.mkdir(parents=True, exist_ok=True)

def write_generator_presets(path: Path, generator_name: str, toolchain_path: Path) -> Path:
"""
Write a generator preset.
@returns - The written json file
"""
generator_tool_path = tool_path / generator_name
generator_tool_path = path / generator_name
generator_tool_path.mkdir(parents=True, exist_ok=True)

configure_preset = ConfigurePreset(name=generator_name, hidden=True, toolchainFile=str(toolchain_path))
Expand All @@ -136,24 +138,37 @@ def write_generator_presets(tool_path: Path, generator_name: str, toolchain_path
names = []
includes = []

tool_path = tool_path / "cppython"
path = path / "cppython"

for generator_name, toolchain in generator_output:

preset_file = write_generator_presets(tool_path, generator_name, toolchain)
preset_file = write_generator_presets(path, generator_name, toolchain)

relative_file = preset_file.relative_to(tool_path)
relative_file = preset_file.relative_to(path)

names.append(generator_name)
includes.append(str(relative_file))

configure_preset = ConfigurePreset(name="cppython", hidden=True, inherits=names)
presets = CMakePresets(configurePresets=[configure_preset], include=includes)

json_path = tool_path / "cppython.json"
json_path = path / "cppython.json"

write_model_json(json_path, presets)

# Read the top level json file and replace the include reference

root_preset_path = self.configuration.root_path / "CMakePresets.json"

if root_preset_path.exists():
root_preset = read_model_json(root_preset_path, CMakePresets)

for include_path in root_preset.include:
if Path(include_path).name is "cppython.json":
include_path = json_path

write_model_json(root_preset_path, root_preset)


class Project(API):
"""
Expand Down Expand Up @@ -300,8 +315,7 @@ def install(self) -> None:
self.download_generator_tools()

cppython_logger.info("Installing project")
tool_path = self.cppython.tool_path
tool_path.mkdir(parents=True, exist_ok=True)
preset_path = self.cppython.build_path

generator_output = []

Expand All @@ -316,7 +330,7 @@ def install(self) -> None:
cppython_logger.error(f"Generator {generator.name()} failed to install")
raise exception

self._builder.write_presets(tool_path, generator_output)
self._builder.write_presets(preset_path, generator_output)

def update(self) -> None:
"""
Expand All @@ -331,8 +345,7 @@ def update(self) -> None:

cppython_logger.info("Updating project")

tool_path = self.cppython.tool_path
tool_path.mkdir(parents=True, exist_ok=True)
preset_path = self.cppython.build_path

generator_output = []

Expand All @@ -347,4 +360,4 @@ def update(self) -> None:
cppython_logger.error(f"Generator {generator.name()} failed to update")
raise exception

self._builder.write_presets(tool_path, generator_output)
self._builder.write_presets(preset_path, generator_output)
102 changes: 51 additions & 51 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.