Skip to content

Commit 03be811

Browse files
authored
Merge pull request #149 from jglick/StepExecutions
Improved `StepExecutions`, available outside test scope
2 parents 6e098b8 + 5485370 commit 03be811

File tree

2 files changed

+134
-89
lines changed

2 files changed

+134
-89
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2019 CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.jenkinsci.plugins.workflow.steps;
26+
27+
/**
28+
* Builder for simple {@link StepExecution} implementations.
29+
* Convenient for use from {@link Step#start} when a permanent serial form is unimportant.
30+
* Use {@link StepContext#get} to access contextual objects as usual.
31+
* <p>The lambda arguments may refer to {@link Step} parameter fields directly.
32+
*/
33+
public class StepExecutions {
34+
35+
@FunctionalInterface
36+
public interface SynchronousBody {
37+
Object call(StepContext context) throws Exception;
38+
}
39+
40+
@FunctionalInterface
41+
public interface SynchronousBodyVoid {
42+
void call(StepContext context) throws Exception;
43+
default SynchronousBody asReturn() {
44+
return c -> {
45+
call(c);
46+
return null;
47+
};
48+
}
49+
}
50+
51+
/**
52+
* Creates a {@link SynchronousStepExecution} running a given block with a return value.
53+
*/
54+
public static StepExecution synchronous(StepContext context, SynchronousBody body) {
55+
return new SynchronousImpl(context, body);
56+
}
57+
58+
/**
59+
* Creates a {@link SynchronousStepExecution} running a given block.
60+
*/
61+
public static StepExecution synchronousVoid(StepContext context, SynchronousBodyVoid body) {
62+
return new SynchronousImpl(context, body.asReturn());
63+
}
64+
65+
private static class SynchronousImpl extends SynchronousStepExecution<Object> {
66+
private static final long serialVersionUID = 1;
67+
private transient final SynchronousBody body;
68+
SynchronousImpl(StepContext context, SynchronousBody body) {
69+
super(context);
70+
this.body = body;
71+
}
72+
@Override protected Object run() throws Exception {
73+
return body.call(getContext());
74+
}
75+
}
76+
77+
/**
78+
* Creates a {@link SynchronousNonBlockingStepExecution} running a given block with a return value.
79+
*/
80+
public static StepExecution synchronousNonBlocking(StepContext context, SynchronousBody body) {
81+
return new SynchronousNonBlockingImpl(context, body);
82+
}
83+
84+
/**
85+
* Creates a {@link SynchronousNonBlockingStepExecution} running a given block.
86+
*/
87+
public static StepExecution synchronousNonBlockingVoid(StepContext context, SynchronousBodyVoid body) {
88+
return new SynchronousNonBlockingImpl(context, body.asReturn());
89+
}
90+
91+
private static class SynchronousNonBlockingImpl extends SynchronousNonBlockingStepExecution<Object> {
92+
private static final long serialVersionUID = 1;
93+
private transient final SynchronousBody body;
94+
SynchronousNonBlockingImpl(StepContext context, SynchronousBody body) {
95+
super(context);
96+
this.body = body;
97+
}
98+
@Override protected Object run() throws Exception {
99+
return body.call(getContext());
100+
}
101+
}
102+
103+
@FunctionalInterface
104+
public interface BlockBody {
105+
void call(StepContext context, BodyInvoker invoker) throws Exception;
106+
}
107+
108+
/**
109+
* Creates a block-scoped execution allowing various initial actions including {@link BodyInvoker#withContext}.
110+
* There is no pluggable final action, since {@link BodyExecutionCallback#wrap} is used, so it is a simple tail call.
111+
*/
112+
public static StepExecution block(StepContext context, BlockBody body) {
113+
return new BlockImpl(context, body);
114+
}
115+
116+
private static class BlockImpl extends StepExecution {
117+
private static final long serialVersionUID = 1;
118+
private transient final BlockBody body;
119+
BlockImpl(StepContext context, BlockBody body) {
120+
super(context);
121+
this.body = body;
122+
}
123+
@Override public boolean start() throws Exception {
124+
StepContext context = getContext();
125+
BodyInvoker invoker = context.newBodyInvoker();
126+
body.call(context, invoker);
127+
invoker.withCallback(BodyExecutionCallback.wrap(context)).start();
128+
return false;
129+
}
130+
}
131+
132+
private StepExecutions() {}
133+
134+
}

src/test/java/org/jenkinsci/plugins/workflow/steps/StepExecutions.java

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)