Track META-INF/ files in state
[yangtools.git] / plugin / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / FileState.java
index e2357d8f74b3fa6302865a144b542e825e4b9499..e06e65e0af4e1b809c2c60a761300e92cc4cea24 100644 (file)
@@ -9,6 +9,10 @@ package org.opendaylight.yangtools.yang2sources.plugin;
 
 import static java.util.Objects.requireNonNull;
 
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.file.Files;
 import java.nio.file.attribute.BasicFileAttributes;
 import org.eclipse.jdt.annotation.NonNull;
 
@@ -16,7 +20,28 @@ import org.eclipse.jdt.annotation.NonNull;
  * Hash of a single file state. {@link #size()} corresponds to {@link BasicFileAttributes#size()}.
  */
 record FileState(@NonNull String path, long size, int crc32) {
+    /**
+     * A simple interface describing the intended content of a file.
+     */
+    @FunctionalInterface
+    interface FileContent {
+        /**
+         * Write the content of this file to specified {@link OutputStream}.
+         *
+         * @param out OutputStream to write to, guaranteed to be non-null
+         * @throws IOException if any error occurs
+         */
+        void writeTo(@NonNull OutputStream out) throws IOException;
+    }
+
     FileState {
         requireNonNull(path);
     }
+
+    static @NonNull FileState ofWrittenFile(final File file, final FileContent content) throws IOException {
+        try (var out = new CapturingOutputStream(Files.newOutputStream(file.toPath()))) {
+            content.writeTo(out);
+            return new FileState(file.getPath(), out.size(), out.crc32c());
+        }
+    }
 }