2
2
3
3
from pathlib import Path
4
4
5
- from cppython .plugins .cmake .schema import CMakePresets , CMakeSyncData , ConfigurePreset
5
+ from cppython .plugins .cmake .schema import CMakeData , CMakePresets , CMakeSyncData , ConfigurePreset
6
6
7
7
8
8
class Builder :
@@ -114,7 +114,7 @@ def write_cppython_preset(
114
114
return cppython_preset_file
115
115
116
116
@staticmethod
117
- def generate_root_preset (preset_file : Path , cppython_preset_file : Path ) -> CMakePresets :
117
+ def generate_root_preset (preset_file : Path , cppython_preset_file : Path , cmake_data : CMakeData ) -> CMakePresets :
118
118
"""Generates the top level root preset with the include reference.
119
119
120
120
Args:
@@ -124,18 +124,34 @@ def generate_root_preset(preset_file: Path, cppython_preset_file: Path) -> CMake
124
124
Returns:
125
125
A CMakePresets object
126
126
"""
127
- initial_root_preset = None
127
+ default_configure_preset = ConfigurePreset (
128
+ name = cmake_data .configuration_name ,
129
+ inherits = 'cppython' ,
130
+ )
128
131
129
- # If the file already exists, we need to compare it
130
132
if preset_file .exists ():
131
133
with open (preset_file , encoding = 'utf-8' ) as file :
132
134
initial_json = file .read ()
133
- initial_root_preset = CMakePresets .model_validate_json (initial_json )
134
- root_preset = initial_root_preset .model_copy (deep = True )
135
+ root_preset = CMakePresets .model_validate_json (initial_json )
136
+
137
+ if root_preset .configurePresets is None :
138
+ root_preset .configurePresets = [default_configure_preset ]
139
+
140
+ # Set defaults
141
+ preset = next ((p for p in root_preset .configurePresets if p .name == default_configure_preset .name ), None )
142
+ if preset :
143
+ # If the name matches, we need to verify it inherits from cppython
144
+ if preset .inherits is None :
145
+ preset .inherits = 'cppython'
146
+ elif isinstance (preset .inherits , str ) and preset .inherits != 'cppython' :
147
+ preset .inherits = [preset .inherits , 'cppython' ]
148
+ elif isinstance (preset .inherits , list ) and 'cppython' not in preset .inherits :
149
+ preset .inherits .append ('cppython' )
150
+ else :
151
+ root_preset .configurePresets .append (default_configure_preset )
152
+
135
153
else :
136
154
# If the file doesn't exist, we need to default it for the user
137
- # TODO: Forward the tool's build directory
138
- default_configure_preset = ConfigurePreset (name = 'default' , inherits = 'cppython' , binaryDir = 'build' )
139
155
root_preset = CMakePresets (configurePresets = [default_configure_preset ])
140
156
141
157
# Get the relative path to the cppython preset file
@@ -153,7 +169,7 @@ def generate_root_preset(preset_file: Path, cppython_preset_file: Path) -> CMake
153
169
return root_preset
154
170
155
171
@staticmethod
156
- def write_root_presets (preset_file : Path , cppython_preset_file : Path ) -> None :
172
+ def write_root_presets (preset_file : Path , cppython_preset_file : Path , cmake_data : CMakeData ) -> None :
157
173
"""Read the top level json file and insert the include reference.
158
174
159
175
Receives a relative path to the tool cmake json file
@@ -172,7 +188,7 @@ def write_root_presets(preset_file: Path, cppython_preset_file: Path) -> None:
172
188
initial_json = file .read ()
173
189
initial_root_preset = CMakePresets .model_validate_json (initial_json )
174
190
175
- root_preset = Builder .generate_root_preset (preset_file , cppython_preset_file )
191
+ root_preset = Builder .generate_root_preset (preset_file , cppython_preset_file , cmake_data )
176
192
177
193
# Only write the file if the data has changed
178
194
if root_preset != initial_root_preset :
0 commit comments