Skip to content

Commit

Permalink
prepare for PR (diffplug#326)
Browse files Browse the repository at this point in the history
moved formatter to lib
use formatter version available on maven central
  • Loading branch information
matthiasbalke committed Dec 28, 2018
1 parent 66f486a commit 40daf9a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.extra.antlr4;
package com.diffplug.spotless.antlr4;

import java.io.IOException;
import java.io.Serializable;
Expand All @@ -28,17 +28,15 @@ public class Antlr4FormatterStep {

private Antlr4FormatterStep() {}

private static final String MAVEN_COORDINATE = "com.khubla.antlr4formatter:antlr4formatter:";
private static final String DEFAULT_VERSION = "0.1.0-SNAPSHOT";
private static final String MAVEN_COORDINATE = "com.khubla.antlr4formatter:antlr4-formatter:";
private static final String DEFAULT_VERSION = "1.1.0";

public static FormatterStep create(Provisioner provisioner) {
return create(DEFAULT_VERSION, provisioner);
return create(defaultVersion(), provisioner);
}

public static FormatterStep create(String version, Provisioner provisioner) {
// return FormatterStep.createNeverUpToDate(NAME, Antlr4Formatter::format);

return FormatterStep.createLazy(NAME, () -> new State(defaultVersion(), provisioner), State::createFormat);
return FormatterStep.createLazy(NAME, () -> new State(version, provisioner), State::createFormat);
}

public static String defaultVersion() {
Expand All @@ -57,19 +55,16 @@ static final class State implements Serializable {
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
}

FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException {
FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException {
ClassLoader classLoader = jarState.getClassLoader();

// String Antlr4Formatter::format(String input)

Class<?> formatter = classLoader.loadClass("com.khubla.antlr4formatter.Antlr4Formatter");
Object formatterInstance = formatter.newInstance();
// and its format method
Method formatterMethod = formatter.getMethod("format", String.class);

return input -> {
try {
return (String) formatterMethod.invoke(formatterInstance, input);
return (String) formatterMethod.invoke(null, input);
} catch (InvocationTargetException e) {
throw ThrowingEx.unwrapCause(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.diffplug.gradle.spotless;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.extra.antlr4.Antlr4FormatterStep;
import com.diffplug.spotless.antlr4.Antlr4FormatterStep;

public class Antlr4Extension extends FormatExtension {
static final String NAME = "antlr4";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@

import java.io.IOException;

import org.gradle.api.GradleException;
import org.junit.Test;

public class Antlr4ExtensionTest extends GradleIntegrationTest {

@Test
public void formatSingleGrammar() throws IOException, InterruptedException {
public void formatSingleGrammar() throws IOException {
String testFile = "src/main/antlr4/Hello.g4";

setFile("build.gradle").toLines(
Expand All @@ -44,18 +43,4 @@ public void formatSingleGrammar() throws IOException, InterruptedException {

assertFile(testFile).sameAsResource(formatted);
}

@Test(expected = GradleException.class)
public void missingTargetArgumentFailsFormatting() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" antlr4 {",
" }",
"}");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.TestProvisioner;
import com.diffplug.spotless.extra.antlr4.Antlr4FormatterStep;
import com.diffplug.spotless.antlr4.Antlr4FormatterStep;

public class Antlr4TaskTest extends ResourceHarness {

private Provisioner provisioner = TestProvisioner.mavenLocal();
private Provisioner provisioner = TestProvisioner.mavenCentral();

private SpotlessTask checkTask;
private SpotlessTask applyTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.extra.antlr4;
package com.diffplug.spotless.antlr4;

import org.junit.Test;

Expand All @@ -25,7 +25,7 @@ public class Antlr4FormatterStepTest extends ResourceHarness {

@Test
public void formatGrammar() throws Throwable {
FormatterStep step = Antlr4FormatterStep.create(TestProvisioner.mavenLocal());
FormatterStep step = Antlr4FormatterStep.create(TestProvisioner.mavenCentral());
assertOnResources(step, "antlr4/Hello.unformatted.g4", "antlr4/Hello.formatted.g4");
}

Expand Down

0 comments on commit 40daf9a

Please sign in to comment.