Skip to content

Commit

Permalink
Format diff task
Browse files Browse the repository at this point in the history
  • Loading branch information
CRogers committed Oct 21, 2019
1 parent 493ed42 commit b7e306f
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit b7e306f

Please sign in to comment.