Skip to content

Read/Write Modifications #62

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 22, 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
37 changes: 21 additions & 16 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 read_json, read_model_json, write_json, write_model_json
from cppython.utility import read_json, write_json, write_model_json


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

return modified

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

path.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -155,24 +156,26 @@ def write_generator_presets(path: Path, generator_name: str, toolchain_path: Pat
json_path = path / "cppython.json"

write_model_json(json_path, presets)
return json_path

# Read the top level json file and replace the include reference
def write_root_presets(self, path: Path):
"""
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_json(root_preset_path)

root_model = CMakePresets.parse_obj(root_preset)
root_preset = read_json(root_preset_path)
root_model = CMakePresets.parse_obj(root_preset)

if root_model.include is not None:
for include_path in root_model.include:
if Path(include_path).name == "cppython.json":
include_path = json_path
if root_model.include is not None:
for include_path in root_model.include:
if Path(include_path).name == "cppython.json":
include_path = path

root_preset.update(root_model.dict())
root_preset.update(root_model.dict(exclude_none=True))

write_json(root_preset_path, root_preset)
write_json(root_preset_path, root_preset)


class Project(API):
Expand Down Expand Up @@ -335,7 +338,8 @@ def install(self) -> None:
cppython_logger.error(f"Generator {generator.name()} failed to install")
raise exception

self._builder.write_presets(preset_path, generator_output)
project_presets = self._builder.write_presets(preset_path, generator_output)
self._builder.write_root_presets(project_presets)

def update(self) -> None:
"""
Expand Down Expand Up @@ -365,4 +369,5 @@ def update(self) -> None:
cppython_logger.error(f"Generator {generator.name()} failed to update")
raise exception

self._builder.write_presets(preset_path, generator_output)
project_presets = self._builder.write_presets(preset_path, generator_output)
self._builder.write_root_presets(project_presets)
20 changes: 17 additions & 3 deletions tests/unit/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ def test_presets(self, tmpdir):
TODO
"""

configuration = ProjectConfiguration(root_path=Path())
builder = ProjectBuilder(configuration)

temporary_directory = Path(tmpdir)
configuration = ProjectConfiguration(root_path=temporary_directory)
builder = ProjectBuilder(configuration)

input_toolchain = temporary_directory / "input.cmake"

Expand All @@ -140,3 +139,18 @@ def test_presets(self, tmpdir):

test_file = test_tool / "test.json"
assert test_file.exists()

def test_root_presets(self, tmpdir):
"""
TODO
"""

temporary_directory = Path(tmpdir)
configuration = ProjectConfiguration(root_path=temporary_directory)
builder = ProjectBuilder(configuration)

input_preset = temporary_directory / "CMakePresets.json"
with open(input_preset, "w", encoding="utf8") as file:
file.write("{}")

builder.write_root_presets(temporary_directory / "test_location")