From 374fbda71cdf71fdf260d1c2e7fea328ef5a2138 Mon Sep 17 00:00:00 2001 From: Callum Rogers Date: Mon, 21 Oct 2019 16:10:22 +0100 Subject: [PATCH 01/12] Format diff task --- .../baseline/plugins/BaselineFormat.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) 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 cebf0ea8c..5a91a4bd5 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 @@ -18,21 +18,26 @@ import com.diffplug.gradle.spotless.SpotlessExtension; import com.diffplug.spotless.FormatterFunc; +import com.palantir.javaformat.java.FormatDiffCli; import com.palantir.javaformat.java.Formatter; import com.palantir.javaformat.java.JavaFormatterOptions; import com.palantir.javaformat.java.JavaFormatterOptions.Style; -import java.io.File; -import java.nio.file.Files; -import java.nio.file.Path; +import java.io.IOException; +import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.plugins.JavaBasePlugin; import org.gradle.api.plugins.JavaPluginConvention; +import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskProvider; import org.gradle.api.tasks.compile.JavaCompile; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; + class BaselineFormat extends AbstractBaselinePlugin { // TODO(dfox): remove this feature flag when we've refined the eclipse.xml sufficiently @@ -45,6 +50,10 @@ public void apply(Project project) { this.project = project; project.getPluginManager().withPlugin("java", plugin -> { + if (palantirJavaFormatterEnabled(project)) { + project.getTasks().register("formatDiff", FormatDiff.class); + } + project.getPluginManager().apply("com.diffplug.gradle.spotless"); Path eclipseXml = eclipseConfigFile(project); @@ -107,6 +116,13 @@ public void apply(Project project) { }); } + public static class FormatDiff extends DefaultTask { + @TaskAction + public final void formatDiff() throws IOException, InterruptedException { + FormatDiffCli.formatDiff(getProject().getProjectDir().toPath()); + } + } + static boolean eclipseFormattingEnabled(Project project) { return project.hasProperty(ECLIPSE_FORMATTING); } From 8d8a5ca634df37b04bade4e891057542b4307c76 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 22 Oct 2019 11:26:27 +0100 Subject: [PATCH 02/12] use new class name --- .../palantir/baseline/plugins/BaselineFormat.java | 15 +++++++-------- versions.props | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) 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 5a91a4bd5..a5ca3d6cd 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 @@ -18,11 +18,14 @@ import com.diffplug.gradle.spotless.SpotlessExtension; import com.diffplug.spotless.FormatterFunc; -import com.palantir.javaformat.java.FormatDiffCli; +import com.palantir.javaformat.java.FormatDiff; import com.palantir.javaformat.java.Formatter; import com.palantir.javaformat.java.JavaFormatterOptions; import com.palantir.javaformat.java.JavaFormatterOptions.Style; +import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; import org.gradle.api.Project; @@ -34,10 +37,6 @@ import org.gradle.api.tasks.TaskProvider; import org.gradle.api.tasks.compile.JavaCompile; -import java.io.File; -import java.nio.file.Files; -import java.nio.file.Path; - class BaselineFormat extends AbstractBaselinePlugin { // TODO(dfox): remove this feature flag when we've refined the eclipse.xml sufficiently @@ -51,7 +50,7 @@ public void apply(Project project) { project.getPluginManager().withPlugin("java", plugin -> { if (palantirJavaFormatterEnabled(project)) { - project.getTasks().register("formatDiff", FormatDiff.class); + project.getTasks().register("formatDiff", FormatDiffTask.class); } project.getPluginManager().apply("com.diffplug.gradle.spotless"); @@ -116,10 +115,10 @@ public void apply(Project project) { }); } - public static class FormatDiff extends DefaultTask { + public static class FormatDiffTask extends DefaultTask { @TaskAction public final void formatDiff() throws IOException, InterruptedException { - FormatDiffCli.formatDiff(getProject().getProjectDir().toPath()); + FormatDiff.formatDiff(getProject().getProjectDir().toPath()); } } diff --git a/versions.props b/versions.props index 02aac34fa..7b1ae820a 100644 --- a/versions.props +++ b/versions.props @@ -7,7 +7,7 @@ com.google.errorprone:error_prone_test_helpers = 2.3.3 com.google.guava:guava = 27.1-jre com.netflix.nebula:nebula-dependency-recommender = 9.0.0 com.palantir.configurationresolver:gradle-configuration-resolver-plugin = 0.4.0 -com.palantir.javaformat:* = 0.2.7 +com.palantir.javaformat:* = 0.2.8 com.palantir.safe-logging:* = 1.11.0 org.apache.maven.shared:maven-dependency-analyzer = 1.11.1 org.github.ngbinh.scalastyle:gradle-scalastyle-plugin_2.11 = 1.0.1 From 79d51dd0bba86ca3f0199f4a5eb071c998714269 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 22 Oct 2019 10:26:27 +0000 Subject: [PATCH 03/12] Add generated changelog entries --- changelog/@unreleased/pr-988.v2.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 changelog/@unreleased/pr-988.v2.yml diff --git a/changelog/@unreleased/pr-988.v2.yml b/changelog/@unreleased/pr-988.v2.yml new file mode 100644 index 000000000..b54ada4b9 --- /dev/null +++ b/changelog/@unreleased/pr-988.v2.yml @@ -0,0 +1,7 @@ +type: improvement +improvement: + description: Run `./gradlew formatDiff -Pcom.palantir.baseline-format.palantir-java-format` + to reformat the relevant sections of any uncommitted changed Java files (relies + on `git diff -U0 HEAD` under the hood) + links: + - https://github.com/palantir/gradle-baseline/pull/988 From 9b07712e705d674bd8d8ec6712652d7df40f2873 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 22 Oct 2019 11:44:09 +0100 Subject: [PATCH 04/12] WIP test --- .../baseline/plugins/BaselineFormat.java | 4 +-- .../BaselineFormatIntegrationTest.groovy | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) 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 a5ca3d6cd..a2d592b4c 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 @@ -49,9 +49,9 @@ public void apply(Project project) { this.project = project; project.getPluginManager().withPlugin("java", plugin -> { - if (palantirJavaFormatterEnabled(project)) { + // if (palantirJavaFormatterEnabled(project)) { project.getTasks().register("formatDiff", FormatDiffTask.class); - } + // } project.getPluginManager().apply("com.diffplug.gradle.spotless"); Path eclipseXml = eclipseConfigFile(project); 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 61f237e80..3e873dac8 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 @@ -221,4 +221,40 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { BuildResult result = with('spotlessJavaCheck').build() result.task(":spotlessJava").outcome == TaskOutcome.SUCCESS } + + def 'formatDiff updates only relevant chunks of files'() { + when: + buildFile << standardBuildFile + "git init".execute(Collections.emptyList(), projectDir) + "git config user.name Foo".execute(Collections.emptyList(), projectDir) + "git config user.email foo@bar.com".execute(Collections.emptyList(), projectDir) + + file('src/main/java/Main.java') << ''' + class Main { + public static void main(String... args) { + + } + } + '''.stripIndent() + + "git commit --allow-empty -m Commit".execute(Collections.emptyList(), projectDir) + + file('src/main/java/Main.java').text = ''' + class Main { + public static void main(String... args) { + System.out.println("Reformat me please"); + } + } + '''.stripIndent() + + then: + with('formatDiff', '-Pcom.palantir.baseline-format.palantir-java-format').build() + file('src/main/java/Main.java').text == ''' + class Main { + public static void main(String... args) { + System.out.println("Reformat me please"); + } + } + '''.stripIndent() + } } From 3cb742bab2d71a48d2e521a76c3e33d3bd66838c Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 22 Oct 2019 11:54:03 +0100 Subject: [PATCH 05/12] git add and git commit --- .../baseline/BaselineFormatIntegrationTest.groovy | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 3e873dac8..f337a86ed 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 @@ -225,9 +225,9 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { def 'formatDiff updates only relevant chunks of files'() { when: buildFile << standardBuildFile - "git init".execute(Collections.emptyList(), projectDir) - "git config user.name Foo".execute(Collections.emptyList(), projectDir) - "git config user.email foo@bar.com".execute(Collections.emptyList(), projectDir) + "git init".execute(Collections.emptyList(), projectDir).waitFor() + "git config user.name Foo".execute(Collections.emptyList(), projectDir).waitFor() + "git config user.email foo@bar.com".execute(Collections.emptyList(), projectDir).waitFor() file('src/main/java/Main.java') << ''' class Main { @@ -237,7 +237,8 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { } '''.stripIndent() - "git commit --allow-empty -m Commit".execute(Collections.emptyList(), projectDir) + "git add .".execute(Collections.emptyList(), projectDir).waitFor() + "git commit -m Commit".execute(Collections.emptyList(), projectDir).waitFor() file('src/main/java/Main.java').text = ''' class Main { From 65026aaac8e525e57c137138e4588089bcc1b168 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 22 Oct 2019 11:57:24 +0100 Subject: [PATCH 06/12] Nice description --- .../palantir/baseline/plugins/BaselineFormat.java | 12 ++++++++---- .../baseline/BaselineFormatIntegrationTest.groovy | 8 ++++---- 2 files changed, 12 insertions(+), 8 deletions(-) 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 a2d592b4c..e04bd2ed1 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 @@ -49,9 +49,11 @@ public void apply(Project project) { this.project = project; project.getPluginManager().withPlugin("java", plugin -> { - // if (palantirJavaFormatterEnabled(project)) { - project.getTasks().register("formatDiff", FormatDiffTask.class); - // } + project.getTasks().register("formatDiff", FormatDiffTask.class, task -> { + task.setDescription("Format only chunks of files that appear in git diff"); + task.setGroup("Formatting"); + task.onlyIf(t -> palantirJavaFormatterEnabled(project)); + }); project.getPluginManager().apply("com.diffplug.gradle.spotless"); Path eclipseXml = eclipseConfigFile(project); @@ -95,7 +97,9 @@ public void apply(Project project) { spotlessExtension.setEnforceCheck(false); // necessary because SpotlessPlugin creates tasks in an afterEvaluate block - TaskProvider formatTask = project.getTasks().register("format"); + TaskProvider formatTask = project.getTasks().register("format", task -> { + task.setGroup("Formatting"); + }); project.afterEvaluate(p -> { Task spotlessJava = project.getTasks().getByName("spotlessJava"); Task spotlessApply = project.getTasks().getByName("spotlessApply"); 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 f337a86ed..897899523 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 @@ -222,7 +222,7 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { result.task(":spotlessJava").outcome == TaskOutcome.SUCCESS } - def 'formatDiff updates only relevant chunks of files'() { + def 'formatDiff updates only lines changed in git diff'() { when: buildFile << standardBuildFile "git init".execute(Collections.emptyList(), projectDir).waitFor() @@ -231,7 +231,7 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { file('src/main/java/Main.java') << ''' class Main { - public static void main(String... args) { + public static void crazyExistingFormatting ( String... args) { } } @@ -242,7 +242,7 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { file('src/main/java/Main.java').text = ''' class Main { - public static void main(String... args) { + public static void crazyExistingFormatting ( String... args) { System.out.println("Reformat me please"); } } @@ -252,7 +252,7 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { with('formatDiff', '-Pcom.palantir.baseline-format.palantir-java-format').build() file('src/main/java/Main.java').text == ''' class Main { - public static void main(String... args) { + public static void crazyExistingFormatting ( String... args) { System.out.println("Reformat me please"); } } From 6f89efc46230ef9a6e8ad077a7939f4eb4702edf Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 22 Oct 2019 12:48:27 +0100 Subject: [PATCH 07/12] -Drecreate=true --- .../MultilineStringConstant.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/gradle-baseline-java/src/test/resources/com/palantir/baseline/palantirjavaformat-expected/MultilineStringConstant.java b/gradle-baseline-java/src/test/resources/com/palantir/baseline/palantirjavaformat-expected/MultilineStringConstant.java index abab4bea4..8792cfbb2 100644 --- a/gradle-baseline-java/src/test/resources/com/palantir/baseline/palantirjavaformat-expected/MultilineStringConstant.java +++ b/gradle-baseline-java/src/test/resources/com/palantir/baseline/palantirjavaformat-expected/MultilineStringConstant.java @@ -3,15 +3,11 @@ class MultilineStringConstant { // NON-NLS comments are required for i18n, it's important they are kept with their strings. - private static final String MULTIPLE_LINE_NON_NLS = "field_0," - + //$NON-NLS-1$ - "field_1," - + //$NON-NLS-1$ - "field_2," - + //$NON-NLS-1$ - "field_3," - + //$NON-NLS-1$ - "field_4"; //$NON-NLS-1$ + private static final String MULTIPLE_LINE_NON_NLS = "field_0," //$NON-NLS-1$ + + "field_1," //$NON-NLS-1$ + + "field_2," //$NON-NLS-1$ + + "field_3," //$NON-NLS-1$ + + "field_4"; //$NON-NLS-1$ private static final String MULTIPLE_LINE_NO_COMMENT = "field_0," + "field_1," + "field_2," + "field_3," + "field_4"; From 4110d46f5361959ed1d69d55f5f771b86af45562 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 22 Oct 2019 12:48:38 +0100 Subject: [PATCH 08/12] Move group and onlyIf --- .../palantir/baseline/plugins/BaselineFormat.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) 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 e04bd2ed1..ee1eade64 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 @@ -49,11 +49,7 @@ public void apply(Project project) { this.project = project; project.getPluginManager().withPlugin("java", plugin -> { - project.getTasks().register("formatDiff", FormatDiffTask.class, task -> { - task.setDescription("Format only chunks of files that appear in git diff"); - task.setGroup("Formatting"); - task.onlyIf(t -> palantirJavaFormatterEnabled(project)); - }); + project.getTasks().register("formatDiff", FormatDiffTask.class); project.getPluginManager().apply("com.diffplug.gradle.spotless"); Path eclipseXml = eclipseConfigFile(project); @@ -120,6 +116,12 @@ public void apply(Project project) { } public static class FormatDiffTask extends DefaultTask { + public FormatDiffTask() { + setDescription("Format only chunks of files that appear in git diff"); + setGroup("Formatting"); + onlyIf(t -> palantirJavaFormatterEnabled(getProject())); + } + @TaskAction public final void formatDiff() throws IOException, InterruptedException { FormatDiff.formatDiff(getProject().getProjectDir().toPath()); From bf66fd20a3173a7258d58dbcad310033e3183f6d Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 22 Oct 2019 12:53:10 +0100 Subject: [PATCH 09/12] Don't require the feature flag --- changelog/@unreleased/pr-988.v2.yml | 6 +++--- .../com/palantir/baseline/plugins/BaselineFormat.java | 1 - .../palantir/baseline/BaselineFormatIntegrationTest.groovy | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/changelog/@unreleased/pr-988.v2.yml b/changelog/@unreleased/pr-988.v2.yml index b54ada4b9..2da0de181 100644 --- a/changelog/@unreleased/pr-988.v2.yml +++ b/changelog/@unreleased/pr-988.v2.yml @@ -1,7 +1,7 @@ type: improvement improvement: - description: Run `./gradlew formatDiff -Pcom.palantir.baseline-format.palantir-java-format` - to reformat the relevant sections of any uncommitted changed Java files (relies - on `git diff -U0 HEAD` under the hood) + description: > + Run `./gradlew formatDiff` to reformat the relevant sections of any uncommitted changed Java files + (relies on `git diff -U0 HEAD` under the hood) links: - https://github.com/palantir/gradle-baseline/pull/988 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 ee1eade64..a3a3abff9 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 @@ -119,7 +119,6 @@ public static class FormatDiffTask extends DefaultTask { public FormatDiffTask() { setDescription("Format only chunks of files that appear in git diff"); setGroup("Formatting"); - onlyIf(t -> palantirJavaFormatterEnabled(getProject())); } @TaskAction 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 897899523..d268ad2bf 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 @@ -249,7 +249,7 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { '''.stripIndent() then: - with('formatDiff', '-Pcom.palantir.baseline-format.palantir-java-format').build() + with('formatDiff').build() file('src/main/java/Main.java').text == ''' class Main { public static void crazyExistingFormatting ( String... args) { From 86f457c75ab32bbd0f91697132a04572a6c22181 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 22 Oct 2019 12:59:07 +0100 Subject: [PATCH 10/12] Redundant modifier --- .../groovy/com/palantir/baseline/plugins/BaselineFormat.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a3a3abff9..3fa8278a1 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 @@ -116,7 +116,7 @@ public void apply(Project project) { } public static class FormatDiffTask extends DefaultTask { - public FormatDiffTask() { + FormatDiffTask() { setDescription("Format only chunks of files that appear in git diff"); setGroup("Formatting"); } From 3e68a7d392777c4d6bbcb4c921e70f05b02bcbfa Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Wed, 23 Oct 2019 11:31:08 +0100 Subject: [PATCH 11/12] Use the upstream formatDiff task --- .../palantir/baseline/plugins/Baseline.java | 4 -- .../baseline/plugins/BaselineFormat.java | 39 ++++++++----------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java index 734a31c3a..b10ea1322 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java @@ -36,10 +36,6 @@ public void apply(Project project) { rootProject.getPluginManager().apply(BaselineConfig.class); rootProject.getPluginManager().apply(BaselineCircleCi.class); - if (BaselineFormat.palantirJavaFormatterEnabled(project)) { - rootProject.getPluginManager().apply("com.palantir.java-format-provider"); - rootProject.getPluginManager().apply("com.palantir.java-format-idea"); - } rootProject.allprojects(proj -> { proj.getPluginManager().apply(BaselineCheckstyle.class); proj.getPluginManager().apply(BaselineScalastyle.class); 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 8113ddf31..0897135dc 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,26 +17,21 @@ package com.palantir.baseline.plugins; import com.diffplug.gradle.spotless.SpotlessExtension; -import com.diffplug.spotless.FormatterFunc; -import com.palantir.javaformat.java.FormatDiff; -import com.palantir.javaformat.java.Formatter; -import com.palantir.javaformat.java.JavaFormatterOptions; -import com.palantir.javaformat.java.JavaFormatterOptions.Style; import com.google.common.base.Preconditions; import com.palantir.baseline.plugins.format.PalantirJavaFormatStep; import com.palantir.javaformat.gradle.JavaFormatExtension; +import com.palantir.javaformat.gradle.PalantirJavaFormatIdeaPlugin; +import com.palantir.javaformat.gradle.PalantirJavaFormatPlugin; +import com.palantir.javaformat.gradle.PalantirJavaFormatProviderPlugin; import java.io.File; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.plugins.JavaBasePlugin; import org.gradle.api.plugins.JavaPluginConvention; -import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskProvider; import org.gradle.api.tasks.compile.JavaCompile; @@ -51,9 +46,19 @@ class BaselineFormat extends AbstractBaselinePlugin { public void apply(Project project) { this.project = project; + if (project == project.getRootProject()) { + if (BaselineFormat.palantirJavaFormatterEnabled(project)) { + project.getPluginManager().apply(PalantirJavaFormatIdeaPlugin.class); + } + } + project.getPluginManager().withPlugin("java", plugin -> { - project.getTasks().register("formatDiff", FormatDiffTask.class); + if (palantirJavaFormatterEnabled(project)) { + project.getPlugins().apply(PalantirJavaFormatPlugin.class); // provides the formatDiff task + } + }); + project.getPluginManager().withPlugin("java", plugin -> { project.getPluginManager().apply("com.diffplug.gradle.spotless"); Path eclipseXml = eclipseConfigFile(project); @@ -85,8 +90,8 @@ public void apply(Project project) { if (palantirJavaFormatterEnabled(project)) { Preconditions.checkState( - project.getRootProject().getPluginManager().hasPlugin("com.palantir.java-format-provider"), - "Must apply `com.palantir.baseline` to root project when setting '%s'", + project.getRootProject().getPlugins().hasPlugin(PalantirJavaFormatProviderPlugin.class), + "Must apply `com.palantir.baseline-format` to root project when setting '%s'", PJF_PROPERTY); java.addStep(PalantirJavaFormatStep.create( project.getRootProject().getConfigurations().getByName("palantirJavaFormat"), @@ -124,18 +129,6 @@ public void apply(Project project) { }); } - public static class FormatDiffTask extends DefaultTask { - FormatDiffTask() { - setDescription("Format only chunks of files that appear in git diff"); - setGroup("Formatting"); - } - - @TaskAction - public final void formatDiff() throws IOException, InterruptedException { - FormatDiff.formatDiff(getProject().getProjectDir().toPath()); - } - } - static boolean eclipseFormattingEnabled(Project project) { return project.hasProperty(ECLIPSE_FORMATTING); } From 4fbd7ffe5fe3bd9c4b6f7ab0a5a007ac7245be97 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Wed, 23 Oct 2019 11:51:25 +0100 Subject: [PATCH 12/12] test passes --- .../palantir/baseline/BaselineFormatIntegrationTest.groovy | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 c03355368..b30ab1318 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 @@ -40,6 +40,11 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { id 'java' id 'com.palantir.baseline-format' } + repositories { + // to resolve the `palantirJavaFormat` configuration + maven { url 'https://dl.bintray.com/palantir/releases' } + jcenter() + } '''.stripIndent() def noJavaBuildFile = ''' @@ -255,7 +260,7 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { '''.stripIndent() then: - with('formatDiff').build() + with('formatDiff', '-Pcom.palantir.baseline-format.palantir-java-format').build() file('src/main/java/Main.java').text == ''' class Main { public static void crazyExistingFormatting ( String... args) {