22
33from pathlib import Path
44
5- from cppython .plugins .cmake .schema import CMakePresets , CMakeSyncData , ConfigurePreset
5+ from cppython .plugins .cmake .schema import CMakeData , CMakePresets , CMakeSyncData , ConfigurePreset
66
77
88class Builder :
@@ -114,7 +114,7 @@ def write_cppython_preset(
114114 return cppython_preset_file
115115
116116 @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 :
118118 """Generates the top level root preset with the include reference.
119119
120120 Args:
@@ -124,18 +124,34 @@ def generate_root_preset(preset_file: Path, cppython_preset_file: Path) -> CMake
124124 Returns:
125125 A CMakePresets object
126126 """
127- initial_root_preset = None
127+ default_configure_preset = ConfigurePreset (
128+ name = cmake_data .configuration_name ,
129+ inherits = 'cppython' ,
130+ )
128131
129- # If the file already exists, we need to compare it
130132 if preset_file .exists ():
131133 with open (preset_file , encoding = 'utf-8' ) as file :
132134 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+
135153 else :
136154 # 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' )
139155 root_preset = CMakePresets (configurePresets = [default_configure_preset ])
140156
141157 # 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
153169 return root_preset
154170
155171 @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 :
157173 """Read the top level json file and insert the include reference.
158174
159175 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:
172188 initial_json = file .read ()
173189 initial_root_preset = CMakePresets .model_validate_json (initial_json )
174190
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 )
176192
177193 # Only write the file if the data has changed
178194 if root_preset != initial_root_preset :
0 commit comments