1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.maven.plugin;
7   
8   import gov.nist.secauto.metaschema.core.model.IModule;
9   import gov.nist.secauto.metaschema.core.util.ObjectUtils;
10  import gov.nist.secauto.metaschema.databind.codegen.IProduction;
11  import gov.nist.secauto.metaschema.databind.codegen.JavaGenerator;
12  import gov.nist.secauto.metaschema.databind.codegen.config.DefaultBindingConfiguration;
13  
14  import org.apache.maven.plugin.MojoExecutionException;
15  import org.apache.maven.plugins.annotations.LifecyclePhase;
16  import org.apache.maven.plugins.annotations.Mojo;
17  import org.apache.maven.plugins.annotations.Parameter;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Set;
25  import java.util.stream.Collectors;
26  
27  import edu.umd.cs.findbugs.annotations.NonNull;
28  
29  /**
30   * Goal which generates Java source files for a given set of Metaschema modules.
31   */
32  @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
33  public class GenerateSourcesMojo
34      extends AbstractMetaschemaMojo {
35    private static final String STALE_FILE_NAME = "generateSourcesStaleFile";
36  
37    /**
38     * A set of binding configurations.
39     */
40    @Parameter
41    protected File[] configs;
42  
43    /**
44     * <p>
45     * Gets the last part of the stale filename.
46     * </p>
47     * <p>
48     * The full stale filename will be generated by pre-pending
49     * {@code "." + getExecution().getExecutionId()} to this staleFileName.
50     *
51     * @return the stale filename postfix
52     */
53    @Override
54    protected String getStaleFileName() {
55      return STALE_FILE_NAME;
56    }
57  
58    /**
59     * Retrieve a list of binding configurations.
60     *
61     * @return the collection of binding configurations
62     */
63    protected List<File> getConfigs() {
64      List<File> retval;
65      if (configs == null) {
66        retval = Collections.emptyList();
67      } else {
68        retval = Arrays.asList(configs);
69      }
70      return retval;
71    }
72  
73    /**
74     * Generate the Java source files for the provided Metaschemas.
75     *
76     * @param modules
77     *          the collection of Metaschema modules to generate sources for
78     * @throws MojoExecutionException
79     *           if an error occurred while generating sources
80     */
81    @Override
82    @NonNull
83    protected List<File> generate(@NonNull Set<IModule> modules) throws MojoExecutionException {
84      DefaultBindingConfiguration bindingConfiguration = new DefaultBindingConfiguration();
85      for (File config : getConfigs()) {
86        try {
87          if (getLog().isInfoEnabled()) {
88            getLog().info("Loading binding configuration: " + config.getPath());
89          }
90          bindingConfiguration.load(config);
91        } catch (IOException ex) {
92          throw new MojoExecutionException(
93              String.format("Unable to load binding configuration from '%s'.", config.getPath()), ex);
94        }
95      }
96  
97      IProduction production;
98      try {
99        if (getLog().isInfoEnabled()) {
100         getLog().info("Generating Java classes in: " + getOutputDirectory().getPath());
101       }
102       production = JavaGenerator.generate(
103           modules,
104           ObjectUtils.notNull(getOutputDirectory().toPath()),
105           bindingConfiguration);
106     } catch (IOException ex) {
107       throw new MojoExecutionException("Creation of Java classes failed.", ex);
108     }
109 
110     // add generated sources to Maven
111     try {
112       getMavenProject().addCompileSourceRoot(getOutputDirectory().getCanonicalFile().getPath());
113     } catch (IOException ex) {
114       throw new MojoExecutionException("Unable to add output directory to maven sources.", ex);
115     }
116 
117     return ObjectUtils.notNull(production.getGeneratedClasses()
118         .map(gen -> gen.getClassFile().toFile())
119         .collect(Collectors.toUnmodifiableList()));
120   }
121 }