Skip to content

Commit 2e81928

Browse files
committed
Fix Model Serialization
1 parent 69509fd commit 2e81928

File tree

7 files changed

+28
-19
lines changed

7 files changed

+28
-19
lines changed

cppython/plugins/cmake/builder.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def write_provider_preset(provider_directory: Path, data: CMakeSyncData) -> None
2323

2424
json_path = provider_directory / f'{data.provider_name}.json'
2525

26-
serialized = json.loads(presets.model_dump_json(exclude_none=True, by_alias=False))
26+
serialized = presets.model_dump_json(exclude_none=True, by_alias=False, indent=4)
2727
with open(json_path, 'w', encoding='utf8') as file:
28-
json.dump(serialized, file, ensure_ascii=False, indent=4)
28+
file.write(serialized)
2929

3030
@staticmethod
3131
def write_cppython_preset(
@@ -44,9 +44,9 @@ def write_cppython_preset(
4444

4545
cppython_json_path = cppython_preset_directory / 'cppython.json'
4646

47-
serialized = json.loads(presets.model_dump_json(exclude_none=True, by_alias=False))
47+
serialized = presets.model_dump_json(exclude_none=True, by_alias=False, indent=4)
4848
with open(cppython_json_path, 'w', encoding='utf8') as file:
49-
json.dump(serialized, file, ensure_ascii=False, indent=4)
49+
file.write(serialized)
5050

5151
return cppython_json_path
5252

@@ -63,8 +63,12 @@ def write_root_presets(preset_file: Path, _: Path) -> None:
6363
preset_file: Preset file to modify
6464
"""
6565
with open(preset_file, encoding='utf-8') as file:
66-
initial_root_preset = json.load(file)
66+
initial_json = file.read()
6767

68+
initial_root_preset = CMakePresets.model_validate_json(initial_json)
69+
70+
# Only write the file if the contents have changed
6871
if (root_preset := deepcopy(initial_root_preset)) != initial_root_preset:
6972
with open(preset_file, 'w', encoding='utf-8') as file:
70-
json.dump(root_preset, file, ensure_ascii=False, indent=4)
73+
preset = root_preset.model_dump_json(exclude_none=True, indent=4)
74+
file.write(preset)

cppython/plugins/cmake/resolution.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ def resolve_cmake_data(data: dict[str, Any], core_data: CorePluginData) -> CMake
2828
# If the user hasn't specified a preset file, we need to create one
2929
if not modified_preset_dir.exists():
3030
modified_preset_dir.parent.mkdir(parents=True, exist_ok=True)
31+
presets_string = CMakePresets().model_dump_json(exclude_none=True, indent=4)
32+
3133
with modified_preset_dir.open('w', encoding='utf-8') as file:
32-
presets_dict = CMakePresets().model_dump_json(exclude_none=True)
33-
json.dump(presets_dict, file, ensure_ascii=False, indent=4)
34+
file.write(presets_string)
3435

3536
return CMakeData(preset_file=modified_preset_dir, configuration_name=parsed_data.configuration_name)

cppython/plugins/cmake/schema.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ConfigurePreset(CPPythonModel, extra='allow'):
4747
"""Partial Configure Preset specification to allow cache variable injection"""
4848

4949
name: str
50-
cacheVariables: dict[str, None | bool | str | CacheVariable] | None
50+
cacheVariables: dict[str, None | bool | str | CacheVariable] | None = None
5151

5252

5353
class CMakePresets(CPPythonModel, extra='allow'):
@@ -56,7 +56,10 @@ class CMakePresets(CPPythonModel, extra='allow'):
5656
The only information needed is the configure preset list for cache variable injection
5757
"""
5858

59-
configurePresets: Annotated[list[ConfigurePreset], Field(description='The list of configure presets')] = []
59+
version: Annotated[int, Field(description='The version of the JSON schema.')] = 9
60+
configurePresets: Annotated[list[ConfigurePreset], Field(description='The list of configure presets')] = [
61+
ConfigurePreset(name='default')
62+
]
6063

6164

6265
class CMakeSyncData(SyncData):

cppython/plugins/conan/plugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def sync_data(self, consumer: SyncConsumer) -> SyncData:
9898
if sync_type == CMakeSyncData:
9999
return CMakeSyncData(
100100
provider_name=TypeName('conan'),
101-
top_level_includes=self.core_data.cppython_data.tool_path / 'conan' / 'conan_provider.cmake',
101+
top_level_includes=self.core_data.cppython_data.install_path / 'conan_provider.cmake',
102102
)
103103

104104
raise NotSupportedError('OOF')

cppython/plugins/vcpkg/plugin.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ def install(self) -> None:
193193
manifest = generate_manifest(self.core_data, self.data)
194194

195195
# Write out the manifest
196-
serialized = json.loads(manifest.model_dump_json(exclude_none=True, by_alias=True))
196+
serialized = manifest.model_dump_json(exclude_none=True, by_alias=True, indent=4)
197197
with open(manifest_directory / 'vcpkg.json', 'w', encoding='utf8') as file:
198-
json.dump(serialized, file, ensure_ascii=False, indent=4)
198+
file.write(serialized)
199199

200200
executable = self.core_data.cppython_data.install_path / 'vcpkg'
201201
logger = getLogger('cppython.vcpkg')
@@ -224,9 +224,9 @@ def update(self) -> None:
224224
manifest = generate_manifest(self.core_data, self.data)
225225

226226
# Write out the manifest
227-
serialized = json.loads(manifest.model_dump_json(exclude_none=True, by_alias=True))
227+
serialized = manifest.model_dump_json(exclude_none=True, by_alias=True, indent=4)
228228
with open(manifest_directory / 'vcpkg.json', 'w', encoding='utf8') as file:
229-
json.dump(serialized, file, ensure_ascii=False, indent=4)
229+
file.write(serialized)
230230

231231
executable = self.core_data.cppython_data.install_path / 'vcpkg'
232232
logger = getLogger('cppython.vcpkg')

tests/integration/examples/test_conan_cmake.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
The tests ensure that the projects build, configure, and execute correctly.
55
"""
66

7+
import os
78
import subprocess
89

910
from typer.testing import CliRunner

tests/unit/plugins/cmake/test_generator.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ def test_root_write(tmp_path: Path) -> None:
105105
root_file = tmp_path / 'CMakePresets.json'
106106
presets = CMakePresets()
107107

108-
serialized = json.loads(presets.model_dump_json(exclude_none=True, by_alias=False))
108+
serialized = presets.model_dump_json(exclude_none=True, by_alias=False, indent=4)
109109
with open(root_file, 'w', encoding='utf8') as file:
110-
json.dump(serialized, file, ensure_ascii=False, indent=4)
110+
file.write(serialized)
111111

112112
data = CMakeSyncData(provider_name=TypeName('test-provider'), top_level_includes=includes_file)
113113
builder.write_provider_preset(provider_directory, data)
@@ -140,9 +140,9 @@ def test_relative_root_write(tmp_path: Path) -> None:
140140

141141
root_file = relative_indirection / 'CMakePresets.json'
142142
presets = CMakePresets()
143-
serialized = json.loads(presets.model_dump_json(exclude_none=True, by_alias=False))
143+
serialized = presets.model_dump_json(exclude_none=True, by_alias=False, indent=4)
144144
with open(root_file, 'w', encoding='utf8') as file:
145-
json.dump(serialized, file, ensure_ascii=False, indent=4)
145+
file.write(serialized)
146146

147147
data = CMakeSyncData(provider_name=TypeName('test-provider'), top_level_includes=includes_file)
148148
builder.write_provider_preset(provider_directory, data)

0 commit comments

Comments
 (0)