|
| 1 | +/* |
| 2 | + * Copyright 2024 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +/** |
| 24 | + * Gradle requires three things: |
| 25 | + * - Gradle defines cache equality based on your serialized representation |
| 26 | + * - Combined with remote build cache, you cannot have any absolute paths in |
| 27 | + * your serialized representation |
| 28 | + * - Combined with configuration cache, you must be able to roundtrip yourself |
| 29 | + * through serialization |
| 30 | + * |
| 31 | + * These requirements are at odds with each other, as described in these issues |
| 32 | + * - Gradle issue to define custom equality |
| 33 | + * https://github.com/gradle/gradle/issues/29816 |
| 34 | + * - Spotless plea for developer cache instead of configuration cache |
| 35 | + * https://github.com/diffplug/spotless/issues/987 |
| 36 | + * - Spotless cache miss bug fixed by this class |
| 37 | + * https://github.com/diffplug/spotless/issues/2168 |
| 38 | + * |
| 39 | + * This class is a `List<FormatterStep>` which can optimize the |
| 40 | + * serialized representation for either |
| 41 | + * - roundtrip integrity |
| 42 | + * - OR |
| 43 | + * - equality |
| 44 | + * |
| 45 | + * Because it is not possible to provide both at the same time. |
| 46 | + * It is a horrific hack, but it works, and it's the only way I can figure |
| 47 | + * to make Spotless work with all of Gradle's cache systems at once. |
| 48 | + */ |
| 49 | +public class ConfigurationCacheHackList implements java.io.Serializable { |
| 50 | + private final boolean optimizeForEquality; |
| 51 | + private final ArrayList<Object> backingList = new ArrayList<>(); |
| 52 | + |
| 53 | + public static ConfigurationCacheHackList forEquality() { |
| 54 | + return new ConfigurationCacheHackList(true); |
| 55 | + } |
| 56 | + |
| 57 | + public static ConfigurationCacheHackList forRoundtrip() { |
| 58 | + return new ConfigurationCacheHackList(false); |
| 59 | + } |
| 60 | + |
| 61 | + private ConfigurationCacheHackList(boolean optimizeForEquality) { |
| 62 | + this.optimizeForEquality = optimizeForEquality; |
| 63 | + } |
| 64 | + |
| 65 | + public void clear() { |
| 66 | + backingList.clear(); |
| 67 | + } |
| 68 | + |
| 69 | + public void addAll(Collection<? extends FormatterStep> c) { |
| 70 | + for (FormatterStep step : c) { |
| 71 | + if (step instanceof FormatterStepSerializationRoundtrip) { |
| 72 | + var clone = ((FormatterStepSerializationRoundtrip) step).hackClone(optimizeForEquality); |
| 73 | + backingList.add(clone); |
| 74 | + } else { |
| 75 | + backingList.add(step); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public List<FormatterStep> getSteps() { |
| 81 | + var result = new ArrayList<FormatterStep>(backingList.size()); |
| 82 | + for (Object obj : backingList) { |
| 83 | + if (obj instanceof FormatterStepSerializationRoundtrip.HackClone) { |
| 84 | + result.add(((FormatterStepSerializationRoundtrip.HackClone) obj).rehydrate()); |
| 85 | + } else { |
| 86 | + result.add((FormatterStep) obj); |
| 87 | + } |
| 88 | + } |
| 89 | + return result; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean equals(Object o) { |
| 94 | + if (this == o) |
| 95 | + return true; |
| 96 | + if (o == null || getClass() != o.getClass()) |
| 97 | + return false; |
| 98 | + ConfigurationCacheHackList stepList = (ConfigurationCacheHackList) o; |
| 99 | + return optimizeForEquality == stepList.optimizeForEquality && |
| 100 | + backingList.equals(stepList.backingList); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public int hashCode() { |
| 105 | + return Objects.hash(optimizeForEquality, backingList); |
| 106 | + } |
| 107 | +} |
0 commit comments