|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + */ |
| 5 | +package org.eolang.maven; |
| 6 | + |
| 7 | +import com.jcabi.log.Logger; |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.URISyntaxException; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.nio.file.Paths; |
| 13 | +import java.util.Comparator; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +/** |
| 17 | + * Invalidates the local EO cache on every SNAPSHOT build. |
| 18 | + * Released versions use version-keyed cache directories that never collide, |
| 19 | + * so no invalidation is needed for them. |
| 20 | + * @see <a href="https://github.com/objectionary/eo/issues/5179">issue #5179</a> |
| 21 | + * @since 0.62.0 |
| 22 | + */ |
| 23 | +final class Janitor { |
| 24 | + |
| 25 | + /** |
| 26 | + * Fingerprint file stored inside the cache root. |
| 27 | + */ |
| 28 | + private static final String FINGERPRINT = ".snapshot-fingerprint"; |
| 29 | + |
| 30 | + /** |
| 31 | + * Stage directories that share the versioned cache layout. |
| 32 | + */ |
| 33 | + private static final String[] STAGES = {"parsed", "transpiled", "linted", "pulled"}; |
| 34 | + |
| 35 | + /** |
| 36 | + * Base cache directory, e.g. ~/.eo/. |
| 37 | + */ |
| 38 | + private final Path cache; |
| 39 | + |
| 40 | + /** |
| 41 | + * Current plugin version, e.g. "1.0-SNAPSHOT". |
| 42 | + */ |
| 43 | + private final String version; |
| 44 | + |
| 45 | + /** |
| 46 | + * Ctor. |
| 47 | + * @param cache Base cache directory |
| 48 | + * @param version Plugin version |
| 49 | + */ |
| 50 | + Janitor(final Path cache, final String version) { |
| 51 | + this.cache = cache; |
| 52 | + this.version = version; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Wipes all SNAPSHOT stage caches on every build. |
| 57 | + * Does nothing for released versions. |
| 58 | + */ |
| 59 | + void clean() { |
| 60 | + if (this.version.contains("SNAPSHOT")) { |
| 61 | + try { |
| 62 | + this.invalidate(); |
| 63 | + } catch (final IOException | URISyntaxException ex) { |
| 64 | + throw new IllegalStateException("Failed to clean SNAPSHOT cache", ex); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Checks the fingerprint and wipes caches if it has changed. |
| 71 | + * @throws IOException If an IO operation fails |
| 72 | + * @throws URISyntaxException If the code source location URI is malformed |
| 73 | + */ |
| 74 | + private void invalidate() throws IOException, URISyntaxException { |
| 75 | + final String current = String.valueOf(Janitor.mtime()); |
| 76 | + final Path fingerprint = this.cache.resolve(Janitor.FINGERPRINT); |
| 77 | + if (!Files.exists(fingerprint) |
| 78 | + || !Files.readString(fingerprint).equals(current)) { |
| 79 | + this.wipeCaches(); |
| 80 | + new Saved(current, fingerprint).value(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Deletes the versioned cache directory for each stage if it exists. |
| 86 | + * @throws IOException If deletion fails |
| 87 | + */ |
| 88 | + private void wipeCaches() throws IOException { |
| 89 | + for (final String stage : Janitor.STAGES) { |
| 90 | + final Path dir = this.cache.resolve(stage).resolve(this.version); |
| 91 | + if (Files.exists(dir)) { |
| 92 | + Logger.info( |
| 93 | + this, |
| 94 | + "Plugin code changed, invalidating SNAPSHOT cache at %[file]s", |
| 95 | + dir |
| 96 | + ); |
| 97 | + Janitor.wipe(dir); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns the modification time of the plugin JAR or classes directory. |
| 104 | + * Used as a build fingerprint: changes on every build, so comparing it |
| 105 | + * to the stored value always detects a new build. |
| 106 | + * @return Last-modified timestamp in milliseconds |
| 107 | + * @throws URISyntaxException If the code source location URI is malformed |
| 108 | + * @throws IOException If reading the directory fails |
| 109 | + */ |
| 110 | + private static long mtime() throws URISyntaxException, IOException { |
| 111 | + final Path location = Paths.get( |
| 112 | + Janitor.class.getProtectionDomain().getCodeSource().getLocation().toURI() |
| 113 | + ); |
| 114 | + final long result; |
| 115 | + if (Files.isDirectory(location)) { |
| 116 | + try (Stream<Path> stream = Files.walk(location)) { |
| 117 | + result = stream.filter(Files::isRegularFile) |
| 118 | + .mapToLong(f -> f.toFile().lastModified()) |
| 119 | + .max() |
| 120 | + .orElse(0L); |
| 121 | + } |
| 122 | + } else { |
| 123 | + result = location.toFile().lastModified(); |
| 124 | + } |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Recursively deletes a directory. |
| 130 | + * @param dir Directory to delete |
| 131 | + * @throws IOException If deletion fails |
| 132 | + */ |
| 133 | + private static void wipe(final Path dir) throws IOException { |
| 134 | + try (Stream<Path> stream = Files.walk(dir)) { |
| 135 | + stream.sorted(Comparator.reverseOrder()).forEach( |
| 136 | + path -> { |
| 137 | + try { |
| 138 | + Files.delete(path); |
| 139 | + } catch (final IOException ex) { |
| 140 | + throw new IllegalStateException( |
| 141 | + String.format("Failed to delete %s", path), ex |
| 142 | + ); |
| 143 | + } |
| 144 | + } |
| 145 | + ); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments