Skip to content
Closed
4 changes: 4 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/FileSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public static Promised promise(Iterable<File> files) {
return new Promised(MoreIterables.toNullHostileList(files), null);
}

public static Promised promise(File file) {
return new Promised(MoreIterables.toNullHostileList(Collections.singletonList(file)), null);
}

/** Returns all of the files in this signature, throwing an exception if there are more or less than 1 file. */
public Collection<File> files() {
return Collections.unmodifiableList(files);
Expand Down
6 changes: 6 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,10 @@ public void close() {

/** This Sentinel reference may be used to pass string content to a Formatter or FormatterStep when there is no actual File to format */
public static final File NO_FILE_SENTINEL = new File("NO_FILE_SENTINEL");

static void checkNotSentinel(File file) {
if (file == Formatter.NO_FILE_SENTINEL) {
throw new IllegalArgumentException("This step requires the underlying file. If this is a test, use StepHarnessWithFile");
}
}
}
4 changes: 2 additions & 2 deletions lib/src/main/java/com/diffplug/spotless/FormatterFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void close() {

@Override
public String apply(String unix, File file) throws Exception {
FormatterStepImpl.checkNotSentinel(file);
Formatter.checkNotSentinel(file);
return function.apply(resource, unix, file);
}

Expand Down Expand Up @@ -144,7 +144,7 @@ interface NeedsFile extends FormatterFunc {

@Override
default String apply(String unix, File file) throws Exception {
FormatterStepImpl.checkNotSentinel(file);
Formatter.checkNotSentinel(file);
return applyWithFile(unix, file);
}

Expand Down
30 changes: 4 additions & 26 deletions lib/src/main/java/com/diffplug/spotless/FormatterStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,6 @@ default FormatterStep filterByFile(SerializableFileFilter filter) {
return new FilterByFileFormatterStep(this, filter);
}

/**
* Implements a FormatterStep in a strict way which guarantees correct and lazy implementation
* of up-to-date checks. This maximizes performance for cases where the FormatterStep is not
* actually needed (e.g. don't load eclipse setting file unless this step is actually running)
* while also ensuring that Gradle can detect changes in a step's settings to determine that
* it needs to rerun a format.
*/
abstract class Strict<State extends Serializable> extends LazyForwardingEquality<State> implements FormatterStep {
private static final long serialVersionUID = 1L;

/**
* Implements the formatting function strictly in terms
* of the input data and the result of {@link #calculateState()}.
*/
protected abstract String format(State state, String rawUnix, File file) throws Exception;

@Override
public final String format(String rawUnix, File file) throws Exception {
return format(state(), rawUnix, file);
}
}

/**
* @param name
* The name of the formatter step.
Expand Down Expand Up @@ -151,8 +129,8 @@ static <RoundtripState extends Serializable, EqualityState extends Serializable>
static <State extends Serializable> FormatterStep createLazy(
String name,
ThrowingEx.Supplier<State> stateSupplier,
ThrowingEx.Function<State, FormatterFunc> stateToFormatter) {
return new FormatterStepImpl.Standard<>(name, stateSupplier, stateToFormatter);
SerializedFunction<State, FormatterFunc> stateToFormatter) {
return createLazy(name, stateSupplier, SerializedFunction.identity(), stateToFormatter);
}

/**
Expand All @@ -168,7 +146,7 @@ static <State extends Serializable> FormatterStep createLazy(
static <State extends Serializable> FormatterStep create(
String name,
State state,
ThrowingEx.Function<State, FormatterFunc> stateToFormatter) {
SerializedFunction<State, FormatterFunc> stateToFormatter) {
Objects.requireNonNull(state, "state");
return createLazy(name, () -> state, stateToFormatter);
}
Expand All @@ -185,7 +163,7 @@ static <State extends Serializable> FormatterStep create(
static FormatterStep createNeverUpToDateLazy(
String name,
ThrowingEx.Supplier<FormatterFunc> functionSupplier) {
return new FormatterStepImpl.NeverUpToDate(name, functionSupplier);
return new NeverUpToDateStep(name, functionSupplier);
}

/**
Expand Down
133 changes: 0 additions & 133 deletions lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java

This file was deleted.

59 changes: 59 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/NeverUpToDateStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2024 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.spotless;

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

/**
* Formatter which is equal to itself, but not to any other Formatter.
*/
class NeverUpToDateStep implements FormatterStep {
private static final long serialVersionUID = 1L;

private final String name;
private final ThrowingEx.Supplier<FormatterFunc> formatterSupplier;
private transient FormatterFunc formatter; // initialized lazily

NeverUpToDateStep(String name, ThrowingEx.Supplier<FormatterFunc> formatterSupplier) {
this.name = name;
this.formatterSupplier = Objects.requireNonNull(formatterSupplier, "formatterSupplier");
}

@Override
public String getName() {
return name;
}

@Override
public String format(String rawUnix, File file) throws Exception {
if (formatter == null) {
formatter = formatterSupplier.get();
if (formatter instanceof FormatterFunc.Closeable) {
throw new AssertionError("NeverUpToDate does not support FormatterFunc.Closeable. See https://github.com/diffplug/spotless/pull/284");
}
}
return formatter.apply(rawUnix, file);
}

@Override
public void close() throws Exception {
if (formatter instanceof FormatterFunc.Closeable) {
((FormatterFunc.Closeable) formatter).close();
formatter = null;
}
}
}
25 changes: 19 additions & 6 deletions lib/src/main/java/com/diffplug/spotless/generic/NativeCmdStep.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,24 +35,37 @@ private NativeCmdStep() {}
public static FormatterStep create(String name, File pathToExe, List<String> arguments) {
Objects.requireNonNull(name, "name");
Objects.requireNonNull(pathToExe, "pathToExe");
return FormatterStep.createLazy(name, () -> new State(FileSignature.signAsList(pathToExe), arguments), State::toFunc);
return FormatterStep.createLazy(name, () -> new State(FileSignature.promise(pathToExe), arguments), State::toRuntime, Runtime::toFunc);
}

static class State implements Serializable {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 2L;
final FileSignature.Promised pathToExe;
final List<String> arguments;

final FileSignature pathToExe;
State(FileSignature.Promised pathToExe, List<String> arguments) {
this.pathToExe = pathToExe;
this.arguments = arguments;
}

Runtime toRuntime() {
return new Runtime(pathToExe.get().getOnlyFile(), arguments);
}
}

static class Runtime implements Serializable {
private static final long serialVersionUID = 2L;
final File pathToExe;
final List<String> arguments;

State(FileSignature pathToExe, List<String> arguments) {
Runtime(File pathToExe, List<String> arguments) {
this.pathToExe = pathToExe;
this.arguments = arguments;
}

String format(ProcessRunner runner, String input) throws IOException, InterruptedException {
List<String> argumentsWithPathToExe = new ArrayList<>();
argumentsWithPathToExe.add(pathToExe.getOnlyFile().getAbsolutePath());
argumentsWithPathToExe.add(pathToExe.getAbsolutePath());
if (arguments != null) {
argumentsWithPathToExe.addAll(arguments);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@
package com.diffplug.spotless.sql;

import java.io.File;
import java.io.Serializable;

import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.FormatterFunc;
Expand All @@ -32,24 +31,14 @@ public class DBeaverSQLFormatterStep {
private DBeaverSQLFormatterStep() {}

public static FormatterStep create(Iterable<File> files) {
return FormatterStep.createLazy(NAME,
() -> new State(files),
State::createFormat);
return FormatterStep.create(NAME, FileSignature.promise(files),
FileSignature.Promised::get,
DBeaverSQLFormatterStep::createFormat);
}

static final class State implements Serializable {
private static final long serialVersionUID = 1L;

final FileSignature settingsSignature;

State(final Iterable<File> settingsFiles) throws Exception {
this.settingsSignature = FileSignature.signAsList(settingsFiles);
}

FormatterFunc createFormat() throws Exception {
FormatterProperties preferences = FormatterProperties.from(settingsSignature.files());
DBeaverSQLFormatter dbeaverSqlFormatter = new DBeaverSQLFormatter(preferences.getProperties());
return dbeaverSqlFormatter::format;
}
private static FormatterFunc createFormat(FileSignature settings) {
FormatterProperties preferences = FormatterProperties.from(settings.files());
DBeaverSQLFormatter dbeaverSqlFormatter = new DBeaverSQLFormatter(preferences.getProperties());
return dbeaverSqlFormatter::format;
}
}
Loading