All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dangernoodle.grt.internal.WorkflowExecutor Maven / Gradle / Ivy

package io.dangernoodle.grt.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.dangernoodle.grt.Repository;
import io.dangernoodle.grt.Workflow;


public class WorkflowExecutor
{
    private static final Logger logger = LoggerFactory.getLogger(WorkflowExecutor.class);

    private final Collection prePost;

    private final Map workflows;

    public WorkflowExecutor(Collection workflows, Collection prePost)
    {
        this.prePost = prePost;
        this.workflows = workflows.stream()
                                  .collect(Collectors.toMap(workflow -> workflow.getName(), Function.identity()));
    }

    public void execute(Repository repository, Map args) throws Exception
    {
        Workflow.Context context = new Workflow.Context(args);
        Collection steps = getSteps(repository.getWorkflow());

        for (String step : steps)
        {
            if (!workflows.containsKey(step))
            {
                logger.warn("unable to find Workflow instance for step [{}]", step);
                continue;
            }

            workflows.get(step)
                     .execute(repository, context);
        }
    }

    public void postExecution() throws Exception
    {
        for (Workflow.PrePost toExecute : prePost)
        {
            toExecute.postExecution();
        }
    }

    public void preExecution() throws Exception
    {
        for (Workflow.PrePost toExecute : prePost)
        {
            toExecute.preExecution();
        }
    }

    private Collection getSteps(Collection workflow)
    {
        ArrayList steps = new ArrayList<>();

        if (workflow != null)
        {
            steps.addAll(workflow);
        }

        if (workflow == null || !steps.contains("github"))
        {
            steps.add(0, "github");
        }

        return steps;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy