Skip to content

Update Root CMakePreset Writes #61

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 1 commit 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
17 changes: 11 additions & 6 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_model_json, write_model_json
from cppython.utility import read_json, read_model_json, write_json, write_model_json


class ProjectBuilder:
Expand Down Expand Up @@ -161,13 +161,18 @@ def write_generator_presets(path: Path, generator_name: str, toolchain_path: Pat
root_preset_path = self.configuration.root_path / "CMakePresets.json"

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

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

write_model_json(root_preset_path, 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

root_preset.update(root_model.dict())

write_json(root_preset_path, root_preset)


class Project(API):
Expand Down
28 changes: 23 additions & 5 deletions cppython/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,43 @@

import json
from pathlib import Path
from typing import Type
from typing import Any, Type

from cppython_core.schema import ModelT
from pydantic import BaseModel


def read_model_json(path: Path, model: Type[ModelT]) -> ModelT:
"""
Reading routine
Reading routine. Only keeps Model data
"""

return model.parse_file(path=path)


def read_json(path: Path) -> Any:
"""
Reading routine
"""

with open(path, "r", encoding="utf-8") as file:
return json.load(file)


def write_model_json(path: Path, model: BaseModel) -> None:
"""
Writing routine
Writing routine. Only writes model data
"""

serialized = json.loads(model.json(exclude_none=True))
with open(path, "w", encoding="utf8") as json_file:
json.dump(serialized, json_file, ensure_ascii=False, indent=2)
with open(path, "w", encoding="utf8") as file:
json.dump(serialized, file, ensure_ascii=False, indent=4)


def write_json(path: Path, data: Any) -> None:
"""
Writing routine
"""

with open(path, "w", encoding="utf-8") as file:
json.dump(data, file, ensure_ascii=False, indent=4)
Empty file added data.json
Empty file.
18 changes: 9 additions & 9 deletions tests/unit/test_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ class TestBuilder:
TODO
"""

def test_model_read_write(self, tmpdir):
class TestModel(BaseModel):
"""
TODO
"""

class TestModel(BaseModel):
"""
TODO
"""
test_path: Path
test_int: int

test_path: Path
test_int: int
def test_model_read_write(self, tmpdir):
"""
TODO
"""

test_model = TestModel(test_path=Path(), test_int=3)
test_model = TestBuilder.TestModel(test_path=Path(), test_int=3)

temporary_directory = Path(tmpdir)

json_path = temporary_directory / "test.json"

write_model_json(json_path, test_model)
output = read_model_json(json_path, TestModel)
output = read_model_json(json_path, TestBuilder.TestModel)

assert test_model == output