Skip to content

Commit

Permalink
Pass a single GoModuleInfo object to both writers
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Yuan committed Jan 27, 2023
1 parent 8013efd commit dcd8f9b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,15 @@ void execute() {
List<SymbolDependency> dependencies = writers.getDependencies();
writers.flushWriters();

GoModGenerator.writeGoMod(settings, fileManifest, dependencies);
GoModuleInfo goModuleInfo = new GoModuleInfo.Builder()
.goDirective(settings.getGoDirective())
.dependencies(dependencies)
.build();

GoModGenerator.writeGoMod(settings, fileManifest, goModuleInfo);

LOGGER.fine("Generating build manifest file");
ManifestWriter.writeManifest(settings, model, fileManifest, dependencies);
ManifestWriter.writeManifest(settings, model, fileManifest, goModuleInfo);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.SymbolDependency;

/**
* Generates a go.mod file for the project.
Expand All @@ -39,7 +37,7 @@ private GoModGenerator() {}
static void writeGoMod(
GoSettings settings,
FileManifest manifest,
List<SymbolDependency> dependencies
GoModuleInfo goModuleInfo
) {
Boolean generateGoMod = settings.getGenerateGoMod();
if (!generateGoMod) {
Expand All @@ -62,11 +60,6 @@ static void writeGoMod(
manifest.addFile(goModFile);
CodegenUtils.runCommand("go mod init " + settings.getModuleName(), manifest.getBaseDir());

GoModuleInfo goModuleInfo = new GoModuleInfo.Builder()
.goDirective(settings.getGoDirective())
.dependencies(dependencies)
.build();

for (Map.Entry<String, String> dependency : goModuleInfo.getMinimumNonStdLibDependencies().entrySet()) {
CodegenUtils.runCommand(
String.format("go mod edit -require=%s@%s", dependency.getKey(), dependency.getValue()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class GoModuleInfo {

private List<SymbolDependency> stdLibDependencies;
private List<SymbolDependency> nonStdLibDependencies;

private String goDirective;
private Map<String, String> minimumNonStdLibDependencies;

Expand Down Expand Up @@ -103,7 +104,7 @@ private Map<String, String> gatherMinimumNonStdDependencies() {
}

public static class Builder implements SmithyBuilder<GoModuleInfo> {
private String goDirective;
private String goDirective = DEFAULT_GO_DIRECTIVE;
private final BuilderRef<List<SymbolDependency>> dependencies = BuilderRef.forList();

public Builder goDirective(String goDirective) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.SymbolDependency;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.ArrayNode;
import software.amazon.smithy.model.node.BooleanNode;
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.model.traits.UnstableTrait;
import software.amazon.smithy.utils.BuilderRef;
import software.amazon.smithy.utils.SmithyBuilder;

/**
Expand All @@ -50,15 +47,13 @@ public final class ManifestWriter {

private final String moduleName;
private final FileManifest fileManifest;
private final List<SymbolDependency> dependencies;
private final String goDirective;
private final GoModuleInfo goModuleInfo;
private final boolean isUnstable;

private ManifestWriter(Builder builder) {
moduleName = SmithyBuilder.requiredState("moduleName", builder.moduleName);
fileManifest = SmithyBuilder.requiredState("fileManifest", builder.fileManifest);
dependencies = builder.dependencies.copy();
goDirective = SmithyBuilder.requiredState("goDirective", builder.goDirective);
goModuleInfo = SmithyBuilder.requiredState("goModuleInfo", builder.goModuleInfo);
isUnstable = builder.isUnstable;
}

Expand All @@ -68,19 +63,18 @@ private ManifestWriter(Builder builder) {
* @param settings the go settings
* @param model the smithy model
* @param fileManifest the file manifest
* @param dependencies the list of symbol dependencies
* @param goModuleInfo the go module info
*/
public static void writeManifest(
GoSettings settings,
Model model,
FileManifest fileManifest,
List<SymbolDependency> dependencies
GoModuleInfo goModuleInfo
) {
builder()
.moduleName(settings.getModuleName())
.fileManifest(fileManifest)
.dependencies(dependencies)
.goDirective(settings.getGoDirective())
.goModuleInfo(goModuleInfo)
.isUnstable(settings.getService(model).getTrait(UnstableTrait.class).isPresent())
.build()
.writeManifest();
Expand Down Expand Up @@ -110,11 +104,6 @@ public void writeManifest() {
}

private Node buildManifestFile() {
GoModuleInfo goModuleInfo = new GoModuleInfo.Builder()
.goDirective(goDirective)
.dependencies(dependencies)
.build();

Map<StringNode, Node> dependencyNodes = gatherDependencyNodes(goModuleInfo.getMinimumNonStdLibDependencies());
Collection<String> generatedFiles = gatherGeneratedFiles(fileManifest);
return ObjectNode.objectNode(Map.of(
Expand Down Expand Up @@ -151,8 +140,7 @@ public static Builder builder() {
public static class Builder implements SmithyBuilder<ManifestWriter> {
private String moduleName;
private FileManifest fileManifest;
private final BuilderRef<List<SymbolDependency>> dependencies = BuilderRef.forList();
private String goDirective = GoModuleInfo.DEFAULT_GO_DIRECTIVE;
private GoModuleInfo goModuleInfo;
private boolean isUnstable;

public Builder moduleName(String moduleName) {
Expand All @@ -165,14 +153,8 @@ public Builder fileManifest(FileManifest fileManifest) {
return this;
}

public Builder dependencies(List<SymbolDependency> dependencies) {
this.dependencies.clear();
this.dependencies.get().addAll(dependencies);
return this;
}

public Builder goDirective(String goDirective) {
this.goDirective = goDirective;
public Builder goModuleInfo(GoModuleInfo goModuleInfo) {
this.goModuleInfo = goModuleInfo;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package software.amazon.smithy.go.codegen;

import static software.amazon.smithy.go.codegen.GoWriter.goBlockTemplate;
import static software.amazon.smithy.go.codegen.GoWriter.goTemplate;
import static software.amazon.smithy.go.codegen.TestUtils.hasGoInstalled;
import static software.amazon.smithy.go.codegen.TestUtils.makeGoModule;
import static software.amazon.smithy.go.codegen.TestUtils.testGoModule;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.logging.Logger;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -78,10 +76,15 @@ public void testGenerateGoModule() throws Exception {
var dependencies = writers.getDependencies();
writers.flushWriters();

var goModuleInfo = new GoModuleInfo.Builder()
.goDirective(GoModuleInfo.DEFAULT_GO_DIRECTIVE)
.dependencies(dependencies)
.build();

ManifestWriter.builder()
.moduleName("github.com/aws/smithy-go/internal/testmodule")
.fileManifest(fileManifest)
.dependencies(dependencies)
.goModuleInfo(goModuleInfo)
.build()
.writeManifest();

Expand Down

0 comments on commit dcd8f9b

Please sign in to comment.