Skip to content

Commit

Permalink
fix formatting of our own java code (#3641)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb6 authored May 17, 2024
1 parent 9659f80 commit 59eb861
Show file tree
Hide file tree
Showing 22 changed files with 405 additions and 386 deletions.
17 changes: 7 additions & 10 deletions generators/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ buildscript {
gradlePluginPortal()
mavenCentral()
}

dependencies {
classpath 'com.palantir.javaformat:gradle-palantir-java-format:2.20.0'
}
}

plugins {
Expand All @@ -21,13 +17,18 @@ group 'io.github.fern-api'

apply plugin: 'com.palantir.consistent-versions'

allprojects {
spotless {
java {
target '*/src/main/java/**/*.java', '*/src/test/java/**/*.java'
palantirJavaFormat('2.46.0').formatJavadoc(true)
}
}

allprojects {
apply plugin: "org.inferred.processors"
apply plugin: "maven-publish"
apply plugin: "com.jfrog.artifactory"
apply plugin: "java-library"
apply plugin: 'com.palantir.java-format'

version rootProject.version
group rootProject.group
Expand All @@ -50,8 +51,4 @@ allprojects {
}

sourceCompatibility = 11

java {
palantirJavaFormat {}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
package com.fern.java;

import com.fern.irV42.core.ObjectMappers;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.fern.java.output.GeneratedGradleProperties;
import com.fern.java.output.GeneratedPublishScript;
import org.immutables.value.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fern.generator.exec.model.config.GeneratorConfig;
import com.fern.generator.exec.model.config.GeneratorPublishConfig;
import com.fern.generator.exec.model.config.GithubOutputMode;
Expand All @@ -33,19 +13,36 @@
import com.fern.generator.exec.model.logging.MavenCoordinate;
import com.fern.generator.exec.model.logging.PackageCoordinate;
import com.fern.generator.exec.model.logging.SuccessfulStatusUpdate;
import com.fern.irV42.core.ObjectMappers;
import com.fern.irV42.model.ir.IntermediateRepresentation;
import com.fern.java.MavenCoordinateParser.MavenArtifactAndGroup;
import com.fern.java.generators.GithubWorkflowGenerator;
import com.fern.java.immutables.StagedBuilderImmutablesStyle;
import com.fern.java.jackson.ClientObjectMappers;
import com.fern.java.output.GeneratedBuildGradle;
import com.fern.java.output.GeneratedFile;
import com.fern.java.output.GeneratedGradleProperties;
import com.fern.java.output.GeneratedPublishScript;
import com.fern.java.output.ImmutableGeneratedBuildGradle;
import com.fern.java.output.RawGeneratedFile;
import com.fern.java.output.gradle.AbstractGradleDependency;
import com.fern.java.output.gradle.GradlePlugin;
import com.fern.java.output.gradle.GradlePublishingConfig;
import com.fern.java.output.gradle.GradleRepository;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.immutables.value.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractGeneratorCli<T extends ICustomConfig, K extends DownloadFilesCustomConfig> {

Expand Down Expand Up @@ -112,8 +109,7 @@ private static Process runCommandAsync(String[] command, Path workingDirectory,

private Path outputDirectory = null;

protected AbstractGeneratorCli() {
}
protected AbstractGeneratorCli() {}

public final void run(String... args) {
String pluginPath = args[0];
Expand Down Expand Up @@ -150,8 +146,8 @@ public Void visitUnknown(String unknownType) {
throw new RuntimeException("Encountered unknown output mode: " + unknownType);
}
});
generatorExecClient.sendUpdate(GeneratorUpdate
.exitStatusUpdate(ExitStatusUpdate.successful(SuccessfulStatusUpdate.builder().build())));
generatorExecClient.sendUpdate(GeneratorUpdate.exitStatusUpdate(
ExitStatusUpdate.successful(SuccessfulStatusUpdate.builder().build())));
} catch (Exception e) {
log.error("Encountered fatal error", e);
generatorExecClient.sendUpdate(GeneratorUpdate.exitStatusUpdate(ExitStatusUpdate.error(
Expand Down Expand Up @@ -185,8 +181,8 @@ public final void runInGithubMode(
Optional<MavenCoordinate> maybeMavenCoordinate = githubOutputMode
.getPublishInfo()
.flatMap(githubPublishInfo -> githubPublishInfo.getMaven().map(mavenGithubPublishInfo -> {
MavenArtifactAndGroup mavenArtifactAndGroup = MavenCoordinateParser
.parse(mavenGithubPublishInfo.getCoordinate());
MavenArtifactAndGroup mavenArtifactAndGroup =
MavenCoordinateParser.parse(mavenGithubPublishInfo.getCoordinate());
return MavenCoordinate.builder()
.group(mavenArtifactAndGroup.group())
.artifact(mavenArtifactAndGroup.artifact())
Expand All @@ -195,9 +191,8 @@ public final void runInGithubMode(
}));
runInGithubModeHook(generatorExecClient, generatorConfig, ir, customConfig, githubOutputMode);

Optional<MavenGithubPublishInfo> mavenGithubPublishInfo = githubOutputMode.getPublishInfo()
.flatMap(githubPublishInfo -> githubPublishInfo
.getMaven());
Optional<MavenGithubPublishInfo> mavenGithubPublishInfo =
githubOutputMode.getPublishInfo().flatMap(githubPublishInfo -> githubPublishInfo.getMaven());
Boolean addSignatureBlock = mavenGithubPublishInfo.isPresent()
&& mavenGithubPublishInfo.get().getSignature().isPresent();
// add project level files
Expand All @@ -206,12 +201,9 @@ public final void runInGithubMode(
mavenGithubPublishInfo.map(MavenGithubPublishInfo::getRegistryUrl),
mavenGithubPublishInfo.flatMap(MavenGithubPublishInfo::getSignature)));
// write files to disk
generatedFiles.forEach(generatedFile -> generatedFile.write(outputDirectory,
false, Optional.empty()));
runCommandBlocking(new String[] { "gradle", "wrapper" }, outputDirectory,
Collections.emptyMap());
runCommandBlocking(new String[] { "gradle", "spotlessApply" },
outputDirectory, Collections.emptyMap());
generatedFiles.forEach(generatedFile -> generatedFile.write(outputDirectory, false, Optional.empty()));
runCommandBlocking(new String[] {"gradle", "wrapper"}, outputDirectory, Collections.emptyMap());
runCommandBlocking(new String[] {"gradle", "spotlessApply"}, outputDirectory, Collections.emptyMap());
}

public abstract void runInGithubModeHook(
Expand All @@ -228,9 +220,10 @@ public final void runInPublishMode(
T customConfig,
GeneratorPublishConfig publishOutputMode) {
// send publishing update
MavenRegistryConfigV2 mavenRegistryConfigV2 = publishOutputMode.getRegistriesV2().getMaven();
MavenArtifactAndGroup mavenArtifactAndGroup = MavenCoordinateParser
.parse(mavenRegistryConfigV2.getCoordinate());
MavenRegistryConfigV2 mavenRegistryConfigV2 =
publishOutputMode.getRegistriesV2().getMaven();
MavenArtifactAndGroup mavenArtifactAndGroup =
MavenCoordinateParser.parse(mavenRegistryConfigV2.getCoordinate());
MavenCoordinate mavenCoordinate = MavenCoordinate.builder()
.group(mavenArtifactAndGroup.group())
.artifact(mavenArtifactAndGroup.artifact())
Expand All @@ -241,11 +234,15 @@ public final void runInPublishMode(
runInPublishModeHook(generatorExecClient, generatorConfig, ir, customConfig, publishOutputMode);

Boolean addSignatureBlock = mavenRegistryConfigV2.getSignature().isPresent();
addRootProjectFiles(Optional.of(mavenCoordinate), false, mavenRegistryConfigV2.getSignature().isPresent(), generatorConfig);
addRootProjectFiles(
Optional.of(mavenCoordinate),
false,
mavenRegistryConfigV2.getSignature().isPresent(),
generatorConfig);

generatedFiles.forEach(generatedFile -> generatedFile.write(outputDirectory, false, Optional.empty()));
runCommandBlocking(new String[] { "gradle", "wrapper" }, outputDirectory, Collections.emptyMap());
runCommandBlocking(new String[] { "gradle", "spotlessApply" }, outputDirectory, Collections.emptyMap());
runCommandBlocking(new String[] {"gradle", "wrapper"}, outputDirectory, Collections.emptyMap());
runCommandBlocking(new String[] {"gradle", "spotlessApply"}, outputDirectory, Collections.emptyMap());

// run publish
if (!generatorConfig.getDryRun()) {
Expand All @@ -257,18 +254,18 @@ public final void runInPublishMode(
GeneratedBuildGradle.MAVEN_PUBLISH_REGISTRY_URL_ENV_VAR,
mavenRegistryConfigV2.getRegistryUrl()));
if (addSignatureBlock) {
MavenCentralSignature signature = mavenRegistryConfigV2.getSignature().get();
MavenCentralSignature signature =
mavenRegistryConfigV2.getSignature().get();
publishEnvVars.put(GeneratedBuildGradle.MAVEN_SIGNING_KEY_ID, signature.getKeyId());
publishEnvVars.put(GeneratedBuildGradle.MAVEN_SIGNING_PASSWORD, signature.getPassword());
publishEnvVars.put(GeneratedBuildGradle.MAVEN_SIGNING_KEY, signature.getSecretKey());
runCommandBlocking(
new String[] { "./.publish/prepare.sh" },
new String[] {"./.publish/prepare.sh"},
Paths.get(generatorConfig.getOutput().getPath()),
publishEnvVars);

}
runCommandBlocking(
new String[] { "gradle", "publish" },
new String[] {"gradle", "publish"},
Paths.get(generatorConfig.getOutput().getPath()),
publishEnvVars);
}
Expand All @@ -295,9 +292,14 @@ protected final void addGeneratedFile(GeneratedFile generatedFile) {
generatedFiles.add(generatedFile);
}

private void addRootProjectFiles(Optional<MavenCoordinate> maybeMavenCoordinate, boolean addTestBlock,
boolean addSignaturePlugin, GeneratorConfig generatorConfig) {
String repositoryUrl = addSignaturePlugin ? "https://oss.sonatype.org/service/local/staging/deploy/maven2/" : "https://s01.oss.sonatype.org/content/repositories/releases/";
private void addRootProjectFiles(
Optional<MavenCoordinate> maybeMavenCoordinate,
boolean addTestBlock,
boolean addSignaturePlugin,
GeneratorConfig generatorConfig) {
String repositoryUrl = addSignaturePlugin
? "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
: "https://s01.oss.sonatype.org/content/repositories/releases/";

ImmutableGeneratedBuildGradle.Builder buildGradle = GeneratedBuildGradle.builder()
.addAllPlugins(List.of(
Expand All @@ -311,9 +313,8 @@ private void addRootProjectFiles(Optional<MavenCoordinate> maybeMavenCoordinate,
.pluginId("com.diffplug.spotless")
.version("6.11.0")
.build()))
.addCustomRepositories(GradleRepository.builder()
.url(repositoryUrl)
.build())
.addCustomRepositories(
GradleRepository.builder().url(repositoryUrl).build())
.gradlePublishingConfig(maybeMavenCoordinate.map(mavenCoordinate -> GradlePublishingConfig.builder()
.version(mavenCoordinate.getVersion())
.group(mavenCoordinate.getGroup())
Expand All @@ -325,12 +326,8 @@ private void addRootProjectFiles(Optional<MavenCoordinate> maybeMavenCoordinate,
.addCustomBlocks("spotless {\n" + " java {\n" + " palantirJavaFormat()\n" + " }\n" + "}\n")
.addCustomBlocks("java {\n" + " withSourcesJar()\n" + " withJavadocJar()\n" + "}\n");
if (addSignaturePlugin) {
buildGradle.addPlugins(GradlePlugin.builder()
.pluginId("signing")
.build());
buildGradle.addCustomBlocks("signing {\n"
+ " sign(publishing.publications)\n"
+ "}");
buildGradle.addPlugins(GradlePlugin.builder().pluginId("signing").build());
buildGradle.addCustomBlocks("signing {\n" + " sign(publishing.publications)\n" + "}");
// Generate an empty gradle.properties file
addGeneratedFile(GeneratedGradleProperties.getGeneratedFile());
// Generate script to populate that file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.fern.java.generators;

import java.util.Optional;

import com.fern.generator.exec.model.config.MavenCentralSignatureGithubInfo;
import com.fern.java.output.GeneratedBuildGradle;
import com.fern.java.output.RawGeneratedFile;
import java.util.Optional;

public final class GithubWorkflowGenerator {

public static RawGeneratedFile getGithubWorkflow(Optional<String> registryUrl,
Optional<MavenCentralSignatureGithubInfo> signatureGithubInfo) {
public static RawGeneratedFile getGithubWorkflow(
Optional<String> registryUrl, Optional<MavenCentralSignatureGithubInfo> signatureGithubInfo) {
String contents = "name: ci\n"
+ "\n"
+ "on: [push]\n"
Expand Down Expand Up @@ -63,8 +62,8 @@ public static String getTestWorkflow() {
+ "\n";
}

public static String getPublishWorkflow(String registryUrl,
Optional<MavenCentralSignatureGithubInfo> maybeSignatureGithubInfo) {
public static String getPublishWorkflow(
String registryUrl, Optional<MavenCentralSignatureGithubInfo> maybeSignatureGithubInfo) {
String content = " publish:\n"
+ " needs: [ compile, test ]\n"
+ " if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')\n"
Expand All @@ -85,8 +84,7 @@ public static String getPublishWorkflow(String registryUrl,
+ " run: |\n";

if (maybeSignatureGithubInfo.isPresent()) {
content = content
+ " ./.publish/prepare.sh\n";
content = content + " ./.publish/prepare.sh\n";
}
content = content
+ " ./gradlew publish\n"
Expand All @@ -106,6 +104,5 @@ public static String getPublishWorkflow(String registryUrl,
return content;
}

private GithubWorkflowGenerator() {
}
private GithubWorkflowGenerator() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.fern.irV42.model.commons.Name;
import com.fern.irV42.model.types.ContainerType;
import com.fern.irV42.model.types.Literal;
import com.fern.irV42.model.types.Literal.Visitor;
import com.fern.irV42.model.types.ObjectProperty;
import com.fern.java.immutables.StagedBuilderImmutablesStyle;
import com.fern.java.utils.JavaDocUtils;
Expand Down Expand Up @@ -97,10 +96,8 @@ static ImmutableEnrichedObjectProperty.CamelCaseKeyBuildStage builder() {

static EnrichedObjectProperty of(ObjectProperty objectProperty, boolean fromInterface, TypeName poetTypeName) {
Name name = objectProperty.getName().getName();
Optional<Literal> maybeLiteral = objectProperty
.getValueType()
.getContainer()
.flatMap(ContainerType::getLiteral);
Optional<Literal> maybeLiteral =
objectProperty.getValueType().getContainer().flatMap(ContainerType::getLiteral);
return EnrichedObjectProperty.builder()
.camelCaseKey(name.getCamelCase().getSafeName())
.pascalCaseKey(name.getPascalCase().getSafeName())
Expand Down
Loading

0 comments on commit 59eb861

Please sign in to comment.