
software.amazon.smithy.build.plugins.BuildInfoPlugin Maven / Gradle / Ivy
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.smithy.build.plugins;
import java.util.List;
import java.util.stream.Collectors;
import software.amazon.smithy.build.PluginContext;
import software.amazon.smithy.build.SmithyBuildPlugin;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.NodeMapper;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ResourceShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.TraitDefinition;
/**
* Writes a build info file for each projection. This file contains the
* projection name, the filters, mappers, and validation events encountered
* while building the model.
*
* This plugin is invoked regardless of whether or not the model contains
* error or unsuppressed validation events.
*
*
This plugin is only invoked if the projection and original model are
* configured on the provided PluginContext.
*/
public final class BuildInfoPlugin implements SmithyBuildPlugin {
private static final String NAME = "build-info";
@Override
public String getName() {
return NAME;
}
@Override
public boolean requiresValidModel() {
return false;
}
@Override
public void execute(PluginContext context) {
if (context.getOriginalModel().isPresent() && context.getProjection().isPresent()) {
context.getFileManifest().writeJson("smithy-build-info.json", serializeBuildInfo(context));
}
}
private static Node serializeBuildInfo(PluginContext context) {
BuildInfo info = new BuildInfo();
info.setProjectionName(context.getProjectionName());
info.setProjection(context.getProjection().orElse(null));
info.setValidationEvents(context.getEvents());
info.setTraitNames(findTraitNames(context.getModel()));
info.setTraitDefNames(context.getModel().getShapesWithTrait(TraitDefinition.class).stream()
.map(Shape::getId)
.sorted()
.collect(Collectors.toList()));
info.setServiceShapeIds(findShapeIds(context.getModel(), ServiceShape.class));
info.setOperationShapeIds(findShapeIds(context.getModel(), OperationShape.class));
info.setResourceShapeIds(findShapeIds(context.getModel(), ResourceShape.class));
info.setMetadata(context.getModel().getMetadata());
NodeMapper mapper = new NodeMapper();
return mapper.serialize(info);
}
private static List findTraitNames(Model model) {
return model.shapes()
.flatMap(shape -> shape.getAllTraits().keySet().stream())
.distinct()
.sorted()
.collect(Collectors.toList());
}
private static List findShapeIds(Model model, Class clazz) {
return model.shapes(clazz)
.map(Shape::getId)
.sorted()
.collect(Collectors.toList());
}
}