diff --git a/cppython/project.py b/cppython/project.py index 72411c6..b4bba63 100644 --- a/cppython/project.py +++ b/cppython/project.py @@ -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: @@ -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): diff --git a/cppython/utility.py b/cppython/utility.py index 1df313f..41a72ef 100644 --- a/cppython/utility.py +++ b/cppython/utility.py @@ -4,7 +4,7 @@ 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 @@ -12,17 +12,35 @@ 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) diff --git a/data.json b/data.json new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/test_utility.py b/tests/unit/test_utility.py index b113e84..d873f7e 100644 --- a/tests/unit/test_utility.py +++ b/tests/unit/test_utility.py @@ -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