package org.jenkinsci.plugins.workflow.cps.steps;

import com.google.inject.Inject;
import groovy.lang.Closure;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import hudson.FilePath;
import hudson.model.Action;
import org.jenkinsci.plugins.workflow.cps.CpsStepContext;
import org.jenkinsci.plugins.workflow.cps.CpsThread;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.jenkinsci.plugins.workflow.steps.StepExecution;

import java.util.Collections;

/**
 * Loads another Groovy script file and executes it.
 *
 * @author Kohsuke Kawaguchi
 */
public class LoadStepExecution extends StepExecution {
    @StepContextParameter
    private transient FilePath cwd;

    @Inject
    private transient LoadStep step;

    @Override
    public boolean start() throws Exception {
        CpsStepContext cps = (CpsStepContext) getContext();
        CpsThread t = CpsThread.current();

        GroovyShell shell = CpsThread.current().getExecution().getShell();

        Script script = shell.parse(cwd.child(step.getPath()).readToString());

        // execute body as another thread that shares the same head as this thread
        // as the body can pause.
        cps.invokeBodyLater(
                t.getGroup().export(asCallable(script)),
                cps, // when the body is done, the load step is done
                Collections.<Action>emptyList()
        );

        return false;
    }

    // TODO: this should be another overload of the export method
    private Closure asCallable(final Script script) {
        return new Closure(null) {
            @Override
            public Object call() {
                return script.run();
            }
        };
    }

    @Override
    public void stop() throws Exception {
        // noop
        //
        // the head of the CPS thread that's executing the body should stop and that's all we need to do.
    }
}

@@ -216,16 +216,75 @@
 Action;%0A
+import org.jenkinsci.plugins.workflow.actions.LabelAction;%0A
 import o
@@ -386,16 +386,70 @@
 Thread;%0A
+import org.jenkinsci.plugins.workflow.graph.FlowNode;%0A
 import o
@@ -858,16 +858,80 @@
  step;%0A%0A
+    @StepContextParameter%0A    private transient FlowNode node;%0A%0A
     @Ove
@@ -1238,24 +1238,100 @@
 String());%0A%0A
+        node.addAction(new LabelAction(%22Loaded script: %22+step.getPath()));%0A%0A
         // e

Set a nicer display name