Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format diff task #988

Merged
merged 13 commits into from
Oct 23, 2019
7 changes: 7 additions & 0 deletions changelog/@unreleased/pr-988.v2.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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"),
Expand All @@ -92,7 +107,9 @@ public void apply(Project project) {
spotlessExtension.setEnforceCheck(false);

// necessary because SpotlessPlugin creates tasks in an afterEvaluate block
TaskProvider<Task> formatTask = project.getTasks().register("format");
TaskProvider<Task> formatTask = project.getTasks().register("format", task -> {
task.setGroup("Formatting");
});
project.afterEvaluate(p -> {
Task spotlessJava = project.getTasks().getByName("spotlessJava");
Task spotlessApply = project.getTasks().getByName("spotlessApply");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '''
Expand Down Expand Up @@ -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");
}
}
iamdanfox marked this conversation as resolved.
Show resolved Hide resolved
'''.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()
}
}