Skip to content
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

Remove JvmLocalCache now that everything is round-trippable on its own #2088

Merged
merged 8 commits into from
Apr 7, 2024

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public LineEnding getLineEndings() {
}

public void setLineEndings(LineEnding lineEndings) {
if (lineEndings == LineEnding.GIT_ATTRIBUTES) {
throw new IllegalArgumentException("GIT_ATTRIBUTES not supported in Gradle, use GIT_ATTRIBUTES_FAST_ALLSAME instead. See https://github.com/diffplug/spotless/issues/1274 for more details.");
}
this.lineEndings = requireNonNull(lineEndings);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 DiffPlug
* Copyright 2020-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 @@ -37,7 +37,6 @@
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.work.Incremental;

import com.diffplug.gradle.spotless.JvmLocalCache.LiveCache;
import com.diffplug.spotless.FormatExceptionPolicy;
import com.diffplug.spotless.FormatExceptionPolicyStrict;
import com.diffplug.spotless.Formatter;
Expand All @@ -49,10 +48,6 @@ public abstract class SpotlessTask extends DefaultTask {
@Internal
abstract Property<SpotlessTaskService> getTaskService();

protected <T> LiveCache<T> createLive(String keyName) {
return JvmLocalCache.createLive(this, keyName);
}

// set by SpotlessExtension, but possibly overridden by FormatExtension
protected String encoding = "UTF-8";

Expand All @@ -65,15 +60,15 @@ public void setEncoding(String encoding) {
this.encoding = Objects.requireNonNull(encoding);
}

protected final LiveCache<Provider<LineEnding.Policy>> lineEndingsPolicy = createLive("lineEndingsPolicy");
protected Provider<LineEnding.Policy> lineEndingsPolicy = null;

@Input
public Provider<LineEnding.Policy> getLineEndingsPolicy() {
return lineEndingsPolicy.get();
return lineEndingsPolicy;
}

public void setLineEndingsPolicy(Provider<LineEnding.Policy> lineEndingsPolicy) {
this.lineEndingsPolicy.set(lineEndingsPolicy);
this.lineEndingsPolicy = lineEndingsPolicy;
}

/** The sha of the tree at repository root, used for determining if an individual *file* is clean according to git. */
Expand Down Expand Up @@ -155,22 +150,17 @@ public File getOutputDirectory() {
return outputDirectory;
}

protected final LiveCache<List<FormatterStep>> steps = createLive("steps");
{
steps.set(new ArrayList<FormatterStep>());
}
protected final List<FormatterStep> steps = new ArrayList<>();

@Input
public List<FormatterStep> getSteps() {
return Collections.unmodifiableList(steps.get());
return Collections.unmodifiableList(steps);
}

public void setSteps(List<FormatterStep> steps) {
this.steps.set(PluginGradlePreconditions.requireElementsNonNull(steps));
}

public boolean addStep(FormatterStep step) {
return this.steps.get().add(Objects.requireNonNull(step));
this.steps.clear();
PluginGradlePreconditions.requireElementsNonNull(steps);
nedtwigg marked this conversation as resolved.
Show resolved Hide resolved
this.steps.addAll(steps);
}

/** Returns the name of this format. */
Expand All @@ -186,10 +176,10 @@ String formatName() {
Formatter buildFormatter() {
return Formatter.builder()
.name(formatName())
.lineEndingsPolicy(lineEndingsPolicy.get().get())
.lineEndingsPolicy(getLineEndingsPolicy().get())
.encoding(Charset.forName(encoding))
.rootDir(getProjectDir().get().getAsFile().toPath())
.steps(steps.get())
.steps(steps)
.exceptionPolicy(exceptionPolicy)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ void failureWhenExeNotFound() throws Exception {
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").buildAndFail();
assertThat(spotlessApply.getOutput()).contains("Build failed with an exception");
assertFile("biome_test.js").sameAsResource("biome/js/fileBefore.js");
assertThat(spotlessApply.getOutput()).contains("Could not create task ':spotlessMybiomeApply'");
assertThat(spotlessApply.getOutput()).contains("Execution failed for task ':spotlessMybiome'");
assertThat(spotlessApply.getOutput()).contains("Biome executable does not exist");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 DiffPlug
* Copyright 2020-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 @@ -17,11 +17,8 @@

import java.io.IOException;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;

public class ConfigurationCacheTest extends GradleIntegrationHarness {
@Override
Expand Down Expand Up @@ -65,8 +62,7 @@ public void helpConfiguresIfTasksAreCreated() throws IOException {
}

@Test
@EnabledForJreRange(max = JRE.JAVA_20)
public void jvmLocalCache() throws IOException {
public void multipleRuns() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
Expand All @@ -93,14 +89,5 @@ public void jvmLocalCache() throws IOException {
gradleRunner().withArguments("spotlessCheck").buildAndFail();
gradleRunner().withArguments("spotlessApply").build();
assertFile("test.java").sameAsResource("java/googlejavaformat/JavaCodeFormatted.test");

// the withDebug forces it to start a new deamon, but only in Gradle 8.3 and older
// starting with Gradle 8.5 this doesn't work anymore
// and we need Gradle 8.5 for Java 21
// so we can't test this on Java 21 for now
BuildResult failure = gradleRunner().withDebug(true).withArguments("spotlessApply", "--stacktrace").buildAndFail();
failure.getOutput().contains("Spotless daemon-local cache is stale. Regenerate the cache with\n" +
" rm -rf .gradle/configuration-cache\n" +
"For more information see #123\n");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 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 Down Expand Up @@ -151,10 +151,10 @@ void customRunToFixMessage() throws Exception {
@Test
void whitespaceProblem() throws Exception {
Bundle spotless = create(setFile("testFile").toContent("A \nB\t\nC \n"));
spotless.task.addStep(FormatterStep.createNeverUpToDate("trimTrailing", input -> {
spotless.task.setSteps(List.of(FormatterStep.createNeverUpToDate("trimTrailing", input -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tempted to revert this code change and all other addStep -> setStep changes. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see an argument for setSteps(FormatterStep... steps) to remove the manual List.of, but I think the mutable-naming addStep is bad. It's used only for a few tests, which is why I just did the List.of.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough! I have no strong opinions either way, so I'm happy to continue with setSteps.

Pattern pattern = Pattern.compile("[ \t]+$", Pattern.UNIX_LINES | Pattern.MULTILINE);
return pattern.matcher(input).replaceAll("");
}));
})));
assertCheckFailure(spotless,
" testFile",
" @@ -1,3 +1,3 @@",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 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 @@ -17,6 +17,7 @@

import java.io.File;
import java.util.Collections;
import java.util.List;

import org.gradle.api.Project;
import org.gradle.api.services.BuildServiceParameters;
Expand Down Expand Up @@ -61,7 +62,7 @@ void testStep() throws Exception {
File outputFile = new File(spotlessTask.getOutputDirectory(), "testFile");
spotlessTask.setTarget(Collections.singleton(testFile));

spotlessTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
spotlessTask.setSteps(List.of(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p"))));
Tasks.execute(spotlessTask);

assertFile(outputFile).hasContent("aple");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 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 @@ -21,6 +21,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.gradle.api.Project;
import org.gradle.api.provider.Provider;
Expand Down Expand Up @@ -65,7 +66,7 @@ public BuildServiceParameters.None getParameters() {
private SpotlessTaskImpl createFormatTask(String name, FormatterStep step) {
SpotlessTaskImpl task = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name), SpotlessTaskImpl.class);
task.init(taskService);
task.addStep(step);
task.setSteps(List.of(step));
task.setLineEndingsPolicy(project.provider(LineEnding.UNIX::createPolicy));
task.setTarget(Collections.singletonList(file));
return task;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 DiffPlug
* Copyright 2023-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 @@ -310,7 +310,7 @@ void failureWhenExeNotFound() throws Exception {
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").buildAndFail();
assertThat(spotlessApply.getOutput()).contains("Build failed with an exception");
assertFile("rome_test.js").sameAsResource("rome/js/fileBefore.js");
assertThat(spotlessApply.getOutput()).contains("Could not create task ':spotlessMyromeApply'");
assertThat(spotlessApply.getOutput()).contains("Execution failed for task ':spotlessMyrome'");
assertThat(spotlessApply.getOutput()).contains("Biome executable does not exist");
}

Expand Down
Loading