Skip to content

Commit

Permalink
Merge pull request #94 from fvgh/feature/groovyEclipseStep
Browse files Browse the repository at this point in the history
Providing explicit support for Groovy source code (fixes #13)
  • Loading branch information
nedtwigg authored Apr 10, 2017
2 parents 5cf811c + 027892f commit 5046667
Show file tree
Hide file tree
Showing 32 changed files with 704 additions and 61 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ You might be looking for:

### Version 1.3.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/))

* Added support for Groovy via [greclipse](https://github.com/groovy/groovy-eclipse).

### Version 1.2.0 - April 3rd 2017 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.2.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.2.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))

* Deprecated `FileSignature.from` in favor of `FileSignature.signAsSet` and the new `FileSignature.signAsList`.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ lib('generic.LicenseHeaderStep') +'{{yes}} | {{no}}
lib('generic.ReplaceRegexStep') +'{{yes}} | {{no}} | {{no}} |',
lib('generic.ReplaceStep') +'{{yes}} | {{no}} | {{no}} |',
lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{no}} | {{no}} |',
extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
lib('java.GoogleJavaFormatStep') +'{{yes}} | {{no}} | {{no}} |',
lib('java.ImportOrderStep') +'{{yes}} | {{no}} | {{no}} |',
extra('java.EclipseFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
Expand All @@ -58,6 +59,7 @@ lib('scala.ScalaFmtStep') +'{{yes}} | {{no}}
| [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`generic.ReplaceStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`generic.TrimTrailingWhitespaceStep`](lib/src/main/java/com/diffplug/spotless/generic/TrimTrailingWhitespaceStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`groovy.GrEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`java.GoogleJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`java.EclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
Expand All @@ -75,6 +77,7 @@ lib('scala.ScalaFmtStep') +'{{yes}} | {{no}}
+ implementing up-to-date checking [#31](https://github.com/diffplug/spotless/issues/31)
+ breaking spotless into libraries [#56](https://github.com/diffplug/spotless/issues/56)
+ lots of other things, but especially the diff support in `spotlessCheck`
* Huge thanks to [Frank Vennemeyer](https://github.com/fvgh) for [Groovy support via greclipse](https://github.com/diffplug/spotless/issues/13).
* Huge thanks to [Stefan Oehme](https://github.com/oehme) for tons of help on the internal mechanics of Gradle.
* Formatting by Eclipse
+ Special thanks to [Mateusz Matela](https://waynebeaton.wordpress.com/2015/03/15/great-fixes-for-mars-winners-part-i/) for huge improvements to the eclipse code formatter!
Expand Down
2 changes: 1 addition & 1 deletion _ext/greclipse/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mayor/Minor versions are in line with the Groovy version values.
# Patch version is an incremental counter for the Spotless plugin.
grec_version=2.3.0-SNAPSHOT
grec_version=2.3.0
grec_artifactId=spotless-ext-greclipse
grec_description=Groovy Eclipse's formatter bundled for Spotless

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
repositories { maven { url 'https://plugins.gradle.org/m2/' } }
repositories { maven { url 'https://plugins.gradle.org/m2/' }}
dependencies {
classpath "com.diffplug.gradle:goomph:${VER_GOOMPH}"
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:${VER_BINTRAY}"
Expand Down
28 changes: 18 additions & 10 deletions gradle/java-publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ javadoc {
// use markdown in javadoc
def makeLink = { url, text -> "<a href=\"${url}\" style=\"text-transform: none;\">${text}</a>" }
def javadocInfo = '<h2>' + makeLink("https://github.com/${org}/${name}", "${group}:${project.ext.artifactId}:${ext.version}") +
' by ' + makeLink('http://www.diffplug.com', 'DiffPlug') + '</h2>'
' by ' + makeLink('http://www.diffplug.com', 'DiffPlug') + '</h2>'

String version_str = ext.version.endsWith('-SNAPSHOT') ? 'snapshot' : ext.version
apply plugin: 'ch.raffael.pegdown-doclet'
Expand Down Expand Up @@ -110,13 +110,14 @@ model {
}
if (project.ext.isSnapshot) {
// upload snapshots to oss.sonatype.org
repositories { maven {
url = 'https://oss.sonatype.org/content/repositories/snapshots'
credentials {
username = cred('nexus_user')
password = cred('nexus_pass')
}
} }
repositories {
maven {
url = 'https://oss.sonatype.org/content/repositories/snapshots'
credentials {
username = cred('nexus_user')
password = cred('nexus_pass')
}
}}
}
}
}
Expand All @@ -126,7 +127,9 @@ if (!ext.isSnapshot) {
bintray {
user = cred('bintray_user')
key = cred('bintray_pass')
publications = ['pluginMaven']
publications = [
'pluginMaven'
]
publish = true
pkg {
repo = 'opensource'
Expand All @@ -143,5 +146,10 @@ if (!ext.isSnapshot) {
}

publish.dependsOn(bintrayUpload)
bintrayUpload.dependsOn(['generatePomFileForPluginMavenPublication', jar, sourcesJar, javadocJar])
bintrayUpload.dependsOn([
'generatePomFileForPluginMavenPublication',
jar,
sourcesJar,
javadocJar
])
}
13 changes: 6 additions & 7 deletions gradle/java-setup.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
//////////
// JAVA //
//////////
repositories {
jcenter()
}
repositories { jcenter() }

// setup java
apply plugin: 'java'

sourceCompatibility = VER_JAVA
targetCompatibility = VER_JAVA
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
tasks.withType(JavaCompile) { options.encoding = 'UTF-8' }

/////////////
// ECLIPSE //
Expand All @@ -38,7 +34,10 @@ eclipseResourceFilters {
apply plugin: 'findbugs'
findbugs {
toolVersion = VER_FINDBUGS
sourceSets = [sourceSets.main] // don't check the test code
sourceSets = [
// don't check the test code
sourceSets.main
]
ignoreFailures = false // bug free or it doesn't ship!
reportsDir = file('build/findbugs')
effort = 'max' // min|default|max
Expand Down
5 changes: 2 additions & 3 deletions lib-extra/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ dependencies {
}

// we'll hold the core lib to a high standard
findbugs {
reportLevel = 'low' // low|medium|high (low = sensitive to even minor mistakes)
}
findbugs { reportLevel = 'low' } // low|medium|high (low = sensitive to even minor mistakes)

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.spotless.extra.groovy;

import java.io.File;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.Properties;

import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterProperties;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;

/** Formatter step which calls out to the Groovy-Eclipse formatter. */
public class GrEclipseFormatterStep {
public static final String defaultVersion() {
return DEFAULT_VERSION;
}

private static final String NAME = "groovy-eclipse formatter";
private static final String FORMATTER_CLASS = "com.diffplug.gradle.spotless.groovy.eclipse.GrEclipseFormatterStepImpl";
private static final String DEFAULT_VERSION = "2.3.0";
private static final String MAVEN_COORDINATE = "com.diffplug.spotless:spotless-ext-greclipse:";
private static final String FORMATTER_METHOD = "format";

/** Creates a formatter step using the default version for the given settings file. */
public static FormatterStep create(Iterable<File> settingsFiles, Provisioner provisioner) {
return create(defaultVersion(), settingsFiles, provisioner);
}

/** Creates a formatter step for the given version and settings file. */
public static FormatterStep create(String version, Iterable<File> settingsFiles, Provisioner provisioner) {
return FormatterStep.createLazy(
NAME,
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), settingsFiles),
State::createFormat);
}

private static class State implements Serializable {
private static final long serialVersionUID = 1L;

/** The jar that contains the eclipse formatter. */
final JarState jarState;
/** The signature of the settings file. */
final FileSignature settings;

State(JarState jar, final Iterable<File> settingsFiles) throws Exception {
this.jarState = Objects.requireNonNull(jar);
this.settings = FileSignature.signAsList(settingsFiles);
}

FormatterFunc createFormat() throws Exception {
FormatterProperties preferences = FormatterProperties.from(settings.files());

ClassLoader classLoader = jarState.getClassLoader();

// instantiate the formatter and get its format method
Class<?> formatterClazz = classLoader.loadClass(FORMATTER_CLASS);
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(preferences.getProperties());
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
return input -> {
try {
return (String) method.invoke(formatter, input);
} catch (InvocationTargetException exceptionWrapper) {
Throwable throwable = exceptionWrapper.getTargetException();
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null;
throw (null == exception) ? exceptionWrapper : exception;
}
};
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
package com.diffplug.spotless.extra.groovy;

import javax.annotation.ParametersAreNonnullByDefault;

import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.spotless.extra.groovy;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.junit.Test;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.SerializableEqualityTester;
import com.diffplug.spotless.StepHarness;
import com.diffplug.spotless.TestProvisioner;

public class GrEclipseFormatterStepTest extends ResourceHarness {

private static final String RESOURCE_PATH = "groovy/greclipse/format/";
private static final String CONFIG_FILE = RESOURCE_PATH + "greclipse.properties";

//String is hard-coded in the GrEclipseFormatter
private static final String FORMATTER_FILENAME_REPALCEMENT = "Hello.groovy";

private static Provisioner provisioner() {
return TestProvisioner.mavenCentral();
}

@Test
public void nominal() throws Throwable {
List<File> config = createTestFiles(CONFIG_FILE);
StepHarness.forStep(GrEclipseFormatterStep.create(config, provisioner()))
.testResource(RESOURCE_PATH + "unformatted.test", RESOURCE_PATH + "formatted.test");
}

@Test
public void formatterException() throws Throwable {
List<File> config = createTestFiles(CONFIG_FILE);
StepHarness.forStep(GrEclipseFormatterStep.create(config, provisioner()))
.testException(RESOURCE_PATH + "exception.test", assertion -> {
assertion.isInstanceOf(IllegalArgumentException.class);
assertion.hasMessageContaining(FORMATTER_FILENAME_REPALCEMENT);
});
}

@Test
public void configurationException() throws Throwable {
String configFileName = "greclipse.exception";
List<File> config = createTestFiles(RESOURCE_PATH + configFileName);
StepHarness.forStep(GrEclipseFormatterStep.create(config, provisioner()))
.testException(RESOURCE_PATH + "unformatted.test", assertion -> {
assertion.isInstanceOf(IllegalArgumentException.class);
assertion.hasMessageContaining(configFileName);
});
}

@Test
public void equality() throws IOException {
List<File> configFile = createTestFiles(CONFIG_FILE);
new SerializableEqualityTester() {

@Override
protected void setupTest(API api) {
api.areDifferentThan();
}

@Override
protected FormatterStep create() {
return GrEclipseFormatterStep.create(configFile, provisioner());
}
}.testEquals();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Class description
*/
class Foo{
String foo

/* Method */
def callBar() {
new Bar().bar(foo)
}
//Compiler error. Missing close-brace.
//
//Note that the error message is additionally logged to system-err
//within GrEclipse itself. That error message is just a dump of a RAW data report.
//The whole error handling therefore looks not nicely readable.
//Since these errors should only occur if the input file is not compile-able,
//the error format is considered acceptable, since the spotless task runs
//in general only after a successful compilation.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Class description
*/
class Foo
{
String foo

/* Method */
def callBar()
{
new Bar().bar(foo) }

/** Inner class */
class Bar
{

def bar(foo)
{
println "${foo}Bar" }}}

def foo = new Foo(foo: 'Foo')
foo.callBar()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The file name extension 'exception' not supported for GrEclipse configuration files.
Loading

0 comments on commit 5046667

Please sign in to comment.