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