001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.tools.plugin.generator;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.InputStreamReader;
025import java.io.OutputStreamWriter;
026import java.io.StringWriter;
027import java.io.Writer;
028
029import org.apache.maven.project.MavenProject;
030import org.apache.velocity.VelocityContext;
031import org.codehaus.plexus.logging.AbstractLogEnabled;
032import org.codehaus.plexus.logging.Logger;
033import org.codehaus.plexus.logging.console.ConsoleLogger;
034import org.codehaus.plexus.util.io.CachingOutputStream;
035import org.codehaus.plexus.velocity.VelocityComponent;
036
037import static java.nio.charset.StandardCharsets.UTF_8;
038
039/**
040 * Generates an <code>HelpMojo</code> class from <code>help-class-source.vm</code> template.
041 * The generated mojo reads help content from <code>META-INF/maven/${groupId}/${artifactId}/plugin-help.xml</code>
042 * resource, which is generated by this {@link PluginDescriptorFilesGenerator}.
043 *
044 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
045 * @since 2.4
046 */
047public class PluginHelpGenerator extends AbstractLogEnabled {
048    /**
049     * Default generated class name
050     */
051    private static final String HELP_MOJO_CLASS_NAME = "HelpMojo";
052
053    private String helpPackageName;
054    private String goalPrefix;
055    private MavenProject mavenProject;
056    private boolean useMaven4Api;
057    private VelocityComponent velocityComponent;
058
059    /**
060     * Default constructor
061     */
062    public PluginHelpGenerator() {
063        this.enableLogging(new ConsoleLogger(Logger.LEVEL_INFO, "PluginHelpGenerator"));
064    }
065
066    // ----------------------------------------------------------------------
067    // Public methods
068    // ----------------------------------------------------------------------
069
070    public void execute(File destinationDirectory) throws GeneratorException {
071        String helpImplementation = getImplementation();
072
073        useMaven4Api = mavenProject.getDependencies().stream()
074                .anyMatch(dep ->
075                        "org.apache.maven".equals(dep.getGroupId()) && "maven-api-core".equals(dep.getArtifactId()));
076
077        try {
078            String sourcePath = helpImplementation.replace('.', File.separatorChar) + ".java";
079
080            File helpClass = new File(destinationDirectory, sourcePath);
081            helpClass.getParentFile().mkdirs();
082
083            String helpClassSources = getHelpClassSources(getPluginHelpPath(mavenProject));
084
085            try (Writer w = new OutputStreamWriter(new CachingOutputStream(helpClass), UTF_8)) {
086                w.write(helpClassSources);
087            }
088        } catch (IOException e) {
089            throw new GeneratorException(e.getMessage(), e);
090        }
091    }
092
093    public PluginHelpGenerator setHelpPackageName(String helpPackageName) {
094        this.helpPackageName = helpPackageName;
095        return this;
096    }
097
098    public PluginHelpGenerator setVelocityComponent(VelocityComponent velocityComponent) {
099        this.velocityComponent = velocityComponent;
100        return this;
101    }
102
103    public PluginHelpGenerator setGoalPrefix(String goalPrefix) {
104        this.goalPrefix = goalPrefix;
105        return this;
106    }
107
108    public PluginHelpGenerator setMavenProject(MavenProject mavenProject) {
109        this.mavenProject = mavenProject;
110        return this;
111    }
112
113    // ----------------------------------------------------------------------
114    // Private methods
115    // ----------------------------------------------------------------------
116
117    private String getHelpClassSources(String pluginHelpPath) throws IOException {
118        VelocityContext context = new VelocityContext();
119        boolean useAnnotations =
120                mavenProject.getArtifactMap().containsKey("org.apache.maven.plugin-tools:maven-plugin-annotations");
121
122        context.put("helpPackageName", helpPackageName);
123        context.put("pluginHelpPath", pluginHelpPath);
124        context.put("artifactId", mavenProject.getArtifactId());
125        // TODO: evaluate prefix from deserialized plugin
126        context.put("goalPrefix", goalPrefix);
127        context.put("useAnnotations", useAnnotations);
128
129        StringWriter stringWriter = new StringWriter();
130
131        // plugin-tools sources are UTF-8 (and even ASCII in this case))
132        try (InputStream is = Thread.currentThread()
133                        .getContextClassLoader()
134                        .getResourceAsStream(useMaven4Api ? "help-class-source-v4.vm" : "help-class-source.vm"); //
135                InputStreamReader isReader = new InputStreamReader(is, UTF_8)) {
136            // isReader =
137            velocityComponent.getEngine().evaluate(context, stringWriter, "", isReader);
138        }
139        // Apply OS lineSeparator instead of template's lineSeparator to have consistent separators for
140        // all source files.
141        return stringWriter.toString().replaceAll("(\r\n|\n|\r)", System.lineSeparator());
142    }
143
144    /**
145     * @return The implementation.
146     */
147    private String getImplementation() {
148        return (helpPackageName == null || helpPackageName.isEmpty())
149                ? HELP_MOJO_CLASS_NAME
150                : helpPackageName + '.' + HELP_MOJO_CLASS_NAME;
151    }
152
153    static String getPluginHelpPath(MavenProject mavenProject) {
154        return mavenProject.getGroupId() + "/" + mavenProject.getArtifactId() + "/plugin-help.xml";
155    }
156}