package com.github.blindpirate.gogradle.task;

import groovy.lang.Closure;
import org.codehaus.groovy.runtime.InvokerInvocationException;
import org.gradle.api.Task;
import org.gradle.api.internal.tasks.ContextAwareTaskAction;
import org.gradle.api.internal.tasks.TaskExecutionContext;

import java.util.Map;

public class GoExecutionAction implements ContextAwareTaskAction {
    private Closure closure;
    private Map<String, String> env;

    public static GoExecutionAction wrapClousureWithEnvs(Closure closure, Map<String, String> env) {
        GoExecutionAction ret = new GoExecutionAction();
        ret.env = env;
        ret.closure = closure;
        return ret;
    }

    private GoExecutionAction() {
    }

    @Override
    public void contextualise(TaskExecutionContext context) {
    }

    @Override
    public void execute(Task task) {
        closure.setDelegate(task);
        closure.setResolveStrategy(Closure.DELEGATE_FIRST);
        ClassLoader original = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(closure.getClass().getClassLoader());
        Map<String, String> originalEnv = Go.class.cast(task).getCurrentEnv();
        Go.class.cast(task).setCurrentEnv(env);
        try {
            synchronized (task) {
                if (closure.getMaximumNumberOfParameters() == 0) {
                    closure.call();
                } else {
                    closure.call(task);
                }
            }
        } catch (InvokerInvocationException e) {
            Throwable cause = e.getCause();
            if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            }
            throw e;
        } finally {
            Thread.currentThread().setContextClassLoader(original);
            Go.class.cast(task).setCurrentEnv(originalEnv);
        }
    }

    @Override
    public ClassLoader getClassLoader() {
        return closure.getClass().getClassLoader();
    }

}

@@ -72,71 +72,8 @@
 re;%0A
-import org.codehaus.groovy.runtime.InvokerInvocationException;%0A
 impo
@@ -1432,237 +1432,8 @@
   %7D%0A
-        %7D catch (InvokerInvocationException e) %7B%0A            Throwable cause = e.getCause();%0A            if (cause instanceof RuntimeException) %7B%0A                throw (RuntimeException) cause;%0A            %7D%0A            throw e;%0A
     

Remove redundant exception catching