3
3
import argparse
4
4
import os
5
5
import platform
6
+ import re
6
7
import subprocess
7
8
import sys
8
9
import tempfile
@@ -341,10 +342,15 @@ def generate_gyb_files(
341
342
print ("** Done Generating gyb Files **" )
342
343
343
344
345
+ # If swiftsyntax_checkout is specified, a SwiftSyntax checkout with the commit that's
346
+ # used for CodeGeneration is assumed at that location, otherwise SwiftSyntax will be
347
+ # pulled at the correct commit from the internet. This is useful to verify files
348
+ # generated using CodeGeneration in CI that doesn't have internet access.
344
349
def run_code_generation (
345
350
swiftideutils_destination : str ,
346
351
swiftbasicformat_destination : str ,
347
352
swiftsyntaxbuilder_destination : str ,
353
+ local_swiftsyntax_checkout : Optional [str ],
348
354
toolchain : str ,
349
355
verbose : bool
350
356
) -> None :
@@ -370,6 +376,9 @@ def run_code_generation(
370
376
371
377
env = dict (os .environ )
372
378
env ["SWIFT_BUILD_SCRIPT_ENVIRONMENT" ] = "1"
379
+ if local_swiftsyntax_checkout :
380
+ env ["SWIFTCI_USE_LOCAL_DEPS" ] = "1"
381
+ env ["SWIFTCI_SWIFTSYNTAX_PATH" ] = local_swiftsyntax_checkout
373
382
check_call (swiftpm_call , env = env , verbose = verbose )
374
383
375
384
@@ -505,6 +514,20 @@ def verify_gyb_generated_files(gyb_exec: str, verbose: bool) -> None:
505
514
)
506
515
507
516
517
+ # Parses the package manifest of CodeGeneration to find the SwiftSyntax commit it
518
+ # references.
519
+ def get_swiftsyntax_commit_for_codegeneration () -> str :
520
+ with open (os .path .join (CODE_GENERATION_DIR , 'Package.swift' )) as \
521
+ codegen_package_manifest :
522
+ for line in codegen_package_manifest :
523
+ match = re .search (r'.package\(url: "https://github.com/apple/swift-syntax.git", revision: "([0-9a-f]+)"\)' , line ) # noqa: E501
524
+ if match :
525
+ return match .group (1 )
526
+
527
+ fatal_error ('Could not find commit of SwiftSyntax that CodeGeneration references' )
528
+ return '' # Make Python's type checker happy
529
+
530
+
508
531
def verify_code_generated_files (
509
532
toolchain : str , verbose : bool
510
533
) -> None :
@@ -522,19 +545,29 @@ def verify_code_generated_files(
522
545
self_swiftbasicformat_generated_dir = tempfile .mkdtemp ()
523
546
self_swiftsyntaxbuilder_generated_dir = tempfile .mkdtemp ()
524
547
525
- try :
526
- run_code_generation (
527
- swiftideutils_destination = self_swiftideutils_generated_dir ,
528
- swiftbasicformat_destination = self_swiftsyntaxbuilder_generated_dir ,
529
- swiftsyntaxbuilder_destination = self_swiftsyntaxbuilder_generated_dir ,
530
- toolchain = toolchain ,
531
- verbose = verbose
532
- )
533
- except subprocess .CalledProcessError as e :
534
- fail_for_called_process_error (
535
- "Source generation using SwiftSyntaxBuilder failed" ,
536
- e
537
- )
548
+ with tempfile .TemporaryDirectory (dir = os .path .dirname (PACKAGE_DIR )) as \
549
+ local_swiftsyntax_checkout :
550
+ # Perform a local clone of SwiftSyntax that we can check out at the commit that
551
+ # CodeGeneration expects.
552
+ check_call (['git' , 'clone' , PACKAGE_DIR , local_swiftsyntax_checkout ],
553
+ verbose = verbose )
554
+ check_call (['git' , 'checkout' , get_swiftsyntax_commit_for_codegeneration ()],
555
+ cwd = local_swiftsyntax_checkout , verbose = verbose )
556
+
557
+ try :
558
+ run_code_generation (
559
+ swiftideutils_destination = self_swiftideutils_generated_dir ,
560
+ swiftbasicformat_destination = self_swiftbasicformat_generated_dir ,
561
+ swiftsyntaxbuilder_destination = self_swiftsyntaxbuilder_generated_dir ,
562
+ local_swiftsyntax_checkout = local_swiftsyntax_checkout ,
563
+ toolchain = toolchain ,
564
+ verbose = verbose
565
+ )
566
+ except subprocess .CalledProcessError as e :
567
+ fail_for_called_process_error (
568
+ "Source generation using SwiftSyntaxBuilder failed" ,
569
+ e
570
+ )
538
571
539
572
print ("** Verifing code generated files **" )
540
573
@@ -729,6 +762,7 @@ def generate_source_code_command(args: argparse.Namespace) -> None:
729
762
swiftideutils_destination = swiftideutils_destination ,
730
763
swiftbasicformat_destination = swiftbasicformat_destination ,
731
764
swiftsyntaxbuilder_destination = swiftsyntaxbuilder_destination ,
765
+ local_swiftsyntax_checkout = None ,
732
766
toolchain = args .toolchain ,
733
767
verbose = args .verbose
734
768
)
0 commit comments