Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup smithy-build #1366

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ public Model getModel() {
* @return Returns true if the projected model is broken.
*/
public boolean isBroken() {
return events.stream().anyMatch(e -> e.getSeverity() == Severity.ERROR || e.getSeverity() == Severity.DANGER);
for (ValidationEvent e : events) {
if (e.getSeverity() == Severity.ERROR || e.getSeverity() == Severity.DANGER) {
return true;
}
}
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -163,8 +158,7 @@ void applyAllProjections(
// The projections are being split up here because we need to be able
// to break out non-parallelizeable plugins. Right now the only
// parallelization that occurs is at the projection level.
List<Callable<Void>> parallelProjections = new ArrayList<>();
List<String> parallelProjectionNameOrder = new ArrayList<>();
List<Runnable> parallelProjections = new ArrayList<>();

for (Map.Entry<String, ProjectionConfig> entry : config.getProjections().entrySet()) {
String name = entry.getKey();
Expand All @@ -184,28 +178,19 @@ void applyAllProjections(
executeSerialProjection(resolvedModel, name, config,
projectionResultConsumer, projectionExceptionConsumer);
} else {
parallelProjectionNameOrder.add(name);
parallelProjections.add(() -> {
executeSerialProjection(resolvedModel, name, config,
projectionResultConsumer, projectionExceptionConsumer);
return null;
});
}
}

// Common case of only executing a single plugin per/projection.
if (parallelProjections.size() == 1) {
try {
parallelProjections.get(0).call();
} catch (Throwable e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e);
}
if (!parallelProjections.isEmpty()) {
if (parallelProjections.size() == 1) {
parallelProjections.get(0).run();
} else {
parallelProjections.parallelStream().forEach(Runnable::run);
}
} else if (!parallelProjections.isEmpty()) {
executeParallelProjections(parallelProjections, parallelProjectionNameOrder, projectionExceptionConsumer);
}
}

Expand All @@ -231,31 +216,6 @@ private void executeSerialProjection(
}
}

private void executeParallelProjections(
List<Callable<Void>> parallelProjections,
List<String> parallelProjectionNameOrder,
BiConsumer<String, Throwable> projectionExceptionConsumer
) {
ExecutorService executor = ForkJoinPool.commonPool();

try {
List<Future<Void>> futures = executor.invokeAll(parallelProjections);
// Futures are returned in the same order they were added, so
// use the list of ordered names to know which projections failed.
for (int i = 0; i < futures.size(); i++) {
try {
futures.get(i).get();
} catch (ExecutionException e) {
Throwable cause = e.getCause() != null ? e.getCause() : e;
String failedProjectionName = parallelProjectionNameOrder.get(i);
projectionExceptionConsumer.accept(failedProjectionName, cause);
}
}
} catch (InterruptedException e) {
throw new SmithyBuildException(e.getMessage(), e);
}
}

private ValidatedResult<Model> createBaseModel() {
if (!config.getImports().isEmpty()) {
LOGGER.fine(() -> "Merging the following imports into the loaded model: " + config.getImports());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,14 @@ static Function<String, Optional<SmithyBuildPlugin>> createServiceFactory(Iterab
// caching a ServiceLoader using a Thread's context ClassLoader VM-wide.
List<SmithyBuildPlugin> pluginList = new ArrayList<>();
plugins.forEach(pluginList::add);
return name -> pluginList.stream()
.filter(plugin -> plugin.getName().equals(name))
.findFirst();
return name -> {
for (SmithyBuildPlugin plugin : pluginList) {
if (plugin.getName().equals(name)) {
return Optional.of(plugin);
}
}
return Optional.empty();
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.SmithyBuilder;
Expand Down Expand Up @@ -52,7 +51,12 @@ public static Builder builder() {
* @return Returns true if any are broken.
*/
public boolean anyBroken() {
return results.stream().anyMatch(ProjectionResult::isBroken);
for (ProjectionResult result : results) {
if (result.isBroken()) {
return true;
}
}
return false;
}

/**
Expand Down Expand Up @@ -80,7 +84,12 @@ public Stream<FileManifest> allManifests() {
* @return Returns the optionally found result.
*/
public Optional<ProjectionResult> getProjectionResult(String projectionName) {
return results.stream().filter(result -> result.getProjectionName().equals(projectionName)).findFirst();
for (ProjectionResult result : results) {
if (result.getProjectionName().equals(projectionName)) {
return Optional.of(result);
}
}
return Optional.empty();
}

/**
Expand All @@ -99,7 +108,11 @@ public List<ProjectionResult> getProjectionResults() {
* @return Returns the projection results as a map.
*/
public Map<String, ProjectionResult> getProjectionResultsMap() {
return results.stream().collect(Collectors.toMap(ProjectionResult::getProjectionName, Function.identity()));
Map<String, ProjectionResult> resultMap = new HashMap<>();
for (ProjectionResult result : results) {
resultMap.put(result.getProjectionName(), result);
}
return Collections.unmodifiableMap(resultMap);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

package software.amazon.smithy.build.model;

import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import software.amazon.smithy.build.SmithyBuildException;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.utils.BuilderRef;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.ToSmithyBuilder;
Expand Down Expand Up @@ -60,10 +60,15 @@ public Builder toBuilder() {
}

public static ProjectionConfig fromNode(Node node) {
return fromNode(node, SmithyBuildUtils.getBasePathFromSourceLocation(node));
}

static ProjectionConfig fromNode(Node node, Path basePath) {
Builder builder = ProjectionConfig.builder();
node.expectObjectNode()
.getBooleanMember("abstract", builder::setAbstract)
.getArrayMember("imports", StringNode::getValue, builder::imports)
.getArrayMember("imports", s -> SmithyBuildUtils.resolveImportPath(basePath, s),
builder::imports)
.getArrayMember("transforms", TransformConfig::fromNode, builder::transforms)
.getObjectMember("plugins", plugins -> {
for (Map.Entry<String, Node> entry : plugins.getStringMap().entrySet()) {
Expand Down Expand Up @@ -103,8 +108,6 @@ public List<String> getImports() {
return imports;
}



/**
* Builds a {@link ProjectionConfig}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2022 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.
Expand All @@ -16,16 +16,19 @@
package software.amazon.smithy.build.model;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import software.amazon.smithy.build.SmithyBuildException;
import software.amazon.smithy.model.loader.ModelSyntaxException;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.utils.BuilderRef;
import software.amazon.smithy.utils.IoUtils;
import software.amazon.smithy.utils.SetUtils;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.ToSmithyBuilder;
Expand Down Expand Up @@ -55,23 +58,10 @@ private SmithyBuildConfig(Builder builder) {
}

public static SmithyBuildConfig fromNode(Node node) {
Builder builder = builder();
node.expectObjectNode()
.expectStringMember("version", builder::version)
.getStringMember("outputDirectory", builder::outputDirectory)
.getArrayMember("imports", StringNode::getValue, builder::imports)
.getObjectMember("projections", v -> {
for (Map.Entry<String, Node> entry : v.getStringMap().entrySet()) {
builder.projections.get().put(entry.getKey(), ProjectionConfig.fromNode(entry.getValue()));
}
})
.getObjectMember("plugins", v -> {
for (Map.Entry<String, Node> entry : v.getStringMap().entrySet()) {
builder.plugins.get().put(entry.getKey(), entry.getValue().expectObjectNode());
}
})
.getBooleanMember("ignoreMissingPlugins", builder::ignoreMissingPlugins);
return builder.build();
Path path = SmithyBuildUtils.getBasePathFromSourceLocation(node);
// Expand variables before deserializing the node into the builder.
ObjectNode expanded = SmithyBuildUtils.expandNode(node);
return builder().loadNode(path, expanded).build();
}

/**
Expand All @@ -85,7 +75,6 @@ public static Builder builder() {
* Loads a SmithyBuildConfig from a JSON file on disk.
*
* <p>The file is expected to contain the following structure:
*
* <code>
* {
* "version": "1.0",
Expand Down Expand Up @@ -232,7 +221,38 @@ public Builder version(String version) {
* @return Returns the updated builder.
*/
public Builder load(Path config) {
return merge(ConfigLoader.load(config));
try {
String content = IoUtils.readUtf8File(config);
Path basePath = config.getParent();
if (basePath == null) {
basePath = Paths.get(".");
}
Node loadedAndExpanded = SmithyBuildUtils.loadAndExpandJson(config.toString(), content);
return loadNode(basePath, loadedAndExpanded);
} catch (ModelSyntaxException e) {
throw new SmithyBuildException(e);
}
}

private Builder loadNode(Path basePath, Node node) {
node.expectObjectNode()
.expectStringMember("version", this::version)
.getStringMember("outputDirectory", this::outputDirectory)
.getArrayMember("imports", s -> SmithyBuildUtils.resolveImportPath(basePath, s),
values -> imports.get().addAll(values))
.getObjectMember("projections", v -> {
for (Map.Entry<String, Node> entry : v.getStringMap().entrySet()) {
projections.get().put(entry.getKey(), ProjectionConfig
.fromNode(entry.getValue(), basePath));
}
})
.getObjectMember("plugins", v -> {
for (Map.Entry<String, Node> entry : v.getStringMap().entrySet()) {
plugins.get().put(entry.getKey(), entry.getValue().expectObjectNode());
}
})
.getBooleanMember("ignoreMissingPlugins", this::ignoreMissingPlugins);
return this;
}

/**
Expand Down
Loading