Skip to content

Commit

Permalink
Intermediate commit - compiles but SelfTest fails!
Browse files Browse the repository at this point in the history
  • Loading branch information
jbduncan committed Oct 30, 2016
1 parent 7e1fcad commit 6061363
Show file tree
Hide file tree
Showing 20 changed files with 453 additions and 275 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ dependencies {
compile "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}"
testCompile "junit:junit:${VER_JUNIT}"
testCompile "org.assertj:assertj-core:${VER_ASSERTJ}"
testCompile "org.mockito:mockito-core:${VER_MOCKITO}"

// add the eclipse jars to the embedded configuration
eclipseDeps.each { embeddedJars "p2:${it}:+" }
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ VER_JGIT=4.5.0.201609210915-r
# Testing
VER_JUNIT=4.12
VER_ASSERTJ=3.5.2
VER_MOCKITO=2.2.9
52 changes: 52 additions & 0 deletions src/main/java/com/diffplug/gradle/spotless/ApplyFormatTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.diffplug.gradle.spotless;

import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.tasks.TaskAction;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class ApplyFormatTask extends DefaultTask {
// set by SpotlessExtension, but possibly overridden by FormatExtension
public Charset encoding = StandardCharsets.UTF_8;
public LineEnding.Policy lineEndingPolicy = LineEnding.UNIX_POLICY;

// set by FormatExtension
public boolean paddedCell = false;
public Iterable<File> target;
public List<FormatterStep> steps = new ArrayList<>();

@TaskAction
public void apply() throws IOException {
if (target == null) {
throw new GradleException("You must specify 'Iterable<File> toFormat'");
}
// combine them into the master formatter
Formatter formatter = Formatter.builder()
.lineEndingPolicy(lineEndingPolicy)
.encoding(encoding)
.projectDirectory(getProject().getProjectDir().toPath())
.steps(steps)
.build();

formatApply(formatter);
}

/** Applies the format. */
private void formatApply(Formatter formatter) throws IOException {
for (File file : target) {
getLogger().debug("Applying format to " + file);
// keep track of the problem toFormat
if (paddedCell) {
PaddedCellTaskMisc.apply(this, formatter, file);
} else {
formatter.applyFormat(file);
}
};
}
}
98 changes: 98 additions & 0 deletions src/main/java/com/diffplug/gradle/spotless/CheckFormatTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.diffplug.gradle.spotless;

import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.incremental.IncrementalTaskInputs;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class CheckFormatTask extends DefaultTask {
// set by SpotlessExtension, but possibly overridden by FormatExtension
@Input
public Charset encoding = StandardCharsets.UTF_8;
@Input
public LineEnding.Policy lineEndingPolicy = LineEnding.UNIX_POLICY;

// set by FormatExtension
@Input
public boolean paddedCell = false;
@InputFiles
@SkipWhenEmpty
public Iterable<File> target;
@Input
@SkipWhenEmpty
public List<FormatterStep> steps = new ArrayList<>();

@TaskAction
public void check(IncrementalTaskInputs inputs) throws IOException {
if (target == null) {
throw new GradleException("You must specify 'Iterable<File> toFormat'");
}
// combine them into the master formatter
Formatter formatter = Formatter.builder()
.lineEndingPolicy(lineEndingPolicy)
.encoding(encoding)
.projectDirectory(getProject().getProjectDir().toPath())
.steps(steps)
.build();

formatCheck(formatter, inputs);
}

/** Checks the format. */
private void formatCheck(Formatter formatter, IncrementalTaskInputs inputs) throws IOException {
List<File> problemFiles = new ArrayList<>();

inputs.outOfDate(input -> {
File file = input.getFile();
getLogger().debug("Checking format on " + file);
// keep track of the problem toFormat
try {
if (!formatter.isClean(file)) {
problemFiles.add(file);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});

if (paddedCell) {
PaddedCellTaskMisc.check(this, formatter, problemFiles);
} else {
if (!problemFiles.isEmpty()) {
// if we're not in paddedCell mode, we'll check if maybe we should be
if (PaddedCellTaskMisc.anyMisbehave(formatter, problemFiles)) {
throw PaddedCellTaskMisc.youShouldTurnOnPaddedCell(this);
} else {
throw formatViolationsFor(formatter, problemFiles);
}
}
}
}

/** Returns an exception which indicates problem files nicely. */
GradleException formatViolationsFor(Formatter formatter, List<File> problemFiles) throws IOException {
return new GradleException(DiffMessageFormatter.messageFor(this, formatter, problemFiles));
}

/** Returns the name of this format. */
String getFormatName() {
String name = getName();
if (name.startsWith(SpotlessPlugin.EXTENSION)) {
String after = name.substring(SpotlessPlugin.EXTENSION.length());
return after.substring(0, after.length() - SpotlessPlugin.CHECK.length()).toLowerCase(Locale.US);
}
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class DiffMessageFormatter {
private static final int MAX_CHECK_MESSAGE_LINES = 50;
static final int MAX_FILES_TO_LIST = 10;

static String messageFor(FormatTask task, Formatter formatter, List<File> problemFiles) throws IOException {
static String messageFor(CheckFormatTask task, Formatter formatter, List<File> problemFiles) throws IOException {
DiffMessageFormatter diffFormater = new DiffMessageFormatter(task, formatter, problemFiles);
return "The following files had format violations:\n"
+ diffFormater.buffer
Expand All @@ -49,10 +49,10 @@ static String messageFor(FormatTask task, Formatter formatter, List<File> proble
+ "' to fix these violations.";
}

StringBuilder buffer = new StringBuilder(MAX_CHECK_MESSAGE_LINES * 64);
int numLines = 0;
private final StringBuilder buffer = new StringBuilder(MAX_CHECK_MESSAGE_LINES * 64);
private int numLines = 0;

private DiffMessageFormatter(FormatTask task, Formatter formatter, List<File> problemFiles) throws IOException {
private DiffMessageFormatter(CheckFormatTask task, Formatter formatter, List<File> problemFiles) throws IOException {
Preconditions.checkArgument(!problemFiles.isEmpty(), "Problem files must not be empty");

Path rootDir = task.getProject().getRootDir().toPath();
Expand Down Expand Up @@ -119,7 +119,7 @@ private void addLine(String line) {
* look like if formatted using the given formatter. Does not end with any newline
* sequence (\n, \r, \r\n).
*/
private static String diff(FormatTask task, Formatter formatter, File file) throws IOException {
private static String diff(CheckFormatTask task, Formatter formatter, File file) throws IOException {
String raw = new String(Files.readAllBytes(file.toPath()), formatter.encoding);
String rawUnix = LineEnding.toUnix(raw);
String formattedUnix;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.diffplug.gradle.spotless;

import java.io.File;
import java.util.Objects;
import java.util.function.Predicate;

final class FilterByFileFormatterStep implements FormatterStep {
private final FormatterStep delegateStep;
private final Predicate<File> filter;

FilterByFileFormatterStep(FormatterStep delegateStep, Predicate<File> filter) {
this.delegateStep = delegateStep;
this.filter = filter;
}

@Override
public String getName() {
return delegateStep.getName();
}

@Override
public String format(String raw, File file) throws Throwable {
if (filter.test(file)) {
return delegateStep.format(raw, file);
} else {
return raw;
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FilterByFileFormatterStep that = (FilterByFileFormatterStep) o;
return Objects.equals(delegateStep, that.delegateStep) &&
Objects.equals(filter, that.filter);
}

@Override
public int hashCode() {
return Objects.hash(delegateStep, filter);
}

private static final long serialVersionUID = 1L;
}
53 changes: 37 additions & 16 deletions src/main/java/com/diffplug/gradle/spotless/FormatExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import com.diffplug.common.collect.ImmutableMap;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.file.UnionFileCollection;

Expand Down Expand Up @@ -100,7 +102,7 @@ public void encoding(String charset) {
/**
* FileCollections pass through raw.
* Strings are treated as the 'include' arg to fileTree, with project.rootDir as the dir.
* List<String> are treates as the 'includes' arg to fileTree, with project.rootDir as the dir.
* List<String> are treated as the 'includes' arg to fileTree, with project.rootDir as the dir.
* Anything else gets passed to getProject().files().
*/
public void target(Object... targets) {
Expand All @@ -109,7 +111,7 @@ public void target(Object... targets) {
} else if (targets.length == 1) {
this.target = parseTarget(targets[0]);
} else {
if (Arrays.stream(targets).allMatch(o -> o instanceof String)) {
if (Stream.of(targets).allMatch(o -> o instanceof String)) {
this.target = parseTarget(Arrays.asList(targets));
} else {
UnionFileCollection union = new UnionFileCollection();
Expand All @@ -125,14 +127,20 @@ protected FileCollection parseTarget(Object target) {
if (target instanceof FileCollection) {
return (FileCollection) target;
} else if (target instanceof String) {
Map<String, Object> args = new HashMap<>();
args.put("dir", getProject().getProjectDir());
args.put("include", target);
// ConfigurableFileCollection excludes = getProject().files(
// getProject().getBuildDir(),
// getProject().absoluteProjectPath(".gradle/"));
Map<String, Object> args = ImmutableMap.of(
"dir", getProject().getProjectDir(),
"include", target);
return getProject().fileTree(args);
} else if (target instanceof List && ((List<?>) target).stream().allMatch(o -> o instanceof String)) {
Map<String, Object> args = new HashMap<>();
args.put("dir", getProject().getProjectDir());
args.put("includes", target);
// ConfigurableFileCollection excludes = getProject().files(
// getProject().getBuildDir(),
// getProject().absoluteProjectPath(".gradle/"));
Map<String, Object> args = ImmutableMap.of(
"dir", getProject().getProjectDir(),
"includes", target);
return getProject().fileTree(args);
} else {
return getProject().files(target);
Expand Down Expand Up @@ -269,13 +277,26 @@ public void licenseHeaderFile(Object licenseHeaderFile, String delimiter) {
customLazy(LicenseHeaderStep.NAME, () -> new LicenseHeaderStep(getProject().file(licenseHeaderFile), getEncoding(), delimiter)::format);
}

/** Sets up a FormatTask according to the values in this extension. */
protected void setupTask(FormatTask task) throws Exception {
task.paddedCell = paddedCell;
task.lineEndingPolicy = getLineEndingPolicy();
task.encoding = getEncoding();
task.target = target;
task.steps = steps;
/** Sets up a CheckFormatTask and an ApplyFormatTask according to the values in this extension. */
protected void setupTasks(CheckFormatTask checkTask, ApplyFormatTask applyTask) {
setupCheckTask(checkTask);
setupApplyTask(applyTask);
}

protected void setupCheckTask(CheckFormatTask checkTask) {
checkTask.paddedCell = paddedCell;
checkTask.lineEndingPolicy = getLineEndingPolicy();
checkTask.encoding = getEncoding();
checkTask.target = target;
checkTask.steps = steps;
}

protected void setupApplyTask(ApplyFormatTask applyTask) {
applyTask.paddedCell = paddedCell;
applyTask.lineEndingPolicy = getLineEndingPolicy();
applyTask.encoding = getEncoding();
applyTask.target = target;
applyTask.steps = steps;
}

/** Returns the project that this extension is attached to. */
Expand Down
Loading

0 comments on commit 6061363

Please sign in to comment.