Skip to content

Commit

Permalink
Provides groovyGradle formatter as agreed in #115.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Vennemeyer committed Aug 12, 2017
1 parent 8c0893a commit 117bdcd
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 8 deletions.
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Changed `importOrder` interface from array to varargs ([#125](https://github.com/diffplug/spotless/issues/125))
* The `kotlin` extension was mis-spelled as `kotin`.
* Added `kotlinGradle` method to `SpotlessExtension` for linting [Gradle Kotlin DSL](https://github.com/gradle/kotlin-dsl) files with [ktlint](https://github.com/shyiko/ktlint). ([#115](https://github.com/diffplug/spotless/issues/115))
* Added dedicated `groovyGradle` for formating of Gradle files


### Version 3.4.1 - July 11th 2017 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.4.1/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.4.1))

Expand Down
5 changes: 5 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ spotless {
// so it formats Java files by default (unless `excludeJava` is used).
greclipse().configFile('greclipse.properties')
}
groovyGradle {
// same as groovy, but for .gradle (defaults to '*.gradle')
target '*.gradle', 'additionalScripts/*.gradle'
greclipse().configFile('greclipse.properties')
}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,29 @@ public GrEclipseConfig greclipse() {
}

public GrEclipseConfig greclipse(String version) {
return new GrEclipseConfig(version);
return new GrEclipseConfig(version, this);
}

public class GrEclipseConfig {
public static class GrEclipseConfig {
final String version;
Object[] configFiles;
final FormatExtension extension;

GrEclipseConfig(String version) {
GrEclipseConfig(String version, FormatExtension extension) {
this.extension = extension;
configFiles = new Object[0];
this.version = Objects.requireNonNull(version);
addStep(createStep());

extension.addStep(createStep());
}

public void configFile(Object... configFiles) {
this.configFiles = requireElementsNonNull(configFiles);
replaceStep(createStep());
extension.replaceStep(createStep());
}

private FormatterStep createStep() {
Project project = getProject();
Project project = extension.getProject();
return GrEclipseFormatterStep.create(version,
project.files(configFiles).getFiles(),
GradleProvisioner.fromProject(project));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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;

import java.util.Objects;

import com.diffplug.spotless.extra.groovy.GrEclipseFormatterStep;
import com.diffplug.spotless.java.ImportOrderStep;

public class GroovyGradleExtension extends FormatExtension {
private static final String GRADLE_FILE_EXTENSION = "*.gradle";
static final String NAME = "groovyGradle";

public GroovyGradleExtension(SpotlessExtension rootExtension) {
super(rootExtension);
}

public void importOrder(String... importOrder) {
addStep(ImportOrderStep.createFromOrder(importOrder));
}

public void importOrderFile(Object importOrderFile) {
Objects.requireNonNull(importOrderFile);
addStep(ImportOrderStep.createFromFile(getProject().file(importOrderFile)));
}

public GroovyExtension.GrEclipseConfig greclipse() {
return greclipse(GrEclipseFormatterStep.defaultVersion());
}

public GroovyExtension.GrEclipseConfig greclipse(String version) {
return new GroovyExtension.GrEclipseConfig(version, this);
}

@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
target = parseTarget(GRADLE_FILE_EXTENSION);
}
super.setupTask(task);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public void groovy(Action<GroovyExtension> closure) {
configure(GroovyExtension.NAME, GroovyExtension.class, closure);
}

/** Configures the special groovy-specific extension for Gradle files. */
public void groovyGradle(Action<GroovyGradleExtension> closure) {
configure(GroovyGradleExtension.NAME, GroovyGradleExtension.class, closure);
}

/** Configures a custom extension. */
public void format(String name, Action<FormatExtension> closure) {
requireNonNull(name, "name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.junit.Assert;
import org.junit.Test;

public class GroovyDefaultTargetTest extends GradleIntegrationTest {
public class GroovyExtensionTest extends GradleIntegrationTest {

private final String HEADER = "//My tests header";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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;

import java.io.File;
import java.io.IOException;

import org.assertj.core.api.Assertions;
import org.junit.Test;

public class GroovyGradleExtensionTest extends GradleIntegrationTest {

private final String HEADER = "//My tests header";

@Test
public void defaultTarget() throws IOException {
testTarget(true);
}

@Test
public void customTarget() throws IOException {
testTarget(false);
}

private void testTarget(boolean useDefaultTarget) throws IOException {
String target = useDefaultTarget ? "" : "target 'other.gradle'";
File projectFile = write("build.gradle",
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
"spotless {",
" groovyGradle {",
target,
" licenseHeader('" + HEADER + "', 'plugins')",
" }",
"}");

// write appends a line ending so re-read to see what the original currently looks like
String unModified = read(projectFile.toPath());

// Run
gradleRunner().withArguments("spotlessApply").build();

if (useDefaultTarget) {
Assertions.assertThat(read(projectFile.toPath())).contains(HEADER);
} else {
assertFileContent(unModified, projectFile);
}
}

}
2 changes: 1 addition & 1 deletion spotlessSelf.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ spotless {
trimTrailingWhitespace()
removeUnusedImports()
}
groovy {
groovyGradle {
target fileTree('.') {
include '**/*.gradle'
exclude '_ext/**'
Expand Down

0 comments on commit 117bdcd

Please sign in to comment.