Rehost file deletion 14/104814/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 9 Mar 2023 19:26:24 +0000 (20:26 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 9 Mar 2023 19:29:43 +0000 (20:29 +0100)
YangToSourceState is a simple DTO, but we really want to host some
utilities here. Wiping output files is one of them.

This also means we never deal with a null state, making
IncrementalBuildSupport simpler.

JIRA: YANGTOOLS-745
Change-Id: I6c5190275742c8518149cac70dfe7586f579e451
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/IncrementalBuildSupport.java
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesState.java

index eaf1f0cf415d13bafc48ed1f5a52911a7c624cb6..7e85d3d43f874ed7095ecd37c45d386c36e23f59 100644 (file)
@@ -18,7 +18,6 @@ import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonatype.plexus.build.incremental.BuildContext;
@@ -36,16 +35,15 @@ final class IncrementalBuildSupport {
     private static final Logger LOG = LoggerFactory.getLogger(IncrementalBuildSupport.class);
     private static final List<String> TRANSIENT_DIRECTORIES = List.of("generated-resources", "generated-sources");
 
+    private final @NonNull YangToSourcesState previousState;
     private final @NonNull ImmutableMap<String, FileGeneratorArg> fileGeneratorArgs;
     private final @NonNull FileStateSet projectYangs;
     private final @NonNull FileStateSet dependencyYangs;
 
-    private final @Nullable YangToSourcesState previousState;
-
     IncrementalBuildSupport(final YangToSourcesState previousState,
             final ImmutableMap<String, FileGeneratorArg> fileGeneratorArgs, final FileStateSet projectYangs,
             final FileStateSet dependencyYangs) {
-        this.previousState = previousState;
+        this.previousState = requireNonNull(previousState);
         this.fileGeneratorArgs = requireNonNull(fileGeneratorArgs);
         this.projectYangs = requireNonNull(projectYangs);
         this.dependencyYangs = requireNonNull(dependencyYangs);
@@ -57,22 +55,16 @@ final class IncrementalBuildSupport {
      * @return {@code true} if restored state is different from currently-observed state
      */
     boolean inputsChanged() {
-        // Local variable to aid null analysis
-        final var local = previousState;
-        if (local == null) {
-            LOG.debug("{}: no previous input state", YangToSourcesProcessor.LOG_PREFIX);
-            return true;
-        }
-        if (!fileGeneratorArgs.equals(local.fileGeneratorArgs())) {
+        if (!fileGeneratorArgs.equals(previousState.fileGeneratorArgs())) {
             LOG.debug("{}: file generator arguments changed from {} to {}", YangToSourcesProcessor.LOG_PREFIX,
-                fileGeneratorArgs, local.fileGeneratorArgs());
+                fileGeneratorArgs, previousState.fileGeneratorArgs());
             return true;
         }
-        if (!projectYangs.equals(local.projectYangs())) {
+        if (!projectYangs.equals(previousState.projectYangs())) {
             LOG.debug("{}: project YANG files changed", YangToSourcesProcessor.LOG_PREFIX);
             return true;
         }
-        if (!dependencyYangs.equals(local.dependencyYangs())) {
+        if (!dependencyYangs.equals(previousState.dependencyYangs())) {
             LOG.debug("{}: dependency YANG files changed", YangToSourcesProcessor.LOG_PREFIX);
             return true;
         }
@@ -88,15 +80,8 @@ final class IncrementalBuildSupport {
      * @return {@code true} if restored state and the build directory differ
      */
     boolean outputsChanged(final String projectBuildDirectory) throws IOException {
-        // Local variable to aid null analysis
-        final var local = previousState;
-        if (local == null) {
-            LOG.debug("{}: no previous output state", YangToSourcesProcessor.LOG_PREFIX);
-            return true;
-        }
-
         // Compare explicit mentions first
-        final var outputFiles = local.outputFiles().fileStates();
+        final var outputFiles = previousState.outputFiles().fileStates();
         if (outputsChanged(outputFiles.values())) {
             return true;
         }
index 94f471e6265d8ee704e6631b6e6585b85db8a704..3e1fae9191df8a09cb4d359a774ddc26f05e11fe 100644 (file)
@@ -35,7 +35,6 @@ import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException;
 import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory;
 import org.opendaylight.yangtools.yang.common.YangConstants;
@@ -139,7 +138,7 @@ class YangToSourcesProcessor {
     }
 
     void execute() throws MojoExecutionException, MojoFailureException {
-        final YangToSourcesState prevState;
+        YangToSourcesState prevState;
         try {
             prevState = stateStorage.loadState();
         } catch (IOException e) {
@@ -147,6 +146,8 @@ class YangToSourcesProcessor {
         }
         if (prevState == null) {
             LOG.debug("{} no previous execution state present", LOG_PREFIX);
+            prevState = new YangToSourcesState(ImmutableMap.of(),
+                    FileStateSet.empty(), FileStateSet.empty(), FileStateSet.empty());
         }
 
         // Collect all files in the current project.
@@ -331,17 +332,12 @@ class YangToSourcesProcessor {
         }
     }
 
-    private void wipeAllState(final @Nullable YangToSourcesState prevState) throws MojoExecutionException {
-        if (prevState != null) {
-            for (var file : prevState.outputFiles().fileStates().keySet()) {
-                try {
-                    Files.deleteIfExists(Path.of(file));
-                } catch (IOException e) {
-                    throw new MojoExecutionException("Failed to remove file " + file, e);
-                }
-            }
+    private void wipeAllState(final YangToSourcesState prevState) throws MojoExecutionException {
+        try {
+            prevState.deleteOutputFiles();
+        } catch (IOException e) {
+            throw new MojoExecutionException("Failed to delete output files", e);
         }
-
         try {
             stateStorage.deleteState();
         } catch (IOException e) {
index d3a839e60a1051bc88944b0c00eab37799c635d7..b3b50be245c1dbb871a53a5c5918cb515dea48cb 100644 (file)
@@ -13,6 +13,8 @@ import com.google.common.collect.ImmutableMap;
 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Collection;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.WritableObject;
@@ -45,11 +47,6 @@ record YangToSourcesState(
             FileStateSet.readFrom(in), FileStateSet.readFrom(in), FileStateSet.readFrom(in));
     }
 
-    static @NonNull YangToSourcesState empty() {
-        return new YangToSourcesState(ImmutableMap.of(),
-            FileStateSet.empty(), FileStateSet.empty(), FileStateSet.empty());
-    }
-
     @Override
     public void writeTo(final DataOutput out) throws IOException {
         out.writeInt(MAGIC);
@@ -59,6 +56,17 @@ record YangToSourcesState(
         outputFiles.writeTo(out);
     }
 
+    /**
+     * Delete all output files mentioned in {@link #outputFiles}.
+     *
+     * @throws IOException if any error occurs
+     */
+    void deleteOutputFiles() throws IOException {
+        for (var file : outputFiles.fileStates().keySet()) {
+            Files.deleteIfExists(Path.of(file));
+        }
+    }
+
     private static void writeConfigurations(final DataOutput out, final Collection<FileGeneratorArg> configurations)
             throws IOException {
         out.writeInt(configurations.size());