Skip to content

Commit db129fd

Browse files
committed
Added the skeleton of a Groovy DSL.
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.
1 parent 87ee3ed commit db129fd

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.gradle.api.Project;
2424

2525
import com.diffplug.gradle.spotless.freshmark.FreshMarkExtension;
26+
import com.diffplug.gradle.spotless.groovy.GroovyExtension;
2627
import com.diffplug.gradle.spotless.java.JavaExtension;
2728
import groovy.lang.Closure;
2829

@@ -59,6 +60,19 @@ public void java(Consumer<JavaExtension> closure) {
5960
closure.accept(java);
6061
}
6162

63+
/** Configures the special groovy-specific extension. */
64+
public void groovy(Closure<GroovyExtension> closure) {
65+
GroovyExtension groovy = new GroovyExtension(this);
66+
closure.setDelegate(groovy);
67+
closure.call();
68+
}
69+
70+
/** Configures the special groovy-specific extension. */
71+
public void groovy(Consumer<GroovyExtension> closure) {
72+
GroovyExtension groovy = new GroovyExtension(this);
73+
closure.accept(groovy);
74+
}
75+
6276
/** Configures the special freshmark-specific extension. */
6377
public void freshmark(Closure<FreshMarkExtension> closure) {
6478
FreshMarkExtension freshmark = new FreshMarkExtension(this);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless.groovy;
17+
18+
import java.util.List;
19+
20+
import org.gradle.api.GradleException;
21+
import org.gradle.api.internal.file.UnionFileCollection;
22+
import org.gradle.api.plugins.JavaPluginConvention;
23+
import org.gradle.api.tasks.SourceSet;
24+
25+
import com.diffplug.gradle.spotless.FormatExtension;
26+
import com.diffplug.gradle.spotless.FormatTask;
27+
import com.diffplug.gradle.spotless.LicenseHeaderStep;
28+
import com.diffplug.gradle.spotless.SpotlessExtension;
29+
import com.diffplug.gradle.spotless.java.ImportSorterStep;
30+
31+
// TODO: None of this is actually groovy specific. Spotless'
32+
// maintainers are not fans of Groovy, but we're happy to take
33+
// PRs to flesh out this functionality.
34+
public class GroovyExtension extends FormatExtension {
35+
public static final String NAME = "groovy";
36+
37+
public GroovyExtension(SpotlessExtension rootExtension) {
38+
super(NAME, rootExtension);
39+
}
40+
41+
public static final String LICENSE_HEADER_DELIMITER = "package ";
42+
43+
public void licenseHeader(String licenseHeader) {
44+
licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER);
45+
}
46+
47+
public void licenseHeaderFile(Object licenseHeaderFile) {
48+
licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER);
49+
}
50+
51+
public void importOrder(List<String> importOrder) {
52+
customLazy(ImportSorterStep.NAME, () -> new ImportSorterStep(importOrder)::format);
53+
}
54+
55+
public void importOrderFile(Object importOrderFile) {
56+
customLazy(ImportSorterStep.NAME, () -> new ImportSorterStep(getProject().file(importOrderFile))::format);
57+
}
58+
59+
/** If the user hasn't specified the files yet, we'll assume he/she means all of the java files. */
60+
@Override
61+
protected void setupTask(FormatTask task) throws Exception {
62+
if (target == null) {
63+
JavaPluginConvention javaPlugin = getProject().getConvention().getPlugin(JavaPluginConvention.class);
64+
if (javaPlugin == null) {
65+
throw new GradleException("You must apply the java plugin before the spotless plugin if you are using the java extension.");
66+
}
67+
UnionFileCollection union = new UnionFileCollection();
68+
for (SourceSet sourceSet : javaPlugin.getSourceSets()) {
69+
union.add(sourceSet.getJava());
70+
}
71+
target = union;
72+
}
73+
// LicenseHeaderStep completely blows apart package-info.java - this common-sense check ensures that
74+
// it skips package-info.java. See https://github.com/diffplug/spotless/issues/1
75+
steps.replaceAll(step -> {
76+
if (LicenseHeaderStep.NAME.equals(step.getName())) {
77+
return step.filterByFile(file -> !file.getName().equals("package-info.java"));
78+
} else {
79+
return step;
80+
}
81+
});
82+
super.setupTask(task);
83+
}
84+
}

0 commit comments

Comments
 (0)