package org.implab.gradle.common.dsl;

import java.util.HashMap;
import java.util.Map;

import org.gradle.api.Action;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.implab.gradle.common.utils.Closures;
import org.implab.gradle.common.utils.Values;

import groovy.lang.Closure;

/**
 * Configuration properties of the execution shell. This object specifies a
 * working directory and environment variables for the processes started within
 * this shell.
 */
public interface TaskEnvSpecMixin {

    /** Inherit environment from current process */
    Property<Boolean> getInheritEnvironment();

    /** Working directory */
    DirectoryProperty getWorkingDirectory();

    /** Environment variables */
    MapProperty<String, String> getEnvironment();

    /**
     * Configures the environment variable using the specified action. The
     * action is called when the value is calculated;
     *
     * <p>
     * The configuration action is called immediately. To support lazy evaluation,
     * properties may be assigned to providers.
     */
    default void env(Action<Map<String, ?>> configure) {
        Values.configureMap(getEnvironment(), configure, Object.class, Values::toString);
    }

    default void env(Closure<?> configure) {
        env(Closures.action(configure));
    }
}
