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 -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
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@

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 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;

Expand All @@ -45,6 +49,12 @@ 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));
iamdanfox marked this conversation as resolved.
Show resolved Hide resolved
});

project.getPluginManager().apply("com.diffplug.gradle.spotless");
Path eclipseXml = eclipseConfigFile(project);

Expand Down Expand Up @@ -87,7 +97,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 All @@ -107,6 +119,13 @@ public void apply(Project project) {
});
}

public static class FormatDiffTask extends DefaultTask {
@TaskAction
public final void formatDiff() throws IOException, InterruptedException {
FormatDiff.formatDiff(getProject().getProjectDir().toPath());
}
}

static boolean eclipseFormattingEnabled(Project project) {
return project.hasProperty(ECLIPSE_FORMATTING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,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()
}
}
2 changes: 1 addition & 1 deletion versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down