From 54853706d987df163e0a833aaf37ca2364cde5f2 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Thu, 18 Jan 2024 11:31:51 -0500 Subject: [PATCH] Improved `StepExecutions`, available outside test scope --- .../workflow/steps/StepExecutions.java | 134 ++++++++++++++++++ .../workflow/steps/StepExecutions.java | 89 ------------ 2 files changed, 134 insertions(+), 89 deletions(-) create mode 100644 src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecutions.java delete mode 100644 src/test/java/org/jenkinsci/plugins/workflow/steps/StepExecutions.java diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecutions.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecutions.java new file mode 100644 index 00000000..f6d05933 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecutions.java @@ -0,0 +1,134 @@ +/* + * The MIT License + * + * Copyright 2019 CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.jenkinsci.plugins.workflow.steps; + +/** + * Builder for simple {@link StepExecution} implementations. + * Convenient for use from {@link Step#start} when a permanent serial form is unimportant. + * Use {@link StepContext#get} to access contextual objects as usual. + *

The lambda arguments may refer to {@link Step} parameter fields directly. + */ +public class StepExecutions { + + @FunctionalInterface + public interface SynchronousBody { + Object call(StepContext context) throws Exception; + } + + @FunctionalInterface + public interface SynchronousBodyVoid { + void call(StepContext context) throws Exception; + default SynchronousBody asReturn() { + return c -> { + call(c); + return null; + }; + } + } + + /** + * Creates a {@link SynchronousStepExecution} running a given block with a return value. + */ + public static StepExecution synchronous(StepContext context, SynchronousBody body) { + return new SynchronousImpl(context, body); + } + + /** + * Creates a {@link SynchronousStepExecution} running a given block. + */ + public static StepExecution synchronousVoid(StepContext context, SynchronousBodyVoid body) { + return new SynchronousImpl(context, body.asReturn()); + } + + private static class SynchronousImpl extends SynchronousStepExecution { + private static final long serialVersionUID = 1; + private transient final SynchronousBody body; + SynchronousImpl(StepContext context, SynchronousBody body) { + super(context); + this.body = body; + } + @Override protected Object run() throws Exception { + return body.call(getContext()); + } + } + + /** + * Creates a {@link SynchronousNonBlockingStepExecution} running a given block with a return value. + */ + public static StepExecution synchronousNonBlocking(StepContext context, SynchronousBody body) { + return new SynchronousNonBlockingImpl(context, body); + } + + /** + * Creates a {@link SynchronousNonBlockingStepExecution} running a given block. + */ + public static StepExecution synchronousNonBlockingVoid(StepContext context, SynchronousBodyVoid body) { + return new SynchronousNonBlockingImpl(context, body.asReturn()); + } + + private static class SynchronousNonBlockingImpl extends SynchronousNonBlockingStepExecution { + private static final long serialVersionUID = 1; + private transient final SynchronousBody body; + SynchronousNonBlockingImpl(StepContext context, SynchronousBody body) { + super(context); + this.body = body; + } + @Override protected Object run() throws Exception { + return body.call(getContext()); + } + } + + @FunctionalInterface + public interface BlockBody { + void call(StepContext context, BodyInvoker invoker) throws Exception; + } + + /** + * Creates a block-scoped execution allowing various initial actions including {@link BodyInvoker#withContext}. + * There is no pluggable final action, since {@link BodyExecutionCallback#wrap} is used, so it is a simple tail call. + */ + public static StepExecution block(StepContext context, BlockBody body) { + return new BlockImpl(context, body); + } + + private static class BlockImpl extends StepExecution { + private static final long serialVersionUID = 1; + private transient final BlockBody body; + BlockImpl(StepContext context, BlockBody body) { + super(context); + this.body = body; + } + @Override public boolean start() throws Exception { + StepContext context = getContext(); + BodyInvoker invoker = context.newBodyInvoker(); + body.call(context, invoker); + invoker.withCallback(BodyExecutionCallback.wrap(context)).start(); + return false; + } + } + + private StepExecutions() {} + +} diff --git a/src/test/java/org/jenkinsci/plugins/workflow/steps/StepExecutions.java b/src/test/java/org/jenkinsci/plugins/workflow/steps/StepExecutions.java deleted file mode 100644 index d46a5ad4..00000000 --- a/src/test/java/org/jenkinsci/plugins/workflow/steps/StepExecutions.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * The MIT License - * - * Copyright 2019 CloudBees, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package org.jenkinsci.plugins.workflow.steps; - -import java.io.Serializable; - -/** - * Builder for simple {@link StepExecution} implementations. - * Convenient for use from {@link Step#start} during tests, when a permanent serial form is unimportant. - * Use {@link StepContext#get} to access contextual objects as usual. - *

The lambda arguments may be {@link Step} instance method references like {@code this::run} - * so long as the {@link Step} is declared to be {@link Serializable}. - * That allows you to use step parameter fields directly. - */ -public class StepExecutions { - - @FunctionalInterface - public interface SynchronousBody extends Serializable { - Object call(StepContext context) throws Exception; - } - - /** - * Creates a {@link SynchronousStepExecution} running a given block. - */ - public static StepExecution synchronous(StepContext context, SynchronousBody body) { - return new SynchronousStepExecution(context) { - @Override protected Object run() throws Exception { - return body.call(getContext()); - } - }; - } - - /** - * Creates a {@link SynchronousNonBlockingStepExecution} running a given block. - */ - public static StepExecution synchronousNonBlocking(StepContext context, SynchronousBody body) { - return new SynchronousNonBlockingStepExecution(context) { - @Override protected Object run() throws Exception { - return body.call(getContext()); - } - }; - } - - @FunctionalInterface - public interface BlockBody extends Serializable { - void call(StepContext context, BodyInvoker invoker) throws Exception; - } - - /** - * Creates a block-scoped execution allowing various initial actions including {@link BodyInvoker#withContext}. - * There is no pluggable final action, since {@link BodyExecutionCallback#wrap} is used, so it is a simple tail call. - */ - public static StepExecution block(StepContext context, BlockBody body) { - return new StepExecution(context) { - @Override public boolean start() throws Exception { - StepContext context = getContext(); - BodyInvoker invoker = context.newBodyInvoker(); - body.call(context, invoker); - invoker.withCallback(BodyExecutionCallback.wrap(context)).start(); - return false; - } - }; - } - - private StepExecutions() {} - -}