o Unified goal documentation by wrapping help goal pieces into a minimal mojo descriptor

git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@654867 13f79535-47bb-0310-9956-ffa450edef68
master
Benjamin Bentmann 2008-05-09 16:02:21 +00:00
parent fe9715f38d
commit 4c11552445
1 changed files with 65 additions and 54 deletions

View File

@ -84,36 +84,37 @@ public class PluginHelpGenerator
return; return;
} }
String packageName = discoverPackageName( pluginDescriptor ); MojoDescriptor helpDescriptor = makeHelpDescriptor( pluginDescriptor );
File helpClass = new File( destinationDirectory, packageName.replace( '.', File.separatorChar )
+ File.separator + HELP_MOJO_CLASS_NAME + ".java" );
// Verify that no help goal already exists // Verify that no help goal already exists
for ( Iterator it = pluginDescriptor.getMojos().iterator(); it.hasNext(); ) for ( Iterator it = pluginDescriptor.getMojos().iterator(); it.hasNext(); )
{ {
MojoDescriptor descriptor = (MojoDescriptor) it.next(); MojoDescriptor descriptor = (MojoDescriptor) it.next();
if ( descriptor.getGoal().equals( HELP_GOAL ) if ( descriptor.getGoal().equals( helpDescriptor.getGoal() )
&& !descriptor.getImplementation().equals( packageName + "." + HELP_MOJO_CLASS_NAME ) ) && !descriptor.getImplementation().equals( helpDescriptor.getImplementation() ) )
{ {
if ( getLogger().isWarnEnabled() ) if ( getLogger().isWarnEnabled() )
{ {
getLogger().warn( "\n\nAn help goal (" + descriptor.getImplementation() getLogger().warn(
+ ") already exists in this plugin. SKIPPED THE " + HELP_MOJO_CLASS_NAME + " GENERATION.\n" ); "\n\nA help goal (" + descriptor.getImplementation()
+ ") already exists in this plugin. SKIPPED THE "
+ helpDescriptor.getImplementation() + " GENERATION.\n" );
} }
return; return;
} }
} }
String sourcePath = helpDescriptor.getImplementation().replace( '.', File.separatorChar ) + ".java";
File helpClass = new File( destinationDirectory, sourcePath );
helpClass.getParentFile().mkdirs(); helpClass.getParentFile().mkdirs();
Writer writer = null; Writer writer = null;
try try
{ {
writer = new FileWriter( helpClass ); writer = new FileWriter( helpClass );
writeClass( writer, packageName, pluginDescriptor ); writeClass( writer, pluginDescriptor, helpDescriptor );
writer.flush(); writer.flush();
} }
finally finally
@ -127,29 +128,36 @@ public class PluginHelpGenerator
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* @return the help goal for the generated mojo * Creates a minimalistic mojo descriptor for the generated help goal.
*
* @param pluginDescriptor The descriptor of the plugin for which to generate a help goal, must not be
* <code>null</code>.
* @return The mojo descriptor for the generated help goal, never <code>null</code>.
*/ */
private static String getHelpGoalName() private static MojoDescriptor makeHelpDescriptor( PluginDescriptor pluginDescriptor )
{ {
return HELP_GOAL; MojoDescriptor descriptor = new MojoDescriptor();
descriptor.setPluginDescriptor( pluginDescriptor );
descriptor.setLanguage( "java" );
descriptor.setGoal( HELP_GOAL );
String packageName = discoverPackageName( pluginDescriptor );
if ( StringUtils.isNotEmpty( packageName ) )
{
descriptor.setImplementation( packageName + '.' + HELP_MOJO_CLASS_NAME );
}
else
{
descriptor.setImplementation( HELP_MOJO_CLASS_NAME );
} }
/** descriptor.setDescription( "Display help information on '" + pluginDescriptor.getPluginLookupKey()
* @return the full help goal name for the generated mojo + "' plugin. Call 'mvn " + descriptor.getFullGoalName() + " -Ddetail=true' to display parameter details." );
*/
private static String getFullHelpGoalName( PluginDescriptor pluginDescriptor )
{
return pluginDescriptor.getGoalPrefix() + ":" + getHelpGoalName();
}
/** return descriptor;
* @param pluginDescriptor
* @return the help description for the generated mojo
*/
private static String getHelpDescription( PluginDescriptor pluginDescriptor )
{
return "Display help information on '" + pluginDescriptor.getPluginLookupKey() + "' plugin. Call 'mvn "
+ getFullHelpGoalName( pluginDescriptor ) + " -Ddetail=true' to display all details.";
} }
/** /**
@ -202,16 +210,25 @@ public class PluginHelpGenerator
} }
/** /**
* Generated the <code>HelpMojo</code> class. * Generates the <code>HelpMojo</code> class.
* *
* @param writer * @param writer
* @param packageName
* @param pluginDescriptor * @param pluginDescriptor
* @param helpDescriptor
* @throws IOException if any * @throws IOException if any
*/ */
private static void writeClass( Writer writer, String packageName, PluginDescriptor pluginDescriptor ) private static void writeClass( Writer writer, PluginDescriptor pluginDescriptor, MojoDescriptor helpDescriptor )
throws IOException throws IOException
{ {
String packageName = "";
String simpleName = helpDescriptor.getImplementation();
int dot = simpleName.lastIndexOf( '.' );
if ( dot >= 0 )
{
packageName = simpleName.substring( 0, dot );
simpleName = simpleName.substring( dot + 1 );
}
if ( packageName.length() > 0 ) if ( packageName.length() > 0 )
{ {
writer.write( "package " + packageName + ";" + LS ); writer.write( "package " + packageName + ";" + LS );
@ -221,9 +238,9 @@ public class PluginHelpGenerator
writeImports( writer ); writeImports( writer );
writer.write( LS ); writer.write( LS );
writeMojoJavadoc( writer, pluginDescriptor ); writeMojoJavadoc( writer, pluginDescriptor, helpDescriptor );
writer.write( "public class HelpMojo" + LS ); writer.write( "public class " + simpleName + LS );
writer.write( " extends AbstractMojo" + LS ); writer.write( " extends AbstractMojo" + LS );
writer.write( "{" + LS ); writer.write( "{" + LS );
@ -231,7 +248,7 @@ public class PluginHelpGenerator
writer.write( LS ); writer.write( LS );
writeExecute( writer, pluginDescriptor ); writeExecute( writer, pluginDescriptor, helpDescriptor );
writer.write( LS ); writer.write( LS );
writeUtilities( writer ); writeUtilities( writer );
@ -250,14 +267,15 @@ public class PluginHelpGenerator
writer.write( "import org.apache.maven.plugin.MojoExecutionException;" + LS ); writer.write( "import org.apache.maven.plugin.MojoExecutionException;" + LS );
} }
private static void writeMojoJavadoc( Writer writer, PluginDescriptor pluginDescriptor ) private static void writeMojoJavadoc( Writer writer, PluginDescriptor pluginDescriptor,
MojoDescriptor helpDescriptor )
throws IOException throws IOException
{ {
writer.write( "/**" + LS ); writer.write( "/**" + LS );
writer.write( " * " + getHelpDescription( pluginDescriptor ) + LS ); writer.write( " * " + helpDescriptor.getDescription() + LS );
writer.write( " *" + LS ); writer.write( " *" + LS );
writer.write( " * @version generated on " + new Date() + LS ); writer.write( " * @version generated on " + new Date() + LS );
writer.write( " * @goal " + getHelpGoalName() + LS ); writer.write( " * @goal " + helpDescriptor.getGoal() + LS );
writer.write( " * @requiresProject false" + LS ); writer.write( " * @requiresProject false" + LS );
writer.write( " */" + LS ); writer.write( " */" + LS );
} }
@ -279,19 +297,19 @@ public class PluginHelpGenerator
writer.write( " private boolean detail;" + LS ); writer.write( " private boolean detail;" + LS );
} }
private static void writeExecute( Writer writer, PluginDescriptor pluginDescriptor ) private static void writeExecute( Writer writer, PluginDescriptor pluginDescriptor, MojoDescriptor helpDescriptor )
throws IOException throws IOException
{ {
List mojoDescriptors = new ArrayList( pluginDescriptor.getMojos() ); List mojoDescriptors = new ArrayList();
for ( Iterator it = mojoDescriptors.iterator(); it.hasNext(); ) mojoDescriptors.add( helpDescriptor );
for ( Iterator it = pluginDescriptor.getMojos().iterator(); it.hasNext(); )
{ {
MojoDescriptor mojoDescriptor = (MojoDescriptor) it.next(); MojoDescriptor mojoDescriptor = (MojoDescriptor) it.next();
if ( getHelpGoalName().equals( mojoDescriptor.getGoal() ) ) if ( !helpDescriptor.getGoal().equals( mojoDescriptor.getGoal() ) )
{ {
// remove previously generated help goal mojoDescriptors.add( mojoDescriptor );
it.remove();
} }
} }
@ -315,8 +333,8 @@ public class PluginHelpGenerator
writer.write( " StringBuffer sb = new StringBuffer();" + LS ); writer.write( " StringBuffer sb = new StringBuffer();" + LS );
writer.write( LS ); writer.write( LS );
writer.write( " sb.append( \"The '" + pluginDescriptor.getPluginLookupKey() + "' plugin has " writer.write( " sb.append( \"The '" + pluginDescriptor.getPluginLookupKey() + "' plugin has "
+ ( mojoDescriptors.size() + 1 ) + " " + mojoDescriptors.size() + " "
+ ( ( mojoDescriptors.size() + 1 ) > 1 ? "goals" : "goal" ) + ":\" ).append( \"\\n\" );" + LS ); + ( mojoDescriptors.size() > 1 ? "goals" : "goal" ) + ":\" ).append( \"\\n\" );" + LS );
writer.write( " sb.append( \"\\n\" );" + LS ); writer.write( " sb.append( \"\\n\" );" + LS );
writer.write( LS ); writer.write( LS );
@ -328,13 +346,6 @@ public class PluginHelpGenerator
writeGoal( writer, descriptor ); writeGoal( writer, descriptor );
} }
// TODO Should be discovered
writer.write( " sb.append( \"" + getFullHelpGoalName( pluginDescriptor ) + "\" ).append( \"\\n\" );"
+ LS );
writer.write( " appendDescription( sb, \"" + getHelpDescription( pluginDescriptor ) + "\", DEFAULT_INDENT );" + LS );
writer.write( LS );
writer.write( " if ( getLog().isInfoEnabled() )" + LS ); writer.write( " if ( getLog().isInfoEnabled() )" + LS );
writer.write( " {" + LS ); writer.write( " {" + LS );
writer.write( " getLog().info( sb.toString() );" + LS ); writer.write( " getLog().info( sb.toString() );" + LS );