From 7b208c5e3fe26f04ebfabcd9e1b4852a31dc2069 Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Mon, 7 Oct 2019 11:03:38 -0700 Subject: [PATCH] Add toBuilder to PluginContext Some abstractions might want to use the PluginContext as a way to provide configuration options when building artifacts. This commit makes it easier to modify specific parts of the PluginContext as needed without needing to manually copy properties that haven't changed over. --- .../amazon/smithy/build/PluginContext.java | 16 +++++++++++++- .../smithy/build/PluginContextTest.java | 22 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/smithy-build/src/main/java/software/amazon/smithy/build/PluginContext.java b/smithy-build/src/main/java/software/amazon/smithy/build/PluginContext.java index 4b052595ec8..0c6dcbbd547 100644 --- a/smithy-build/src/main/java/software/amazon/smithy/build/PluginContext.java +++ b/smithy-build/src/main/java/software/amazon/smithy/build/PluginContext.java @@ -34,11 +34,12 @@ import software.amazon.smithy.model.validation.ValidationEvent; import software.amazon.smithy.utils.SetUtils; import software.amazon.smithy.utils.SmithyBuilder; +import software.amazon.smithy.utils.ToSmithyBuilder; /** * Context object used in plugin execution. */ -public final class PluginContext { +public final class PluginContext implements ToSmithyBuilder { private final ProjectionConfig projection; private final String projectionName; private final Model model; @@ -239,6 +240,19 @@ private int findOffsetFromStart(String location) { return position == -1 ? 0 : position + "file:".length(); } + @Override + public Builder toBuilder() { + return builder() + .projection(projectionName, projection) + .model(model) + .originalModel(originalModel) + .events(events) + .settings(settings) + .fileManifest(fileManifest) + .pluginClassLoader(pluginClassLoader) + .sources(sources); + } + /** * Builds a {@link PluginContext}. */ diff --git a/smithy-build/src/test/java/software/amazon/smithy/build/PluginContextTest.java b/smithy-build/src/test/java/software/amazon/smithy/build/PluginContextTest.java index 80f0aaf85f1..610b00818fa 100644 --- a/smithy-build/src/test/java/software/amazon/smithy/build/PluginContextTest.java +++ b/smithy-build/src/test/java/software/amazon/smithy/build/PluginContextTest.java @@ -3,11 +3,13 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; import java.nio.file.Paths; import org.junit.jupiter.api.Test; import software.amazon.smithy.build.model.ProjectionConfig; import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.node.Node; import software.amazon.smithy.model.shapes.ShapeIndex; import software.amazon.smithy.model.transform.ModelTransformer; import software.amazon.smithy.utils.ListUtils; @@ -61,4 +63,24 @@ public void createsNonTraitShapeIndex() { assertThat(context.getNonTraitShapes(), equalTo(scrubbed)); assertThat(context.getNonTraitShapes(), equalTo(scrubbed)); // trigger loading from cache } + + @Test + public void convertsToBuilder() { + PluginContext context = PluginContext.builder() + .projection("foo", ProjectionConfig.builder().build()) + .fileManifest(new MockManifest()) + .model(Model.builder().build()) + .originalModel(Model.builder().build()) + .settings(Node.objectNode().withMember("foo", "bar")) + .build(); + PluginContext context2 = context.toBuilder().build(); + + assertThat(context.getProjectionName(), equalTo(context2.getProjectionName())); + assertThat(context.getProjection(), equalTo(context2.getProjection())); + assertThat(context.getModel(), equalTo(context2.getModel())); + assertThat(context.getOriginalModel(), equalTo(context2.getOriginalModel())); + assertThat(context.getFileManifest(), is(context2.getFileManifest())); + assertThat(context.getSources(), equalTo(context2.getSources())); + assertThat(context.getEvents(), equalTo(context2.getEvents())); + } }