Skip to content

Commit

Permalink
Added the skeleton of a Groovy DSL.
Browse files Browse the repository at this point in the history
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
nedtwigg committed Jan 14, 2016
1 parent 87ee3ed commit db129fd
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.gradle.api.Project;

import com.diffplug.gradle.spotless.freshmark.FreshMarkExtension;
import com.diffplug.gradle.spotless.groovy.GroovyExtension;
import com.diffplug.gradle.spotless.java.JavaExtension;
import groovy.lang.Closure;

Expand Down Expand Up @@ -59,6 +60,19 @@ public void java(Consumer<JavaExtension> closure) {
closure.accept(java);
}

/** Configures the special groovy-specific extension. */
public void groovy(Closure<GroovyExtension> closure) {
GroovyExtension groovy = new GroovyExtension(this);
closure.setDelegate(groovy);
closure.call();
}

/** Configures the special groovy-specific extension. */
public void groovy(Consumer<GroovyExtension> closure) {
GroovyExtension groovy = new GroovyExtension(this);
closure.accept(groovy);
}

/** Configures the special freshmark-specific extension. */
public void freshmark(Closure<FreshMarkExtension> closure) {
FreshMarkExtension freshmark = new FreshMarkExtension(this);
Expand Down
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);
}
}

0 comments on commit db129fd

Please sign in to comment.