-
Notifications
You must be signed in to change notification settings - Fork 134
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
Improvement: Load palantir-java-format dynamically #989
Changes from all commits
30167b0
e9be62d
7f9d8f0
e1c33bf
188ab74
36b428a
18b1a18
7607d30
4f80d9a
719e269
b043fa6
74355fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type: improvement | ||
improvement: | ||
description: Load palantir-java-format dynamically from the same configuration set | ||
up by `com.palantir-java-format` which is also used to determine the version used | ||
by IntelliJ. | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/989 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,9 @@ | |
package com.palantir.baseline.plugins; | ||
|
||
import com.diffplug.gradle.spotless.SpotlessExtension; | ||
import com.diffplug.spotless.FormatterFunc; | ||
import com.palantir.javaformat.java.Formatter; | ||
import com.palantir.javaformat.java.JavaFormatterOptions; | ||
import com.palantir.javaformat.java.JavaFormatterOptions.Style; | ||
import com.google.common.base.Preconditions; | ||
import com.palantir.baseline.plugins.format.PalantirJavaFormatStep; | ||
import com.palantir.javaformat.gradle.JavaFormatExtension; | ||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
@@ -52,11 +51,8 @@ public void apply(Project project) { | |
spotlessExtension.java(java -> { | ||
// Configure a lazy FileCollection then pass it as the target | ||
ConfigurableFileCollection allJavaFiles = project.files(); | ||
project | ||
.getConvention() | ||
.getPlugin(JavaPluginConvention.class) | ||
.getSourceSets() | ||
.all(sourceSet -> allJavaFiles.from( | ||
project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all( | ||
sourceSet -> allJavaFiles.from( | ||
sourceSet.getAllJava().filter(file -> !file.toString().contains(GENERATED_MARKER)))); | ||
|
||
java.target(allJavaFiles); | ||
|
@@ -67,15 +63,24 @@ public void apply(Project project) { | |
if (eclipseFormattingEnabled(project) && palantirJavaFormatterEnabled(project)) { | ||
throw new GradleException( | ||
"Can't use both eclipse and palantir-java-format at the same time, please delete one of " | ||
+ ECLIPSE_FORMATTING + " or " + PJF_PROPERTY + " from your gradle.properties"); | ||
+ ECLIPSE_FORMATTING | ||
+ " or " | ||
+ PJF_PROPERTY | ||
+ " from your gradle.properties"); | ||
} | ||
|
||
if (eclipseFormattingEnabled(project)) { | ||
java.eclipse().configFile(project.file(eclipseXml.toString())); | ||
} | ||
|
||
if (palantirJavaFormatterEnabled(project)) { | ||
java.customLazy("palantir-java-format", PalantirJavaFormatterFunc::new); | ||
Preconditions.checkState( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. who produces There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's produced in |
||
project.getRootProject().getPluginManager().hasPlugin("com.palantir.java-format-provider"), | ||
"Must apply `com.palantir.baseline` to root project when setting '%s'", | ||
PJF_PROPERTY); | ||
java.addStep(PalantirJavaFormatStep.create( | ||
project.getRootProject().getConfigurations().getByName("palantirJavaFormat"), | ||
project.getRootProject().getExtensions().getByType(JavaFormatExtension.class))); | ||
} | ||
|
||
java.trimTrailingWhitespace(); | ||
|
@@ -99,7 +104,7 @@ public void apply(Project project) { | |
}); | ||
project.getTasks().withType(JavaCompile.class).configureEach(spotlessJava::mustRunAfter); | ||
|
||
//re-enable spotless checking, but lazily so it doesn't eagerly configure everything else | ||
// re-enable spotless checking, but lazily so it doesn't eagerly configure everything else | ||
project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME).configure(t -> { | ||
t.dependsOn(project.getTasks().named("spotlessCheck")); | ||
}); | ||
|
@@ -118,14 +123,4 @@ static boolean palantirJavaFormatterEnabled(Project project) { | |
static Path eclipseConfigFile(Project project) { | ||
return project.getRootDir().toPath().resolve(".baseline/spotless/eclipse.xml"); | ||
} | ||
|
||
private static class PalantirJavaFormatterFunc implements FormatterFunc { | ||
private static final JavaFormatterOptions OPTIONS = | ||
JavaFormatterOptions.builder().style(Style.PALANTIR).build(); | ||
|
||
@Override | ||
public String apply(String input) throws Exception { | ||
return Formatter.createFormatter(OPTIONS).formatSourceAndFixImports(input); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License 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 com.palantir.baseline.plugins.format; | ||
|
||
import com.diffplug.spotless.FileSignature; | ||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.google.common.base.Suppliers; | ||
import com.palantir.javaformat.gradle.JavaFormatExtension; | ||
import com.palantir.javaformat.java.FormatterService; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.function.Supplier; | ||
import org.gradle.api.artifacts.Configuration; | ||
|
||
public final class PalantirJavaFormatStep { | ||
|
||
private static final String IMPL_CLASS = "com.palantir.javaformat.java.Formatter"; | ||
|
||
private PalantirJavaFormatStep() {} | ||
|
||
private static final String NAME = "palantir-java-format"; | ||
|
||
/** Creates a step which formats everything - code, import order, and unused imports. */ | ||
public static FormatterStep create(Configuration palantirJavaFormat, JavaFormatExtension extension) { | ||
ensureImplementationNotDirectlyLoadable(); | ||
Supplier<FormatterService> memoizedService = Suppliers.memoize(extension::serviceLoad); | ||
return FormatterStep.createLazy( | ||
NAME, () -> new State(palantirJavaFormat.getFiles(), memoizedService), State::createFormat); | ||
} | ||
|
||
static final class State implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
// Kept for state serialization purposes. | ||
@SuppressWarnings("unused") | ||
private final String stepName = NAME; | ||
|
||
// Kept for state serialization purposes. | ||
@SuppressWarnings({"unused", "FieldCanBeLocal"}) | ||
private final FileSignature jarsSignature; | ||
|
||
// Transient as this is not serializable. | ||
private final transient Supplier<FormatterService> memoizedFormatter; | ||
|
||
/** | ||
* Build a cacheable state for spotless from the given jars, that uses the given {@link FormatterService}. | ||
* | ||
* @param jars The jars that contain the palantir-java-format implementation. This is only used for caching and | ||
* up-to-dateness purposes. | ||
*/ | ||
State(Iterable<File> jars, Supplier<FormatterService> memoizedFormatter) throws IOException { | ||
this.jarsSignature = FileSignature.signAsSet(jars); | ||
this.memoizedFormatter = memoizedFormatter; | ||
} | ||
|
||
@SuppressWarnings("NullableProblems") | ||
FormatterFunc createFormat() { | ||
return memoizedFormatter.get()::formatSourceReflowStringsAndFixImports; | ||
} | ||
} | ||
|
||
private static void ensureImplementationNotDirectlyLoadable() { | ||
try { | ||
PalantirJavaFormatStep.class.getClassLoader().loadClass(IMPL_CLASS); | ||
} catch (ClassNotFoundException e) { | ||
// expected | ||
return; | ||
} | ||
throw new RuntimeException("Expected not be be able to load " | ||
+ IMPL_CLASS | ||
+ " via main class loader but was able to. Please ensure that `buildscript.configurations.classpath`" | ||
+ " doesn't depend on `com.palantir.javaformat:palantir-java-format`."); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reformatted the whole file using the formatter so a few other things have changed, sorry!