|
| 1 | +package me.itzg.helpers; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.BufferedWriter; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.*; |
| 9 | +import java.nio.file.attribute.BasicFileAttributes; |
| 10 | + |
| 11 | +import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; |
| 12 | +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; |
| 13 | + |
| 14 | +@Slf4j |
| 15 | +class InterpolatingFileVisitor implements FileVisitor<Path> { |
| 16 | + private final Path src; |
| 17 | + private final Path dest; |
| 18 | + private final boolean skipNewerInDestination; |
| 19 | + private final ReplaceEnvOptions replaceEnv; |
| 20 | + private final Interpolator interpolator; |
| 21 | + |
| 22 | + public InterpolatingFileVisitor(Path src, Path dest, boolean skipNewerInDestination, ReplaceEnvOptions replaceEnv, Interpolator interpolator) { |
| 23 | + this.src = src; |
| 24 | + this.dest = dest; |
| 25 | + this.skipNewerInDestination = skipNewerInDestination; |
| 26 | + this.replaceEnv = replaceEnv; |
| 27 | + this.interpolator = interpolator; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 32 | + log.trace("pre visit dir={}", dir); |
| 33 | + final Path destPath = dest.resolve(src.relativize(dir)); |
| 34 | + |
| 35 | + log.debug("ensuring destinationDirectory={}", destPath); |
| 36 | + Files.createDirectories(destPath); |
| 37 | + |
| 38 | + return FileVisitResult.CONTINUE; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public FileVisitResult visitFile(Path srcFile, BasicFileAttributes attrs) throws IOException { |
| 43 | + log.trace("visit file={}", srcFile); |
| 44 | + final Path destFile = dest.resolve(src.relativize(srcFile)); |
| 45 | + |
| 46 | + if (processFile(srcFile, destFile)) { |
| 47 | + if (replaceEnv.matches(destFile)) { |
| 48 | + log.debug("interpolate from={} to={}", srcFile, destFile); |
| 49 | + |
| 50 | + try (BufferedReader srcReader = Files.newBufferedReader(srcFile)) { |
| 51 | + try (BufferedWriter destWriter = Files.newBufferedWriter(destFile, StandardOpenOption.CREATE)) { |
| 52 | + interpolator.interpolate(srcReader, destWriter); |
| 53 | + } |
| 54 | + } |
| 55 | + } else { |
| 56 | + log.debug("copy from={} to={}", srcFile, destFile); |
| 57 | + |
| 58 | + Files.copy(srcFile, destFile, COPY_ATTRIBUTES, REPLACE_EXISTING); |
| 59 | + } |
| 60 | + } |
| 61 | + else { |
| 62 | + log.debug("skipping destFile={}", destFile); |
| 63 | + } |
| 64 | + |
| 65 | + return FileVisitResult.CONTINUE; |
| 66 | + } |
| 67 | + |
| 68 | + private boolean processFile(Path srcFile, Path destFile) throws IOException { |
| 69 | + if (Files.notExists(destFile)) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + if (skipNewerInDestination) { |
| 74 | + if (Files.getLastModifiedTime(destFile).compareTo(Files.getLastModifiedTime(srcFile)) > 0) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return Files.size(srcFile) != Files.size(destFile) || |
| 80 | + Files.getLastModifiedTime(srcFile) != Files.getLastModifiedTime(destFile); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { |
| 85 | + log.warn("Failed to access {} due to {}", file, exc.getMessage()); |
| 86 | + log.debug("Details", exc); |
| 87 | + return FileVisitResult.CONTINUE; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 92 | + return FileVisitResult.CONTINUE; |
| 93 | + } |
| 94 | +} |
0 commit comments