Delete previous output state on execution skip 09/104809/3
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 9 Mar 2023 15:47:57 +0000 (16:47 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 9 Mar 2023 16:37:17 +0000 (17:37 +0100)
Address two FIXMEs related to wiping actual state: when there are no
input files or no code generators, we should be wiping all of our
previous state.

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

index 2422d575dc5505f5d4f4a06e6ab0d7fe89a19d01..d8e57cfc7b25c2f70d3dcbfbe434226a207a9ea9 100644 (file)
@@ -43,4 +43,9 @@ final class BuildContextStateStorage extends StateStorage {
     void storeState(final YangToSourcesState state) {
         buildContext.setValue(STATE_KEY, requireNonNull(state));
     }
+
+    @Override
+    void deleteState() {
+        buildContext.setValue(STATE_KEY, null);
+    }
 }
\ No newline at end of file
index 6db7c3200a3ea72e93382d7d8df0074c9db26476..18b6a2cc233f1d4fb6c49d83baf1055252088962 100644 (file)
@@ -43,4 +43,9 @@ final class FileStateStorage extends StateStorage {
             state.writeTo(out);
         }
     }
+
+    @Override
+    void deleteState() throws IOException {
+        Files.deleteIfExists(stateFile);
+    }
 }
\ No newline at end of file
index de93466fdd36b15d98884438a905177c5b264c2d..ffc3bcce480e2330bae8bfa921ca67225fd44466 100644 (file)
@@ -40,4 +40,6 @@ abstract class StateStorage {
     abstract @Nullable YangToSourcesState loadState() throws IOException;
 
     abstract void storeState(YangToSourcesState state) throws IOException;
+
+    abstract void deleteState() throws IOException;
 }
index 6467d4498c62d01429d9f49cd1a1e7034c6afe04..0149d188a2935ce64f80d307fc8b260f295d3978 100644 (file)
@@ -156,6 +156,7 @@ class YangToSourcesProcessor {
         if (yangFilesInProject.isEmpty()) {
             // No files to process, skip.
             LOG.info("{} No input files found", LOG_PREFIX);
+            wipeAllState(prevState);
             return;
         }
 
@@ -163,6 +164,7 @@ class YangToSourcesProcessor {
         final List<GeneratorTask> codeGenerators = instantiateGenerators();
         if (codeGenerators.isEmpty()) {
             LOG.warn("{} No code generators provided", LOG_PREFIX);
+            wipeAllState(prevState);
             return;
         }
 
@@ -312,6 +314,24 @@ class YangToSourcesProcessor {
         }
     }
 
+    private void wipeAllState(final 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);
+                }
+            }
+        }
+
+        try {
+            stateStorage.deleteState();
+        } catch (IOException e) {
+            throw new MojoExecutionException("Failed to remove execution state", e);
+        }
+    }
+
     private List<GeneratorTask> instantiateGenerators() throws MojoExecutionException {
         // Search for available FileGenerator implementations
         final Map<String, FileGeneratorFactory> factories = Maps.uniqueIndex(