def main()

in scripts/check_for_xcodebuild_warnings.py [0:0]


def main():
    base_dir = git_base_dir() if is_git_dir() else generic_base_dir()
    os.chdir(base_dir)

    xcodebuild_command = " ".join(
        [
            "xcodebuild clean build-for-testing",
            "-workspace FacebookSDK.xcworkspace",
            "-scheme BuildAllKits-Dynamic",
            "-destination 'platform=iOS Simulator,name=iPhone 13'",
        ]
    )

    completed_process = subprocess.run(
        xcodebuild_command, shell=True, check=False, capture_output=True
    )

    output_lines = completed_process.stdout.decode().splitlines()

    warning_lines = {
        line for line in output_lines if "warning: " in line or "error: " in line
    }

    # Make the output prettier by removing the base_dir from the warning file paths
    warning_lines = [line.replace(f"{base_dir}/", "") for line in warning_lines]

    non_allowlisted_warnings = []

    for warning in warning_lines:
        can_ignore = any(
            allowed_warning_text in warning
            for allowed_warning_text in XCODEBUILD_WARNINGS_ALLOWLIST
        )
        if not can_ignore:
            non_allowlisted_warnings.append(warning)

    # If there are warnings print an issue and exit
    if non_allowlisted_warnings:
        warning_count = len(non_allowlisted_warnings)
        warning_word = "WARNINGS" if warning_count > 1 else "WARNING"

        print_to_stderr(
            f"\nFAILED DUE TO THE FOLLOWING {warning_count} NON-ALLOWLISTED {warning_word}:"
        )
        for i, warning in enumerate(non_allowlisted_warnings, start=1):
            print_to_stderr(f"{i}. {warning}")

        print_to_stderr(
            "If any of these warnings should be ALLOWLISTED, add them to xcodebuild_warnings_allowlist.py"
        )

        sys.exit(1)

    if completed_process.returncode != 0:
        print(f"Failed to run xcodebuild. Return code: {completed_process.returncode}")
        print(f"STDERR: {completed_process.stderr.decode()}")
        sys.exit(completed_process.returncode)

    print("Check complete. No unexpected warnings encountered.")
    sys.exit(0)