From 2b61742a7ff4f27eaebc7884d631b85dcedf4863 Mon Sep 17 00:00:00 2001 From: Nicholas Gates Date: Tue, 23 Jul 2019 17:34:05 +0100 Subject: [PATCH] Eclipse formatter --- .../resources/spotless/eclipse.xml | 318 ++++++++++++++++++ .../baseline/plugins/BaselineFormat.java | 10 +- .../BaselineConfigIntegrationTest.groovy | 10 +- .../BaselineFormatIntegrationTest.groovy | 26 +- .../baseline/BaselineFormatTest.groovy | 5 + 5 files changed, 353 insertions(+), 16 deletions(-) create mode 100644 gradle-baseline-java-config/resources/spotless/eclipse.xml diff --git a/gradle-baseline-java-config/resources/spotless/eclipse.xml b/gradle-baseline-java-config/resources/spotless/eclipse.xml new file mode 100644 index 000000000..4878f2269 --- /dev/null +++ b/gradle-baseline-java-config/resources/spotless/eclipse.xml @@ -0,0 +1,318 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineFormat.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineFormat.java index d3873a75f..7bed667e1 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineFormat.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineFormat.java @@ -17,6 +17,7 @@ package com.palantir.baseline.plugins; import com.diffplug.gradle.spotless.SpotlessExtension; +import java.nio.file.Paths; import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.file.ConfigurableFileCollection; @@ -46,14 +47,12 @@ public void apply(Project project) { // use empty string to specify one group for all non-static imports java.importOrder(""); java.trimTrailingWhitespace(); - java.indentWithSpaces(4); - java.endWithNewline(); + + java.eclipse().configFile( + project.file(Paths.get(getConfigDir(), "spotless/eclipse.xml").toString())); // No empty lines at start of blocks java.replaceRegex("Block starts with blank lines", "\\) \\{\n+", ") {\n"); - - // No dangling parentheses - closing paren must be on the same line as the expression - java.replaceRegex("Dangling closing parenthesis", "(\n\\s+)+\\)", ")"); }); // necessary because SpotlessPlugin creates tasks in an afterEvaluate block @@ -65,4 +64,5 @@ public void apply(Project project) { }); }); } + } diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineConfigIntegrationTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineConfigIntegrationTest.groovy index 290941f71..8f75dc0d8 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineConfigIntegrationTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineConfigIntegrationTest.groovy @@ -22,7 +22,7 @@ import org.gradle.testkit.runner.TaskOutcome * This test relies on running ./gradlew :gradle-baseline-java-config:publishToMavenLocal. */ class BaselineConfigIntegrationTest extends AbstractPluginTest { - def projectVersion = "git describe --tags --first-parent --dirty=.dirty".execute().text.trim() + def projectVersion = "git describe --tags --first-parent --dirty=.dirty --abbrev=7".execute().text.trim() def standardBuildFile = """ plugins { id 'com.palantir.baseline-config' @@ -46,7 +46,9 @@ class BaselineConfigIntegrationTest extends AbstractPluginTest { then: with('--stacktrace', '--info', 'baselineUpdateConfig').build() - directory('.baseline').list().toList().toSet() == ['checkstyle', 'copyright', 'eclipse', 'idea'].toSet() + directory('.baseline').list().toList().toSet() == [ + 'checkstyle', 'copyright', 'eclipse', 'idea', 'spotless' + ].toSet() directory('project').list().toList().isEmpty() } @@ -65,7 +67,9 @@ class BaselineConfigIntegrationTest extends AbstractPluginTest { then: with('--stacktrace', '--info', 'baselineUpdateConfig').build() - directory('.baseline').list().toList().toSet() == ['checkstyle', 'copyright', 'eclipse', 'idea'].toSet() + directory('.baseline').list().toList().toSet() == [ + 'checkstyle', 'copyright', 'eclipse', 'idea', 'spotless' + ].toSet() directory('project').list().toList().toSet() == ['scalastyle_config.xml'].toSet() } diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineFormatIntegrationTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineFormatIntegrationTest.groovy index 40b3db4f8..34b3ade59 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineFormatIntegrationTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineFormatIntegrationTest.groovy @@ -16,11 +16,18 @@ package com.palantir.baseline +import org.apache.commons.io.FileUtils import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.TaskOutcome class BaselineFormatIntegrationTest extends AbstractPluginTest { + def setup() { + FileUtils.copyDirectory( + new File("../gradle-baseline-java-config/resources"), + new File(projectDir, ".baseline")) + } + def standardBuildFile = ''' plugins { id 'java' @@ -36,14 +43,17 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { def validJavaFile = ''' package test; - public class Test { void test() { - int x = 1; - System.out.println( - "Hello"); - Optional.of("hello").orElseGet(() -> { - return "Hello World"; - }); - } } + + public class Test { + void test() { + int x = 1; + System.out.println( + "Hello"); + Optional.of("hello").orElseGet(() -> { + return "Hello World"; + }); + } + } '''.stripIndent() def invalidJavaFile = ''' diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineFormatTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineFormatTest.groovy index 73b8bbd41..563d452c3 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineFormatTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineFormatTest.groovy @@ -28,6 +28,11 @@ class BaselineFormatTest extends Specification { def setup() { project = ProjectBuilder.builder().build() + project.buildscript { + repositories { + mavenCentral() + } + } project.plugins.apply 'java' project.plugins.apply BaselineFormat project.evaluate()