def dispatch(root_path,
             repo,
             action,
             swiftc,
             swift_version,
             sandbox_profile_xcodebuild,
             sandbox_profile_package,
             added_swift_flags,
             should_strip_resource_phases=False,
             stdout=sys.stdout,
             stderr=sys.stderr,
             incremental=False):
    """Call functions corresponding to actions."""

    if action['action'] == 'BuildSwiftPackage':
        return build_swift_package(os.path.join(root_path, repo['path']),
                                   swiftc,
                                   action['configuration'],
                                   sandbox_profile_package,
                                   stdout=stdout,
                                   stderr=stderr,
                                   added_swift_flags=added_swift_flags,
                                   incremental=incremental)
    elif action['action'] == 'TestSwiftPackage':
        return test_swift_package(os.path.join(root_path, repo['path']),
                                  swiftc,
                                  sandbox_profile_package,
                                  stdout=stdout,
                                  stderr=stderr,
                                  added_swift_flags=added_swift_flags,
                                  incremental=incremental)
    elif re.match(r'^(Build|Test)Xcode(Workspace|Project)(Scheme|Target)$',
                  action['action']):
        match = re.match(
            r'^(Build|Test)Xcode(Workspace|Project)(Scheme|Target)$',
            action['action'])

        build_settings = {'SWIFT_EXEC': swiftc}

        if 'configuration' in action:
            build_settings['CONFIGURATION'] = action['configuration']

        other_swift_flags = []
        if swift_version:
            other_swift_flags += ['-swift-version', swift_version]
            build_settings['SWIFT_VERSION'] = swift_version
        if added_swift_flags:
            other_swift_flags.append(added_swift_flags)
        if other_swift_flags:
            other_swift_flags = ['$(OTHER_SWIFT_FLAGS)'] + other_swift_flags
            build_settings['OTHER_SWIFT_FLAGS'] = ' '.join(other_swift_flags)

        is_workspace = match.group(2).lower() == 'workspace'
        project_path = os.path.join(root_path, repo['path'],
                                    action[match.group(2).lower()])
        has_scheme = match.group(3).lower() == 'scheme'
        xcode_target = \
            XcodeTarget(project_path,
                        action[match.group(3).lower()],
                        action['destination'],
                        get_sdk_platform_path(action['destination'],
                                              stdout=stdout, stderr=stderr),
                        build_settings,
                        is_workspace,
                        has_scheme)
        if should_strip_resource_phases:
            strip_resource_phases(os.path.join(root_path, repo['path']),
                                  stdout=stdout,
                                  stderr=stderr)
        if match.group(1) == 'Build':
            return xcode_target.build(sandbox_profile_xcodebuild,
                                      stdout=stdout,
                                      stderr=stderr,
                                      incremental=incremental)
        else:
            return xcode_target.test(sandbox_profile_xcodebuild,
                                     stdout=stdout,
                                     stderr=stderr,
                                     incremental=incremental)
    else:
        raise common.Unimplemented("Unknown action: %s" % action['action'])
def dispatch(root_path,
             repo,
             action,
             swiftc,
             swift_version,
             sandbox_profile_xcodebuild,
             sandbox_profile_package,
             added_swift_flags,
             build_config,
             should_strip_resource_phases=False,
             stdout=sys.stdout,
             stderr=sys.stderr,
             incremental=False):
    """Call functions corresponding to actions."""

    if added_swift_flags:
        # Support added swift flags specific to the current repository and
        # action by passing their fields as keyword arguments to format, e.g.
        # so that {path} in '-index-store-path /tmp/index/{path}' is replaced
        # with the value of repo's path field.
        substitutions = action.copy()
        substitutions.update(repo)
        added_swift_flags = added_swift_flags.format(**substitutions)

    if action['action'] == 'BuildSwiftPackage':
        if not build_config:
            build_config = action['configuration']
        return build_swift_package(os.path.join(root_path, repo['path']),
                                   swiftc,
                                   build_config,
                                   sandbox_profile_package,
                                   stdout=stdout,
                                   stderr=stderr,
                                   added_swift_flags=added_swift_flags,
                                   incremental=incremental)
    elif action['action'] == 'TestSwiftPackage':
        return test_swift_package(os.path.join(root_path, repo['path']),
                                  swiftc,
                                  sandbox_profile_package,
                                  stdout=stdout,
                                  stderr=stderr,
                                  added_swift_flags=added_swift_flags,
                                  incremental=incremental)
    elif re.match(r'^(Build|Test)Xcode(Workspace|Project)(Scheme|Target)$',
                  action['action']):
        match = re.match(
            r'^(Build|Test)Xcode(Workspace|Project)(Scheme|Target)$',
            action['action'])

        build_settings = {'SWIFT_EXEC': swiftc}

        if build_config == 'debug':
            build_settings['CONFIGURATION'] = 'Debug'
        elif build_config == 'release':
            build_settings['CONFIGURATION'] = 'Release'
        elif 'configuration' in action:
            build_settings['CONFIGURATION'] = action['configuration']

        other_swift_flags = []
        if swift_version:
            other_swift_flags += ['-swift-version', swift_version]
            build_settings['SWIFT_VERSION'] = swift_version
        if added_swift_flags:
            other_swift_flags.append(added_swift_flags)
        if other_swift_flags:
            other_swift_flags = ['$(OTHER_SWIFT_FLAGS)'] + other_swift_flags
            build_settings['OTHER_SWIFT_FLAGS'] = ' '.join(other_swift_flags)

        is_workspace = match.group(2).lower() == 'workspace'
        project_path = os.path.join(root_path, repo['path'],
                                    action[match.group(2).lower()])
        has_scheme = match.group(3).lower() == 'scheme'
        xcode_target = \
            XcodeTarget(project_path,
                        action[match.group(3).lower()],
                        action['destination'],
                        build_settings,
                        is_workspace,
                        has_scheme)
        if should_strip_resource_phases:
            strip_resource_phases(os.path.join(root_path, repo['path']),
                                  stdout=stdout,
                                  stderr=stderr)
        if match.group(1) == 'Build':
            return xcode_target.build(sandbox_profile_xcodebuild,
                                      stdout=stdout,
                                      stderr=stderr,
                                      incremental=incremental)
        else:
            return xcode_target.test(sandbox_profile_xcodebuild,
                                     stdout=stdout,
                                     stderr=stderr,
                                     incremental=incremental)
    else:
        raise common.Unimplemented("Unknown action: %s" % action['action'])