-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intermediate commit - compiles but SelfTest fails!
- Loading branch information
Showing
20 changed files
with
453 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
52
src/main/java/com/diffplug/gradle/spotless/ApplyFormatTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
98
src/main/java/com/diffplug/gradle/spotless/CheckFormatTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/diffplug/gradle/spotless/FilterByFileFormatterStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.