4
4
5
5
import logging
6
6
from importlib import metadata
7
+ from pathlib import Path
7
8
from typing import Any , Type , TypeVar
8
9
9
10
from cppython_core .core import cppython_logger
18
19
)
19
20
from pydantic import create_model
20
21
21
- from cppython .schema import API , ProjectConfiguration
22
+ from cppython .schema import API , CMakePresets , ConfigurePreset , ProjectConfiguration
22
23
23
24
24
25
class ProjectBuilder :
@@ -186,6 +187,38 @@ def download(self):
186
187
else :
187
188
cppython_logger .info (f"The { generator .name ()} generator is already downloaded" )
188
189
190
+ def _write_presets_file (self , path : Path , presets : CMakePresets ) -> None :
191
+ """
192
+ Writing routing
193
+ """
194
+
195
+ with open (path / "CMakePresets.json" , "w" , encoding = "utf8" ) as json_file :
196
+ presets .json (json_file ) # type: ignore
197
+
198
+ def _write_generator_presets (self , tool_path : Path , generator : Generator , toolchain_path : Path ) -> Path :
199
+ """
200
+ Write a generator preset.
201
+ @returns - The written directory
202
+ """
203
+ generator_tool_path = tool_path / generator .name ()
204
+ generator_tool_path .mkdir (parents = True , exist_ok = True )
205
+
206
+ configure_preset = ConfigurePreset (name = generator .name (), hidden = True , toolchainFile = toolchain_path )
207
+ presets = CMakePresets (configurePresets = [configure_preset ])
208
+
209
+ self ._write_presets_file (generator_tool_path , presets )
210
+
211
+ return generator_tool_path
212
+
213
+ def _write_presets (self , tool_path : Path , names : list [str ], includes : list [Path ]) -> None :
214
+ """
215
+ Write the cppython main preset
216
+ """
217
+
218
+ configure_preset = ConfigurePreset (name = "cppython" , hidden = True , inherits = names )
219
+ presets = CMakePresets (configurePresets = [configure_preset ], include = includes )
220
+ self ._write_presets_file (tool_path , presets )
221
+
189
222
# API Contract
190
223
def install (self ) -> None :
191
224
"""
@@ -198,9 +231,23 @@ def install(self) -> None:
198
231
cppython_logger .info ("Installing project" )
199
232
self .download ()
200
233
234
+ tool_path = self .pyproject .tool .cppython .tool_path
235
+ tool_path .mkdir (parents = True , exist_ok = True )
236
+
237
+ names = []
238
+ includes = []
239
+
240
+ # TODO: Async
201
241
for generator in self ._generators :
202
242
cppython_logger .info (f"Installing { generator .name ()} generator" )
203
- generator .install ()
243
+
244
+ toolchain_path = generator .install ()
245
+
246
+ directory = self ._write_generator_presets (tool_path , generator , toolchain_path )
247
+ includes .append (directory )
248
+ names .append (generator .name ())
249
+
250
+ self ._write_presets (tool_path , names , includes )
204
251
205
252
def update (self ) -> None :
206
253
"""
@@ -212,6 +259,20 @@ def update(self) -> None:
212
259
213
260
cppython_logger .info ("Updating project" )
214
261
262
+ tool_path = self .pyproject .tool .cppython .tool_path
263
+ tool_path .mkdir (parents = True , exist_ok = True )
264
+
265
+ names = []
266
+ includes = []
267
+
268
+ # TODO: Async
215
269
for generator in self ._generators :
216
270
cppython_logger .info (f"Updating { generator .name ()} generator" )
217
- generator .update ()
271
+
272
+ toolchain_path = generator .update ()
273
+
274
+ directory = self ._write_generator_presets (tool_path , generator , toolchain_path )
275
+ includes .append (directory )
276
+ names .append (generator .name ())
277
+
278
+ self ._write_presets (tool_path , names , includes )
0 commit comments