@@ -191,16 +191,13 @@ def generate_single_gyb_file(
191
191
# `sources_dir/gyb_generated`.
192
192
def generate_gyb_files_helper (
193
193
sources_dir : str ,
194
- destination_dir : Optional [ str ] ,
194
+ destination_dir : str ,
195
195
gyb_exec : str ,
196
196
add_source_locations : bool ,
197
197
verbose : bool ,
198
198
) -> None :
199
199
temp_files_dir = tempfile .gettempdir ()
200
200
make_dir_if_needed (temp_files_dir )
201
-
202
- if destination_dir is None :
203
- destination_dir = os .path .join (sources_dir , "gyb_generated" )
204
201
make_dir_if_needed (destination_dir )
205
202
206
203
# Clear any *.swift files that are relics from the previous run.
@@ -284,35 +281,48 @@ def generate_syntax_node_template_gyb_files(
284
281
verbose = verbose ,
285
282
)
286
283
287
- # Gyb-files that should be generated before running code generation using SwiftSyntaxBuilder.
284
+
285
+ # If `temp_directories` is True, creates a dictionary that maps every source dir in
286
+ # `source_dirs` to a unique temporary directory.
287
+ # If `temp_directories` is False, it maps each source dir to the corresponding
288
+ # gyb_generated directory.
289
+ def gyb_dir_mapping (temp_directories : bool , source_dirs : List [str ]) -> Dict [str , str ]:
290
+ mapping = {}
291
+ for source_dir in source_dirs :
292
+ if temp_directories :
293
+ mapping [source_dir ] = tempfile .mkdtemp ()
294
+ else :
295
+ mapping [source_dir ] = os .path .join (source_dir , "gyb_generated" )
296
+ return mapping
297
+
298
+
299
+ # Gyb-files that should be generated before running generate-swift-syntax-builder.
288
300
# Maps directories containing .gyb files to the directories the generated files should
289
301
# live in.
290
302
def first_stage_gyb_dir_mapping (
291
- generateswiftsyntaxbuilder_destination : Optional [ str ] = None ,
292
- ) -> Dict [str , Optional [ str ] ]:
293
- return {
294
- GENERATESWIFTSYNTAXBUILDER_DIR : generateswiftsyntaxbuilder_destination ,
295
- }
303
+ temp_directories : bool
304
+ ) -> Dict [str , str ]:
305
+ return gyb_dir_mapping ( temp_directories = temp_directories , source_dirs = [
306
+ GENERATESWIFTSYNTAXBUILDER_DIR
307
+ ])
296
308
297
- # Gyb-files that should be generated after running code generation using SwiftSyntaxBuilder.
309
+
310
+ # Gyb-files that should be generated after running generate-swift-syntax-builder.
298
311
# Maps directories containing .gyb files to the directories the generated files should
299
312
# live in.
300
313
def second_stage_gyb_dir_mapping (
301
- swiftsyntax_destination : Optional [str ] = None ,
302
- swiftsyntax_raw_destination : Optional [str ] = None ,
303
- swiftsyntaxbuilder_destination : Optional [str ] = None ,
304
- swiftsyntaxparser_destination : Optional [str ] = None ,
305
- ) -> Dict [str , Optional [str ]]:
306
- return {
307
- SWIFTSYNTAX_DIR : swiftsyntax_destination ,
308
- os .path .join (SWIFTSYNTAX_DIR , "Raw" ): swiftsyntax_raw_destination ,
309
- SWIFTSYNTAXBUILDER_DIR : swiftsyntaxbuilder_destination ,
310
- SWIFTSYNTAXPARSER_DIR : swiftsyntaxparser_destination ,
311
- }
314
+ temp_directories : bool
315
+ ) -> Dict [str , str ]:
316
+ return gyb_dir_mapping (temp_directories = temp_directories , source_dirs = [
317
+ SWIFTSYNTAX_DIR ,
318
+ os .path .join (SWIFTSYNTAX_DIR , "Raw" ),
319
+ SWIFTSYNTAXBUILDER_DIR ,
320
+ SWIFTSYNTAXPARSER_DIR ,
321
+ ])
312
322
313
323
314
324
def generate_gyb_files (
315
- gyb_exec : str , gyb_dir_mapping : Dict [str , Optional [ str ] ],
325
+ gyb_exec : str , gyb_dir_mapping : Dict [str , str ],
316
326
add_source_locations : bool , verbose : bool ,
317
327
) -> None :
318
328
print ("** Generating gyb Files **" )
@@ -463,13 +473,10 @@ def build(self, product_name: str) -> None:
463
473
464
474
def verify_gyb_generated_files (gyb_exec : str , verbose : bool ) -> None :
465
475
first_stage_gyb_dirs = first_stage_gyb_dir_mapping (
466
- generateswiftsyntaxbuilder_destination = tempfile . mkdtemp (),
476
+ temp_directories = True
467
477
)
468
478
second_stage_gyb_dirs = second_stage_gyb_dir_mapping (
469
- swiftsyntax_destination = tempfile .mkdtemp (),
470
- swiftsyntax_raw_destination = tempfile .mkdtemp (),
471
- swiftsyntaxbuilder_destination = tempfile .mkdtemp (),
472
- swiftsyntaxparser_destination = tempfile .mkdtemp (),
479
+ temp_directories = True
473
480
)
474
481
gyb_dirs = first_stage_gyb_dirs
475
482
gyb_dirs .update (second_stage_gyb_dirs )
@@ -697,7 +704,7 @@ def generate_source_code_command(args: argparse.Namespace) -> None:
697
704
try :
698
705
generate_gyb_files (
699
706
args .gyb_exec ,
700
- gyb_dir_mapping = first_stage_gyb_dir_mapping (),
707
+ gyb_dir_mapping = first_stage_gyb_dir_mapping (temp_directories = False ),
701
708
add_source_locations = args .add_source_locations ,
702
709
verbose = args .verbose ,
703
710
)
@@ -722,7 +729,7 @@ def generate_source_code_command(args: argparse.Namespace) -> None:
722
729
try :
723
730
generate_gyb_files (
724
731
args .gyb_exec ,
725
- gyb_dir_mapping = second_stage_gyb_dir_mapping (),
732
+ gyb_dir_mapping = second_stage_gyb_dir_mapping (temp_directories = False ),
726
733
add_source_locations = args .add_source_locations ,
727
734
verbose = args .verbose ,
728
735
)
0 commit comments