-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Anyone interested in #13 is invited to submit PR's against branch feature/groovy. Spotless' maintainers are not fans of Groovy. We'll help and support anyone who wants to contribute, but we don't actually know anything about it.
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
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
84 changes: 84 additions & 0 deletions
84
src/main/java/com/diffplug/gradle/spotless/groovy/GroovyExtension.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,84 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* 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.diffplug.gradle.spotless.groovy; | ||
|
||
import java.util.List; | ||
|
||
import org.gradle.api.GradleException; | ||
import org.gradle.api.internal.file.UnionFileCollection; | ||
import org.gradle.api.plugins.JavaPluginConvention; | ||
import org.gradle.api.tasks.SourceSet; | ||
|
||
import com.diffplug.gradle.spotless.FormatExtension; | ||
import com.diffplug.gradle.spotless.FormatTask; | ||
import com.diffplug.gradle.spotless.LicenseHeaderStep; | ||
import com.diffplug.gradle.spotless.SpotlessExtension; | ||
import com.diffplug.gradle.spotless.java.ImportSorterStep; | ||
|
||
// TODO: None of this is actually groovy specific. Spotless' | ||
// maintainers are not fans of Groovy, but we're happy to take | ||
// PRs to flesh out this functionality. | ||
public class GroovyExtension extends FormatExtension { | ||
public static final String NAME = "groovy"; | ||
|
||
public GroovyExtension(SpotlessExtension rootExtension) { | ||
super(NAME, rootExtension); | ||
} | ||
|
||
public static final String LICENSE_HEADER_DELIMITER = "package "; | ||
|
||
public void licenseHeader(String licenseHeader) { | ||
licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER); | ||
} | ||
|
||
public void licenseHeaderFile(Object licenseHeaderFile) { | ||
licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER); | ||
} | ||
|
||
public void importOrder(List<String> importOrder) { | ||
customLazy(ImportSorterStep.NAME, () -> new ImportSorterStep(importOrder)::format); | ||
} | ||
|
||
public void importOrderFile(Object importOrderFile) { | ||
customLazy(ImportSorterStep.NAME, () -> new ImportSorterStep(getProject().file(importOrderFile))::format); | ||
} | ||
|
||
/** If the user hasn't specified the files yet, we'll assume he/she means all of the java files. */ | ||
@Override | ||
protected void setupTask(FormatTask task) throws Exception { | ||
if (target == null) { | ||
JavaPluginConvention javaPlugin = getProject().getConvention().getPlugin(JavaPluginConvention.class); | ||
if (javaPlugin == null) { | ||
throw new GradleException("You must apply the java plugin before the spotless plugin if you are using the java extension."); | ||
} | ||
UnionFileCollection union = new UnionFileCollection(); | ||
for (SourceSet sourceSet : javaPlugin.getSourceSets()) { | ||
union.add(sourceSet.getJava()); | ||
} | ||
target = union; | ||
} | ||
// LicenseHeaderStep completely blows apart package-info.java - this common-sense check ensures that | ||
// it skips package-info.java. See https://github.com/diffplug/spotless/issues/1 | ||
steps.replaceAll(step -> { | ||
if (LicenseHeaderStep.NAME.equals(step.getName())) { | ||
return step.filterByFile(file -> !file.getName().equals("package-info.java")); | ||
} else { | ||
return step; | ||
} | ||
}); | ||
super.setupTask(task); | ||
} | ||
} |