Do not pass BuildContext to GeneratorTask 90/104790/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Mar 2023 18:21:20 +0000 (19:21 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Mar 2023 18:22:18 +0000 (19:22 +0100)
We have previously decomposed OutputStream creation and refresh
notification. Now move the refresh notification processing to
YangToSourcesProcessor.

This improves the plugin in that:
- WriteTask is simpler (and smaller in some runtimes)
- BuildContext interactions are more localized
- BuildContext executes on the initial thread, improving its
  confinement

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

index fe71b83d0460dee9ef8a4e3f3b5f15465f28f769..be391cdbc677fda45202d9a5806d07f260186965 100644 (file)
@@ -33,7 +33,6 @@ import org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType;
 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.sonatype.plexus.build.incremental.BuildContext;
 
 final class GeneratorTask extends ParserConfigAware {
     private static final Logger LOG = LoggerFactory.getLogger(GeneratorTask.class);
@@ -61,7 +60,7 @@ final class GeneratorTask extends ParserConfigAware {
         return factory.parserConfig();
     }
 
-    List<FileState> execute(final BuildContext buildContext) throws FileGeneratorException, IOException {
+    List<FileState> execute() throws FileGeneratorException, IOException {
         // Step one: determine what files are going to be generated
         final Stopwatch sw = Stopwatch.createStarted();
         final FileGenerator gen = factory.generator();
@@ -91,7 +90,7 @@ final class GeneratorTask extends ParserConfigAware {
                     throw new IllegalStateException("Unsupported file type in " + file);
             }
 
-            dirs.put(target.getParentFile(), new WriteTask(buildContext, target, cell.getValue()));
+            dirs.put(target.getParentFile(), new WriteTask(target, cell.getValue()));
         }
         LOG.info("Sorted {} files into {} directories in {}", dirs.size(), dirs.keySet().size(), sw);
 
@@ -178,12 +177,10 @@ final class GeneratorTask extends ParserConfigAware {
     }
 
     private static final class WriteTask {
-        private final BuildContext buildContext;
         private final GeneratedFile file;
         private final File target;
 
-        WriteTask(final BuildContext buildContext, final File target, final GeneratedFile file) {
-            this.buildContext = requireNonNull(buildContext);
+        WriteTask(final File target, final GeneratedFile file) {
             this.target = requireNonNull(target);
             this.file = requireNonNull(file);
         }
@@ -195,7 +192,6 @@ final class GeneratorTask extends ParserConfigAware {
             } catch (IOException e) {
                 throw new IllegalStateException("Failed to generate file " + target, e);
             }
-            buildContext.refresh(target);
             return ret;
         }
     }
index 4fbb29c62545f5adeeec5f2d7f5c6ec8b42ac202..aaeb0bac2db28292cfa71abf12a1488f31e2eb68 100644 (file)
@@ -249,11 +249,12 @@ class YangToSourcesProcessor {
 
                     final List<FileState> files;
                     try {
-                        files = task.execute(buildContext);
+                        files = task.execute();
                     } catch (FileGeneratorException | IOException e) {
                         throw new MojoFailureException(LOG_PREFIX + " Generator " + factory + " failed", e);
                     }
 
+                    files.forEach(state -> buildContext.refresh(new File(state.path())));
                     outputFiles.addAll(files);
 
                     LOG.info("{} Sources generated by {}: {} in {}", LOG_PREFIX, factory.generatorName(), files.size(),