/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.nativeplatform.fixtures;

import org.gradle.integtests.fixtures.AbstractMultiTestRunner;

import java.util.List;

public class SingleToolChainTestRunner extends AbstractMultiTestRunner {
    private static final String TOOLCHAINS_SYSPROP_NAME = "org.gradle.integtest.cpp.toolChains";

    public SingleToolChainTestRunner(Class<? extends AbstractInstalledToolChainIntegrationSpec> target) {
        super(target);
    }

    @Override
    protected void createExecutions() {
        boolean enableAllToolChains = "all".equals(System.getProperty(TOOLCHAINS_SYSPROP_NAME, "default"));
        List<AvailableToolChains.ToolChainCandidate> toolChains = AvailableToolChains.getToolChains();
        if (enableAllToolChains) {
            for (AvailableToolChains.ToolChainCandidate toolChain : toolChains) {
                if (!toolChain.isAvailable()) {
                    throw new RuntimeException(String.format("Tool chain %s is not available.", toolChain.getDisplayName()));
                }
                add(new ToolChainExecution(toolChain, toolChain.meets(getToolChainRequirement(target))));
            }
        } else {
            boolean hasEnabled = false;
            for (AvailableToolChains.ToolChainCandidate toolChain : toolChains) {
                if (!hasEnabled && toolChain.meets(getToolChainRequirement(target))) {
                    add(new ToolChainExecution(toolChain, true));
                    hasEnabled = true;
                } else {
                    add(new ToolChainExecution(toolChain, false));
                }
            }
        }
    }

    private static ToolChainRequirement getToolChainRequirement(Class<?> target) {
        RequiresInstalledToolChain requirements = target.getAnnotation(RequiresInstalledToolChain.class);
        if (requirements == null) {
            return ToolChainRequirement.AVAILABLE;
        }
        return requirements.value();
    }

    private static class ToolChainExecution extends Execution {
        private final AvailableToolChains.ToolChainCandidate toolChain;
        private final boolean enabled;

        public ToolChainExecution(AvailableToolChains.ToolChainCandidate toolChain, boolean enabled) {
            this.toolChain = toolChain;
            this.enabled = enabled;
        }

        @Override
        protected String getDisplayName() {
            return toolChain.getDisplayName();
        }

        @Override
        protected boolean isTestEnabled(TestDetails testDetails) {
            if (enabled) {
                RequiresInstalledToolChain toolChainRestriction = testDetails.getAnnotation(RequiresInstalledToolChain.class);
                return toolChainRestriction == null
                        || toolChain.meets(toolChainRestriction.value());
            }
            return false;
        }

        @Override
        protected void assertCanExecute() {
            assert toolChain.isAvailable() : String.format("Tool chain %s not available", toolChain.getDisplayName());
        }

        @Override
        protected void before() {
            System.out.println(String.format("Using tool chain %s", toolChain.getDisplayName()));
            AbstractInstalledToolChainIntegrationSpec.setToolChain((AvailableToolChains.InstalledToolChain) toolChain);
        }
    }
}

@@ -736,20 +736,120 @@
 va.util.
-List
+EnumSet;%0Aimport java.util.List;%0A%0Aimport static org.gradle.nativeplatform.fixtures.ToolChainRequirement.*
 ;%0A%0Apubli
@@ -1782,55 +1782,45 @@
 in, 
-toolChain.meets(getToolChainRequirement(target)
+isRespectingSwiftConstraint(toolChain
 )));
@@ -2022,45 +2022,62 @@
 ain.
-meets(getToolChainRequirement(target)
+isAvailable() && isRespectingSwiftConstraint(toolChain
 )) %7B
@@ -2339,75 +2339,92 @@
 ate 
-static ToolChainRequirement getToolChainRequirement(Class%3C?%3E target
+boolean isRespectingSwiftConstraint(AvailableToolChains.ToolChainCandidate toolChain
 ) %7B%0A
@@ -2458,17 +2458,26 @@
 olChain 
-r
+toolChainR
 equireme
@@ -2478,17 +2478,16 @@
 uirement
-s
  = targe
@@ -2549,17 +2549,26 @@
     if (
-r
+toolChainR
 equireme
@@ -2569,17 +2569,16 @@
 uirement
-s
  == null
@@ -2604,66 +2604,92 @@
 urn 
-ToolChainRequirement.AVAILABLE;%0A        %7D%0A        return r
+true;%0A        %7D%0A        return EnumSet.of(SWIFT, SWIFT3, SWIFT4).contains(toolChainR
 equi
@@ -2698,17 +2698,16 @@
 ment
-s
 .value()
 ;%0A  
@@ -2702,16 +2702,77 @@
 .value()
+) == toolChain instanceof AvailableToolChains.InstalledSwiftc
 ;%0A    %7D%0A

Revert logic used for selecting Swift compiler