diff --git a/changelog/@unreleased/pr-988.v2.yml b/changelog/@unreleased/pr-988.v2.yml new file mode 100644 index 000000000..2da0de181 --- /dev/null +++ b/changelog/@unreleased/pr-988.v2.yml @@ -0,0 +1,7 @@ +type: improvement +improvement: + 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/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 3af9343ae..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 @@ -20,6 +20,9 @@ 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.nio.file.Files; import java.nio.file.Path; @@ -43,6 +46,18 @@ 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 -> { + 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); @@ -75,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"), @@ -92,7 +107,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 5703a4c47..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 = ''' @@ -227,4 +232,41 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest { BuildResult result = with('spotlessJavaCheck').build() result.task(":spotlessJava").outcome == TaskOutcome.SUCCESS } + + def 'formatDiff updates only lines changed in git diff'() { + when: + buildFile << standardBuildFile + "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 { + public static void crazyExistingFormatting ( String... args) { + + } + } + '''.stripIndent() + + "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 { + public static void crazyExistingFormatting ( 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 crazyExistingFormatting ( String... args) { + System.out.println("Reformat me please"); + } + } + '''.stripIndent() + } }