Skip to content

Fix issue #31 #45

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

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 "com.diffplug.durian:durian-testlib:${VER_DURIAN}"

// add the eclipse jars to the embedded configuration
eclipseDeps.each { embeddedJars "p2:${it}:+" }
Expand Down
40 changes: 40 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,40 @@
/*
* Copyright 2016 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.gradle.spotless;

import java.io.File;
import java.io.IOException;

public class ApplyFormatTask extends BaseFormatTask {
@Override
public void doTask(Formatter formatter) throws Exception {
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);
}
}
;
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/diffplug/gradle/spotless/BaseFormatTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2016 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.gradle.spotless;

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

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;

public abstract class BaseFormatTask extends DefaultTask {
// set by SpotlessExtension, but possibly overridden by FormatExtension
@Input
public Charset encoding = StandardCharsets.UTF_8;
@Input
public LineEnding.Policy lineEndingsPolicy = LineEnding.UNIX_POLICY;
// set by FormatExtension
@Input
public boolean paddedCell = false;
@InputFiles
@SkipWhenEmpty
public Iterable<File> target;
@Input
public List<FormatterStep> steps = new ArrayList<>();

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

doTask(formatter);
}

public abstract void doTask(Formatter formatter) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,17 @@

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

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

public class FormatTask extends DefaultTask {
// set by SpotlessExtension, but possibly overridden by FormatExtension
public Charset encoding = StandardCharsets.UTF_8;
public LineEnding.Policy lineEndingsPolicy = LineEnding.UNIX_POLICY;
// set by FormatExtension
public boolean paddedCell = false;
public Iterable<File> target;
public List<FormatterStep> steps = new ArrayList<>();
// set by plugin
public boolean check = false;

/** Returns the name of this format. */
public String getFormatName() {
String name = getName();
if (name.startsWith(SpotlessPlugin.EXTENSION)) {
String after = name.substring(SpotlessPlugin.EXTENSION.length());
if (after.endsWith(SpotlessPlugin.CHECK)) {
return after.substring(0, after.length() - SpotlessPlugin.CHECK.length()).toLowerCase(Locale.US);
} else if (after.endsWith(SpotlessPlugin.APPLY)) {
return after.substring(0, after.length() - SpotlessPlugin.APPLY.length()).toLowerCase(Locale.US);
}
}
return name;
}

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

// perform the check
if (check) {
formatCheck(formatter);
} else {
formatApply(formatter);
}
public class CheckFormatTask extends BaseFormatTask {
@Override
public void doTask(Formatter formatter) throws Exception {
formatCheck(formatter);
}

/** Checks the format. */
Expand All @@ -75,8 +37,12 @@ private void formatCheck(Formatter formatter) throws IOException {
for (File file : target) {
getLogger().debug("Checking format on " + file);
// keep track of the problem toFormat
if (!formatter.isClean(file)) {
problemFiles.add(file);
try {
if (!formatter.isClean(file)) {
problemFiles.add(file);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

Expand All @@ -99,16 +65,13 @@ GradleException formatViolationsFor(Formatter formatter, List<File> problemFiles
return new GradleException(DiffMessageFormatter.messageFor(this, formatter, problemFiles));
}

/** 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);
}
/** 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,64 @@
/*
* Copyright 2016 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
}
Loading