-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from fvgh/feature/groovyEclipseStep
Providing explicit support for Groovy source code (fixes #13)
- Loading branch information
Showing
32 changed files
with
704 additions
and
61 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
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
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
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
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
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
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
91 changes: 91 additions & 0 deletions
91
lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.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,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; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/package-info.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,7 @@ | ||
@ParametersAreNonnullByDefault | ||
@ReturnValuesAreNonnullByDefault | ||
package com.diffplug.spotless.extra.groovy; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault; |
88 changes: 88 additions & 0 deletions
88
lib-extra/src/test/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStepTest.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,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(); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
lib-extra/src/test/resources/groovy/greclipse/format/exception.test
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,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. |
22 changes: 22 additions & 0 deletions
22
lib-extra/src/test/resources/groovy/greclipse/format/formatted.test
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,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() |
1 change: 1 addition & 0 deletions
1
lib-extra/src/test/resources/groovy/greclipse/format/greclipse.exception
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 @@ | ||
The file name extension 'exception' not supported for GrEclipse configuration files. |
Oops, something went wrong.