Add property "yang.skip"
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / YangToSourcesMojo.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang2sources.plugin;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import java.io.File;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Set;
16 import org.apache.maven.artifact.repository.ArtifactRepository;
17 import org.apache.maven.plugin.AbstractMojo;
18 import org.apache.maven.plugin.MojoExecutionException;
19 import org.apache.maven.plugin.MojoFailureException;
20 import org.apache.maven.plugins.annotations.Component;
21 import org.apache.maven.plugins.annotations.LifecyclePhase;
22 import org.apache.maven.plugins.annotations.Mojo;
23 import org.apache.maven.plugins.annotations.Parameter;
24 import org.apache.maven.plugins.annotations.ResolutionScope;
25 import org.apache.maven.project.MavenProject;
26 import org.apache.maven.repository.RepositorySystem;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
29 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
30 import org.sonatype.plexus.build.incremental.BuildContext;
31
32 /**
33  * Generate sources from yang files using user provided set of
34  * {@link BasicCodeGenerator}s. Steps of this process:
35  * <ol>
36  * <li>List yang files from {@link #yangFilesRootDir}</li>
37  * <li>Process yang files using Yang Parser</li>
38  * <li>For each {@link BasicCodeGenerator} from {@link #codeGenerators}:
39  * <ol>
40  * <li>Instantiate using default constructor</li>
41  * <li>Call {@link BasicCodeGenerator#generateSources(SchemaContext, File, Set)}</li>
42  * </ol></li>
43  * </ol>
44  */
45 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
46 public final class YangToSourcesMojo extends AbstractMojo {
47     public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
48
49     /**
50      * Classes implementing {@link BasicCodeGenerator} interface. An instance will be
51      * created out of every class using default constructor. Method {@link
52      * CodeGenerator#generateSources(SchemaContext, File, Set<String>
53      * yangModulesNames)} will be called on every instance.
54      */
55     @Parameter(required = false)
56     private CodeGeneratorArg[] codeGenerators;
57
58     /**
59      * Source directory that will be recursively searched for yang files (ending
60      * with .yang suffix).
61      */
62     @Parameter(required = false)
63     // defaults to ${basedir}/src/main/yang
64     private String yangFilesRootDir;
65
66     @Parameter(required = false)
67     private String[] excludeFiles;
68
69     @Parameter(property = "project", required = true, readonly = true)
70     private MavenProject project;
71
72     @Parameter(property = "inspectDependencies")
73     private boolean inspectDependencies;
74
75     @Component
76     private BuildContext buildContext;
77
78     private YangToSourcesProcessor yangToSourcesProcessor;
79
80     @Component
81     private RepositorySystem repoSystem;
82
83     @Parameter( readonly = true, defaultValue = "${localRepository}" )
84     private ArtifactRepository localRepository;
85
86     @Parameter( readonly = true, defaultValue = "${project.remoteArtifactRepositories}" )
87     private List<ArtifactRepository> remoteRepos;
88
89     // When set to "true", then the execution of the plugin is disabled
90     @Parameter( property = "yang.skip" )
91     private String yang_skip;
92
93     public YangToSourcesMojo() {
94     }
95
96     public void setProject(final MavenProject project) {
97         this.project = project;
98     }
99
100     @VisibleForTesting
101     YangToSourcesMojo(final YangToSourcesProcessor processor) {
102         this.yangToSourcesProcessor = processor;
103     }
104
105     @Override
106     public void execute() throws MojoExecutionException, MojoFailureException {
107         Util.checkClasspath(project, repoSystem, localRepository, remoteRepos);
108
109         if (yangToSourcesProcessor == null) {
110             List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
111
112             // defaults to ${basedir}/src/main/yang
113             File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
114             File[] excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
115
116             yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, yangFilesRootFile,
117                     excludedFiles, codeGeneratorArgs, project, inspectDependencies);
118         }
119         yangToSourcesProcessor.conditionalExecute("true".equals(yang_skip));
120     }
121
122     private static List<CodeGeneratorArg> processCodeGenerators(final CodeGeneratorArg[] codeGenerators) {
123         List<CodeGeneratorArg> codeGeneratorArgs;
124         if (codeGenerators == null) {
125             codeGeneratorArgs = Collections.emptyList();
126         } else {
127             codeGeneratorArgs = Arrays.asList(codeGenerators);
128         }
129         return codeGeneratorArgs;
130     }
131
132     private static File processYangFilesRootDir(final String yangFilesRootDir, final File baseDir) {
133         File yangFilesRootFile;
134         if (yangFilesRootDir == null) {
135             yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
136         } else {
137             File file = new File(yangFilesRootDir);
138             if (file.isAbsolute()) {
139                 yangFilesRootFile = file;
140             } else {
141                 yangFilesRootFile = new File(baseDir, file.getPath());
142             }
143         }
144         return yangFilesRootFile;
145     }
146
147     private static File[] processExcludeFiles(final String[] excludeFiles, final File baseDir) {
148         if (excludeFiles == null) {
149             return new File[] {};
150         }
151         File[] result = new File[excludeFiles.length];
152         for (int i = 0; i < excludeFiles.length; i++) {
153             result[i] = new File(baseDir, excludeFiles[i]);
154         }
155
156         return result;
157     }
158
159 }