/*
 * Copyright 2014 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.tooling.internal.provider.runner;

import org.gradle.BuildAdapter;
import org.gradle.BuildResult;
import org.gradle.api.BuildCancelledException;
import org.gradle.includedbuild.IncludedBuild;
import org.gradle.api.internal.GradleInternal;
import org.gradle.api.invocation.Gradle;
import org.gradle.composite.internal.IncludedBuildInternal;
import org.gradle.execution.ProjectConfigurer;
import org.gradle.internal.invocation.BuildAction;
import org.gradle.internal.invocation.BuildActionRunner;
import org.gradle.internal.invocation.BuildController;
import org.gradle.tooling.internal.protocol.BuildExceptionVersion1;
import org.gradle.tooling.internal.protocol.InternalBuildAction;
import org.gradle.tooling.internal.protocol.InternalBuildActionFailureException;
import org.gradle.tooling.internal.protocol.InternalBuildCancelledException;
import org.gradle.tooling.internal.protocol.InternalBuildController;
import org.gradle.tooling.internal.provider.BuildActionResult;
import org.gradle.tooling.internal.provider.ClientProvidedBuildAction;
import org.gradle.tooling.internal.provider.serialization.PayloadSerializer;

public class ClientProvidedBuildActionRunner implements BuildActionRunner {
    @Override
    public void run(BuildAction action, final BuildController buildController) {
        if (!(action instanceof ClientProvidedBuildAction)) {
            return;
        }

        final GradleInternal gradle = buildController.getGradle();

        ClientProvidedBuildAction clientProvidedBuildAction = (ClientProvidedBuildAction) action;
        PayloadSerializer payloadSerializer = getPayloadSerializer(gradle);
        final InternalBuildAction<?> clientAction = (InternalBuildAction<?>) payloadSerializer.deserialize(clientProvidedBuildAction.getAction());

        final boolean isRunTasks = clientProvidedBuildAction.isRunTasks();

        gradle.addBuildListener(new BuildAdapter() {

            @Override
            public void projectsLoaded(Gradle gradle) {
                forceFullConfiguration((GradleInternal) gradle);
            }

            @Override
            public void buildFinished(BuildResult result) {
                if (result.getFailure() == null) {
                    buildController.setResult(buildResult(clientAction, gradle, isRunTasks));
                }
            }
        });

        if (isRunTasks) {
            buildController.run();
        } else {
            buildController.configure();
        }
    }

    private BuildActionResult buildResult(InternalBuildAction<?> clientAction, GradleInternal gradle, boolean isRunTasks) {
        if (!isRunTasks) {
            forceFullConfiguration(gradle);
        }

        InternalBuildController internalBuildController = new DefaultBuildController(gradle);
        Object model = null;
        Throwable failure = null;
        try {
            model = clientAction.execute(internalBuildController);
        } catch (BuildCancelledException e) {
            failure = new InternalBuildCancelledException(e);
        } catch (RuntimeException e) {
            failure = new InternalBuildActionFailureException(e);
        }

        PayloadSerializer payloadSerializer = getPayloadSerializer(gradle);
        if (failure != null) {
            return new BuildActionResult(null, payloadSerializer.serialize(failure));
        } else {
            return new BuildActionResult(payloadSerializer.serialize(model), null);
        }
    }

    private void forceFullConfiguration(GradleInternal gradle) {
        try {
            gradle.getServices().get(ProjectConfigurer.class).configureHierarchyFully(gradle.getRootProject());
            for (IncludedBuild includedBuild : gradle.getIncludedBuilds()) {
                GradleInternal build = ((IncludedBuildInternal) includedBuild).getConfiguredBuild();
                forceFullConfiguration(build);
            }
        } catch (BuildCancelledException e) {
            throw new InternalBuildCancelledException(e);
        } catch (RuntimeException e) {
            throw new BuildExceptionVersion1(e);
        }
    }

    private PayloadSerializer getPayloadSerializer(GradleInternal gradle) {
        return gradle.getServices().get(PayloadSerializer.class);
    }

}

@@ -778,55 +778,8 @@
 on;%0A
-import org.gradle.includedbuild.IncludedBuild;%0A
 impo
@@ -965,24 +965,71 @@
 Configurer;%0A
+import org.gradle.includedbuild.IncludedBuild;%0A
 import org.g
@@ -2605,32 +2605,71 @@
 radle gradle) %7B%0A
+                if (!isRunTasks) %7B%0A    
                 
@@ -2709,32 +2709,50 @@
 ernal) gradle);%0A
+                %7D%0A
             %7D%0A%0A 
@@ -2965,20 +2965,8 @@
 adle
-, isRunTasks
 ));%0A
@@ -3250,113 +3250,11 @@
 adle
-, boolean isRunTasks) %7B%0A        if (!isRunTasks) %7B%0A            forceFullConfiguration(gradle);%0A        %7D%0A
+) %7B
 %0A   

Remove duplicate method call