Skip to content

Commit

Permalink
Fix for Spotless bug #144.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Sep 25, 2017
1 parent f9fdada commit 8f2fa20
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import com.diffplug.common.base.Errors;
import com.diffplug.common.base.Preconditions;
import com.diffplug.common.io.Files;

class SerializableMisc {
static void toFile(Serializable obj, File file) {
try {
java.nio.file.Files.createDirectories(file.getParentFile().toPath());
try (OutputStream output = Files.asByteSink(file).openBufferedStream()) {
toStream(obj, output);
}
} catch (IOException e) {
throw Errors.asRuntime(e);
}
}

static <T extends Serializable> T fromFile(Class<T> clazz, File file) {
try (InputStream input = Files.asByteSource(file).openBufferedStream()) {
return fromStream(clazz, input);
} catch (IOException e) {
throw Errors.asRuntime(e);
}
}

static void toStream(Serializable obj, OutputStream stream) {
try (ObjectOutputStream objectOutput = new ObjectOutputStream(stream)) {
objectOutput.writeObject(obj);
} catch (IOException e) {
throw Errors.asRuntime(e);
}
}

@SuppressWarnings("unchecked")
static <T> T fromStream(Class<T> clazz, InputStream stream) {
try (ObjectInputStream objectInput = new ObjectInputStream(stream)) {
T object = (T) objectInput.readObject();
Preconditions.checkArgument(clazz.isInstance(object), "Requires class %s, was %s", clazz, object);
return object;
} catch (ClassNotFoundException | IOException e) {
throw Errors.asRuntime(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
Expand All @@ -32,10 +33,11 @@
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 com.diffplug.common.collect.ImmutableList;
import com.diffplug.common.collect.Iterables;
import com.diffplug.spotless.FormatExceptionPolicy;
import com.diffplug.spotless.FormatExceptionPolicyStrict;
import com.diffplug.spotless.Formatter;
Expand Down Expand Up @@ -93,8 +95,6 @@ public FormatExceptionPolicy getExceptionPolicy() {

protected Iterable<File> target;

@InputFiles
@SkipWhenEmpty
public Iterable<File> getTarget() {
return target;
}
Expand All @@ -103,6 +103,18 @@ public void setTarget(Iterable<File> target) {
this.target = requireElementsNonNull(target);
}

/** Internal use only. */
@InputFiles
@Deprecated
public Iterable<File> getInternalTarget() {
// used to combine the special cache file and the real target
return Iterables.concat(ImmutableList.of(getCacheFile()), target);
}

private File getCacheFile() {
return new File(getProject().getBuildDir(), getName());
}

protected List<FormatterStep> steps = new ArrayList<>();

@Input
Expand Down Expand Up @@ -160,30 +172,64 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
List<File> outOfDate = new ArrayList<>();
inputs.outOfDate(inputDetails -> {
File file = inputDetails.getFile();
if (file.isFile()) {
if (file.isFile() && !file.equals(getCacheFile())) {
outOfDate.add(file);
}
});
// load the files that were changed by the last run
// because it's possible the user changed them back to their
// unformatted form, so we need to treat them as dirty
// (see bug #144)
if (getCacheFile().exists()) {
LastApply lastApply = SerializableMisc.fromFile(LastApply.class, getCacheFile());
for (File file : lastApply.changedFiles) {
if (!outOfDate.contains(file) && file.exists()) {
outOfDate.add(file);
}
}
}

if (apply) {
apply(formatter, outOfDate);
List<File> changedFiles = applyAnyChanged(formatter, outOfDate);
if (!changedFiles.isEmpty()) {
// If any file changed, we need to mark the task as dirty
// next time to avoid bug #144.
LastApply lastApply = new LastApply();
lastApply.timestamp = System.currentTimeMillis();
lastApply.changedFiles = changedFiles;

SerializableMisc.toFile(lastApply, getCacheFile());
}
}
if (check) {
check(formatter, outOfDate);
}
}

private void apply(Formatter formatter, List<File> outOfDate) throws Exception {
static class LastApply implements Serializable {
private static final long serialVersionUID = 6245070824310295090L;

long timestamp;
List<File> changedFiles;
}

private List<File> applyAnyChanged(Formatter formatter, List<File> outOfDate) throws Exception {
List<File> changed = new ArrayList<>(outOfDate.size());
if (isPaddedCell()) {
for (File file : outOfDate) {
getLogger().debug("Applying format to " + file);
PaddedCellBulk.apply(formatter, file);
if (PaddedCellBulk.applyAnyChanged(formatter, file)) {
changed.add(file);
}
}
} else {
boolean anyMisbehave = false;
for (File file : outOfDate) {
getLogger().debug("Applying format to " + file);
String unixResultIfDirty = formatter.applyToAndReturnResultIfDirty(file);
if (unixResultIfDirty != null) {
changed.add(file);
}
// because apply will count as up-to-date, it's important
// that every call to apply will get a PaddedCell check
if (!anyMisbehave && unixResultIfDirty != null) {
Expand All @@ -207,6 +253,7 @@ private void apply(Formatter formatter, List<File> outOfDate) throws Exception {
throw PaddedCellGradle.youShouldTurnOnPaddedCell(this);
}
}
return changed;
}

private void check(Formatter formatter, List<File> outOfDate) throws Exception {
Expand Down

0 comments on commit 8f2fa20

Please sign in to comment.