-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for palantir-java-format
- Loading branch information
1 parent
8ea1d03
commit 177393c
Showing
6 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/dev/lukebemish/immaculate/steps/PalantirJavaFormatStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package dev.lukebemish.immaculate.steps; | ||
|
||
import dev.lukebemish.immaculate.ForkFormatterSpec; | ||
import dev.lukebemish.immaculate.ImmaculatePlugin; | ||
import java.util.List; | ||
import javax.inject.Inject; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.artifacts.Dependency; | ||
import org.gradle.api.model.ObjectFactory; | ||
import org.gradle.api.provider.Property; | ||
import org.gradle.api.tasks.Internal; | ||
import org.gradle.jvm.toolchain.JavaToolchainService; | ||
|
||
public abstract class PalantirJavaFormatStep extends WrapperFormattingStep { | ||
|
||
private static final String MAVEN_PATH = "com.palantir.javaformat:palantir-java-format"; | ||
private static final String DEFAULT_VERSION = "2.50.0"; | ||
private static final List<String> GOOGLE_JAVA_FORMAT_ADD_EXPORTS = List.of( | ||
"jdk.compiler/com.sun.tools.javac.api", "jdk.compiler/com.sun.tools.javac.code", | ||
"jdk.compiler/com.sun.tools.javac.file", "jdk.compiler/com.sun.tools.javac.parser", | ||
"jdk.compiler/com.sun.tools.javac.tree", "jdk.compiler/com.sun.tools.javac.util" | ||
); | ||
private final transient Property<Dependency> formatterDependency; | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
@Inject | ||
public PalantirJavaFormatStep(final String name, final String workflowName, final Project project, final ObjectFactory objectFactory, final JavaToolchainService javaToolchainService) { | ||
super(name, workflowName, project, objectFactory, javaToolchainService); | ||
this.getDependencies().getRuntime().add("dev.lukebemish.immaculate.wrapper:palantir-java-format", dep -> { | ||
if (ImmaculatePlugin.PLUGIN_VERSION != null) { | ||
dep.version(constraint -> | ||
constraint.require(ImmaculatePlugin.PLUGIN_VERSION) | ||
); | ||
} | ||
}); | ||
this.formatterDependency = objectFactory.property(Dependency.class); | ||
this.formatterDependency.convention(this.getDependencies().module(MAVEN_PATH + ":" + DEFAULT_VERSION)); | ||
this.getDependencies().getRuntime().add(this.formatterDependency); | ||
} | ||
|
||
@Internal | ||
protected Property<Dependency> getGoogleJavaFormatter() { | ||
return this.formatterDependency; | ||
} | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
public void version(final String version) { | ||
this.formatterDependency.set(this.getDependencies().module(MAVEN_PATH + ":" + version)); | ||
} | ||
|
||
@Override | ||
protected void configureSpec(final ForkFormatterSpec spec) { | ||
GOOGLE_JAVA_FORMAT_ADD_EXPORTS.forEach(e -> spec.getJvmArgs().addAll("--add-exports", e + "=ALL-UNNAMED")); | ||
spec.getWrapperClass().set("dev.lukebemish.immaculate.wrapper.palantirjavaformat.PalantirJavaFormatWrapper"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
dependencies { | ||
compileOnly 'com.palantir.javaformat:palantir-java-format:2.50.0' | ||
implementation project(':wrapper') | ||
} | ||
|
||
publishing { | ||
publications { | ||
mavenJava(MavenPublication) { | ||
from components.java | ||
managedVersioning.publishing.sign(signing, it) | ||
managedVersioning.publishing.pom(it, github_repo, license) | ||
pom { | ||
name = 'Immaculate -- Palantir Java Format Wrapper' | ||
description = 'A wrapper around the Palantir java formatter' | ||
} | ||
} | ||
} | ||
} | ||
|
||
tasks.named('publishCentral') { | ||
dependsOn tasks.publish | ||
} |
47 changes: 47 additions & 0 deletions
47
.../java/dev/lukebemish/immaculate/wrapper/palantirjavaformat/PalantirJavaFormatWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package dev.lukebemish.immaculate.wrapper.palantirjavaformat; | ||
|
||
import com.palantir.javaformat.java.Main; | ||
import dev.lukebemish.immaculate.wrapper.Wrapper; | ||
import java.io.BufferedWriter; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.io.PrintWriter; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class PalantirJavaFormatWrapper implements Wrapper { | ||
|
||
private final String[] args; | ||
|
||
public PalantirJavaFormatWrapper(final String[] args) { | ||
this.args = args; | ||
} | ||
|
||
@Override | ||
public String format(final String fileName, final String text) { | ||
final ByteArrayInputStream inStream = new ByteArrayInputStream(text.getBytes()); | ||
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
final PrintWriter outWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))); | ||
final PrintWriter errWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, StandardCharsets.UTF_8))); | ||
final var formatter = new Main(outWriter, errWriter, inStream); | ||
final String[] fullArgs = new String[this.args.length + 1]; | ||
fullArgs[this.args.length] = "-"; | ||
System.arraycopy(this.args, 0, fullArgs, 0, this.args.length); | ||
try { | ||
final int ok = formatter.format(fullArgs); | ||
if (ok != 0) { | ||
throw new RuntimeException("Failed to format " + fileName + ", exit code: " + ok); | ||
} | ||
} catch (final Exception e) { | ||
if (e instanceof final RuntimeException runtimeException) { | ||
throw runtimeException; | ||
} else { | ||
throw new RuntimeException(e); | ||
} | ||
} finally { | ||
errWriter.flush(); | ||
outWriter.flush(); | ||
} | ||
return outputStream.toString(StandardCharsets.UTF_8); | ||
} | ||
} |