|
| 1 | +/* |
| 2 | + * Copyright 2022 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.io.File; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.OutputStream; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.util.Arrays; |
| 23 | + |
| 24 | +import javax.annotation.Nullable; |
| 25 | + |
| 26 | +/** |
| 27 | + * The clean/dirty state of a single file. Intended use: |
| 28 | + * - {@link #isClean()} means that the file is is clean, and there's nothing else to say |
| 29 | + * - {@link #didNotConverge()} means that we were unable to determine a clean state |
| 30 | + * - once you've tested the above conditions and you know that it's a dirty file with a converged state, |
| 31 | + * then you can call {@link #writeCanonicalTo(OutputStream)} to get the canonical form of the given file. |
| 32 | + */ |
| 33 | +public class DirtyState { |
| 34 | + @Nullable |
| 35 | + private final byte[] canonicalBytes; |
| 36 | + |
| 37 | + DirtyState(@Nullable byte[] canonicalBytes) { |
| 38 | + this.canonicalBytes = canonicalBytes; |
| 39 | + } |
| 40 | + |
| 41 | + public boolean isClean() { |
| 42 | + return this == isClean; |
| 43 | + } |
| 44 | + |
| 45 | + public boolean didNotConverge() { |
| 46 | + return this == didNotConverge; |
| 47 | + } |
| 48 | + |
| 49 | + private byte[] canonicalBytes() { |
| 50 | + if (canonicalBytes == null) { |
| 51 | + throw new IllegalStateException("First make sure that {@code !isClean()} and {@code !didNotConverge()}"); |
| 52 | + } |
| 53 | + return canonicalBytes; |
| 54 | + } |
| 55 | + |
| 56 | + public void writeCanonicalTo(File file) throws IOException { |
| 57 | + Files.write(file.toPath(), canonicalBytes()); |
| 58 | + } |
| 59 | + |
| 60 | + public void writeCanonicalTo(OutputStream out) throws IOException { |
| 61 | + out.write(canonicalBytes()); |
| 62 | + } |
| 63 | + |
| 64 | + /** Returns the DirtyState which corresponds to {@code isClean()}. */ |
| 65 | + public static DirtyState clean() { |
| 66 | + return isClean; |
| 67 | + } |
| 68 | + |
| 69 | + static final DirtyState didNotConverge = new DirtyState(null); |
| 70 | + static final DirtyState isClean = new DirtyState(null); |
| 71 | + |
| 72 | + public static Calculation of(Formatter formatter, File file) throws IOException { |
| 73 | + return of(formatter, file, Files.readAllBytes(file.toPath())); |
| 74 | + } |
| 75 | + |
| 76 | + public static Calculation of(Formatter formatter, File file, byte[] rawBytes) { |
| 77 | + return new Calculation(formatter, file, rawBytes); |
| 78 | + } |
| 79 | + |
| 80 | + public static class Calculation { |
| 81 | + private final Formatter formatter; |
| 82 | + private final File file; |
| 83 | + private final byte[] rawBytes; |
| 84 | + private final String raw; |
| 85 | + |
| 86 | + private Calculation(Formatter formatter, File file, byte[] rawBytes) { |
| 87 | + this.formatter = formatter; |
| 88 | + this.file = file; |
| 89 | + this.rawBytes = rawBytes; |
| 90 | + this.raw = new String(rawBytes, formatter.getEncoding()); |
| 91 | + // check that all characters were encodable |
| 92 | + String encodingError = EncodingErrorMsg.msg(raw, rawBytes, formatter.getEncoding()); |
| 93 | + if (encodingError != null) { |
| 94 | + throw new IllegalArgumentException(encodingError); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Calculates whether the given file is dirty according to a PaddedCell invocation of the given formatter. |
| 100 | + * DirtyState includes the clean state of the file, as well as a warning if we were not able to apply the formatter |
| 101 | + * due to diverging idempotence. |
| 102 | + */ |
| 103 | + public DirtyState calculateDirtyState() { |
| 104 | + String rawUnix = LineEnding.toUnix(raw); |
| 105 | + |
| 106 | + // enforce the format |
| 107 | + String formattedUnix = formatter.compute(rawUnix, file); |
| 108 | + // convert the line endings if necessary |
| 109 | + String formatted = formatter.computeLineEndings(formattedUnix, file); |
| 110 | + |
| 111 | + // if F(input) == input, then the formatter is well-behaving and the input is clean |
| 112 | + byte[] formattedBytes = formatted.getBytes(formatter.getEncoding()); |
| 113 | + if (Arrays.equals(rawBytes, formattedBytes)) { |
| 114 | + return isClean; |
| 115 | + } |
| 116 | + |
| 117 | + // F(input) != input, so we'll do a padded check |
| 118 | + String doubleFormattedUnix = formatter.compute(formattedUnix, file); |
| 119 | + if (doubleFormattedUnix.equals(formattedUnix)) { |
| 120 | + // most dirty files are idempotent-dirty, so this is a quick-short circuit for that common case |
| 121 | + return new DirtyState(formattedBytes); |
| 122 | + } |
| 123 | + |
| 124 | + PaddedCell cell = PaddedCell.check(formatter, file, rawUnix); |
| 125 | + if (!cell.isResolvable()) { |
| 126 | + return didNotConverge; |
| 127 | + } |
| 128 | + |
| 129 | + // get the canonical bytes |
| 130 | + String canonicalUnix = cell.canonical(); |
| 131 | + String canonical = formatter.computeLineEndings(canonicalUnix, file); |
| 132 | + byte[] canonicalBytes = canonical.getBytes(formatter.getEncoding()); |
| 133 | + if (!Arrays.equals(rawBytes, canonicalBytes)) { |
| 134 | + // and write them to disk if needed |
| 135 | + return new DirtyState(canonicalBytes); |
| 136 | + } else { |
| 137 | + return isClean; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments