package org.implab.gradle.common.utils;

import groovy.lang.Closure;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.gradle.api.Action;

@NonNullByDefault
public final class Closures {
    private Closures() {
    }

    /**
     * Wraps {@link Action} around the specified closure. The parameter
     * of the action will be used as delegate in the specified closure.
     *
     * @param <T> The type of the action parameter
     * @param closure The closure
     * @return
     */
    public static <T> Action<T> action(Closure<?> closure) {
        return arg -> apply(closure, arg);
    }

    public static void apply(Closure<?> action, Object target) {
        var c = (Closure<?>)action.clone();
        c.setResolveStrategy(Closure.DELEGATE_FIRST);
        c.setDelegate(target);
        c.call(target);

    }
}
