-
Notifications
You must be signed in to change notification settings - Fork 455
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
Align Java Eclipse formatter gradle interface with greclipse/scala #109
Changes from all commits
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 |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
*/ | ||
package com.diffplug.gradle.spotless; | ||
|
||
import static com.diffplug.gradle.spotless.PluginGradlePreconditions.requireElementsNonNull; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
|
@@ -24,6 +26,8 @@ | |
import org.gradle.api.plugins.JavaPluginConvention; | ||
import org.gradle.api.tasks.SourceSet; | ||
|
||
import com.diffplug.common.base.StringPrinter; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.SerializableFileFilter; | ||
import com.diffplug.spotless.extra.java.EclipseFormatterStep; | ||
import com.diffplug.spotless.generic.LicenseHeaderStep; | ||
|
@@ -59,17 +63,23 @@ public void importOrderFile(Object importOrderFile) { | |
addStep(ImportOrderStep.createFromFile(getProject().file(importOrderFile))); | ||
} | ||
|
||
/** Use {@link #eclipse()} instead */ | ||
@Deprecated | ||
public void eclipseFormatFile(Object eclipseFormatFile) { | ||
eclipseFormatFile(EclipseFormatterStep.defaultVersion(), eclipseFormatFile); | ||
} | ||
|
||
/** Use {@link #eclipse(String)} instead */ | ||
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. I think it would be easier for a new user to understand if this said,
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. Again, 50/50, tie goes to the implementor :) |
||
@Deprecated | ||
public void eclipseFormatFile(String eclipseVersion, Object eclipseFormatFile) { | ||
Objects.requireNonNull(eclipseVersion, "eclipseVersion"); | ||
Objects.requireNonNull(eclipseFormatFile, "eclipseFormatFile"); | ||
Project project = getProject(); | ||
addStep(EclipseFormatterStep.create(eclipseVersion, | ||
project.files(eclipseFormatFile).getFiles(), | ||
GradleProvisioner.fromProject(project))); | ||
getProject().getLogger().warn( | ||
StringPrinter.buildStringFromLines( | ||
"'eclipseFormatFile [version] <file>' is deprecated.", | ||
"Use 'eclipse([version]).configFile(<file>)' instead.", | ||
"For details see https://github.com/diffplug/spotless/tree/master/plugin-gradle#applying-to-java-source")); | ||
eclipse(eclipseVersion).configFile(eclipseFormatFile); | ||
} | ||
|
||
/** Removes any unused imports. */ | ||
|
@@ -93,6 +103,37 @@ public void googleJavaFormat(String version) { | |
addStep(GoogleJavaFormatStep.create(version, GradleProvisioner.fromProject(getProject()))); | ||
} | ||
|
||
public EclipseConfig eclipse() { | ||
return eclipse(EclipseFormatterStep.defaultVersion()); | ||
} | ||
|
||
public EclipseConfig eclipse(String version) { | ||
return new EclipseConfig(version); | ||
} | ||
|
||
public class EclipseConfig { | ||
final String version; | ||
Object[] configFiles; | ||
|
||
EclipseConfig(String version) { | ||
configFiles = new Object[0]; | ||
this.version = Objects.requireNonNull(version); | ||
addStep(createStep()); | ||
} | ||
|
||
public void configFile(Object... configFiles) { | ||
this.configFiles = requireElementsNonNull(configFiles); | ||
replaceStep(createStep()); | ||
} | ||
|
||
private FormatterStep createStep() { | ||
Project project = getProject(); | ||
return EclipseFormatterStep.create(version, | ||
project.files(configFiles).getFiles(), | ||
GradleProvisioner.fromProject(project)); | ||
} | ||
} | ||
|
||
/** If the user hasn't specified the files yet, we'll assume he/she means all of the java files. */ | ||
@Override | ||
protected void setupTask(SpotlessTask task) { | ||
|
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 think it would be easier for a new user to understand if this said,
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.
That would not be JavaDoc compliant. The comment is more fore the plugin developer. The user gets the "Use 'eclipse([version]).configFile()' instead." warning at the end of the build. Sounds reasonable to you?
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.
Oh yes, you're right! Silly me. 😉
I do understand that, from a Gradle user perspective, they'd write
eclipse([version]).configFile configFile.xml
, but I still think that, from a spotless-lib user perspective, it should be clearly stated which method aftereclipse()
needs to be invoked in the chain to get the exact same behaviour aseclipseFormatFile()
, so as to prevent confusion or ambiguity.How about the following JavaDoc for
eclipseFormatFile(String, Object)
......and the following for
eclipseFormatFile(Object)
instead?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.
The idea itself is very nice and I will definitely use this approach in future when describing more complex use cases.
But honestly I do not think that this would fit here. I saw that you guys try avoid superfluous comments where code is self explaining. I think we agree that the usage of
EclipseConfig
is quite self explaining.But that's the final comment from my side. The final decision is yours. Let me know.
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.
Hmm, @nedtwigg, what do you think?
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 feel it's 50/50 - tie goes to the implementor :)