Switch to using plexus-build-api for file output
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / YangToSourcesMojo.java
index 07c3edcffe432b8348adb57e97adee8b0a407bdb..dcaed9513d8b6695aec466df0a577bb9d5586932 100644 (file)
@@ -11,18 +11,23 @@ import java.io.File;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Set;
 
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.maven.project.MavenProject;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
+import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
 import org.slf4j.impl.StaticLoggerBinder;
+import org.sonatype.plexus.build.incremental.BuildContext;
 
 import com.google.common.annotations.VisibleForTesting;
 
@@ -31,11 +36,11 @@ import com.google.common.annotations.VisibleForTesting;
  * {@link CodeGenerator}s. Steps of this process:
  * <ol>
  * <li>List yang files from {@link #yangFilesRootDir}</li>
- * <li>Process yang files using {@link YangModelParserImpl}</li>
+ * <li>Process yang files using {@link YangParserImpl}</li>
  * <li>For each {@link CodeGenerator} from {@link #codeGenerators}:</li>
  * <ol>
  * <li>Instantiate using default constructor</li>
- * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File)}</li>
+ * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File, Set)}</li>
  * </ol>
  * </ol>
  */
@@ -56,20 +61,31 @@ public final class YangToSourcesMojo extends AbstractMojo {
      * with .yang suffix).
      */
     @Parameter(required = false)
-    private String yangFilesRootDir; // defaults to ${basedir}/src/main/yang
+    // defaults to ${basedir}/src/main/yang
+    private String yangFilesRootDir;
+
+    @Parameter(required = false)
+    private String[] excludeFiles;
 
     @Parameter(property = "project", required = true, readonly = true)
-    protected MavenProject project;
+    private MavenProject project;
 
     @Parameter(property = "inspectDependencies", required = true, readonly = true)
     private boolean inspectDependencies;
 
+    @Component
+    private BuildContext buildContext;
+
     private YangToSourcesProcessor yangToSourcesProcessor;
 
     public YangToSourcesMojo() {
 
     }
 
+    public void setProject(MavenProject project) {
+        this.project = project;
+    }
+
     @VisibleForTesting
     YangToSourcesMojo(YangToSourcesProcessor processor) {
         this.yangToSourcesProcessor = processor;
@@ -82,18 +98,16 @@ public final class YangToSourcesMojo extends AbstractMojo {
             List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
 
             // defaults to ${basedir}/src/main/yang
-            File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir,
-                    project.getBasedir());
+            File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
+            File[] excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
 
-            yangToSourcesProcessor = new YangToSourcesProcessor(getLog(),
-                    yangFilesRootFile, codeGeneratorArgs, project,
-                    inspectDependencies);
+            yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, getLog(), yangFilesRootFile, excludedFiles,
+                    codeGeneratorArgs, project, inspectDependencies);
         }
         yangToSourcesProcessor.execute();
     }
 
-    private static List<CodeGeneratorArg> processCodeGenerators(
-            CodeGeneratorArg[] codeGenerators) {
+    private static List<CodeGeneratorArg> processCodeGenerators(CodeGeneratorArg[] codeGenerators) {
         List<CodeGeneratorArg> codeGeneratorArgs;
         if (codeGenerators == null) {
             codeGeneratorArgs = Collections.emptyList();
@@ -103,12 +117,10 @@ public final class YangToSourcesMojo extends AbstractMojo {
         return codeGeneratorArgs;
     }
 
-    private static File processYangFilesRootDir(String yangFilesRootDir,
-            File baseDir) {
+    private static File processYangFilesRootDir(String yangFilesRootDir, File baseDir) {
         File yangFilesRootFile;
         if (yangFilesRootDir == null) {
-            yangFilesRootFile = new File(baseDir, "src" + File.separator
-                    + "main" + File.separator + "yang");
+            yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
         } else {
             File file = new File(yangFilesRootDir);
             if (file.isAbsolute()) {
@@ -119,4 +131,17 @@ public final class YangToSourcesMojo extends AbstractMojo {
         }
         return yangFilesRootFile;
     }
+
+    private static File[] processExcludeFiles(String[] excludeFiles, File baseDir) {
+        if (excludeFiles == null) {
+            return new File[] {};
+        }
+        File[] result = new File[excludeFiles.length];
+        for (int i = 0; i < excludeFiles.length; i++) {
+            result[i] = new File(baseDir, excludeFiles[i]);
+        }
+
+        return result;
+    }
+
 }