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