@ -21,10 +21,12 @@ import java.io.FileWriter;
import java.io.IOException ;
import java.io.IOException ;
import java.io.PrintWriter ;
import java.io.PrintWriter ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.stream.Collectors ;
import java.util.stream.Collectors ;
import org.gradle.api.DefaultTask ;
import org.gradle.api.DefaultTask ;
import org.gradle.api.Task ;
import org.gradle.api.Task ;
import org.gradle.api.tasks.Input ;
import org.gradle.api.tasks.InputFile ;
import org.gradle.api.tasks.InputFile ;
import org.gradle.api.tasks.OutputDirectory ;
import org.gradle.api.tasks.OutputDirectory ;
import org.gradle.api.tasks.TaskAction ;
import org.gradle.api.tasks.TaskAction ;
@ -46,6 +48,8 @@ public class DocumentPluginGoals extends DefaultTask {
private File outputDir ;
private File outputDir ;
private Map < String , String > goalSections ;
@OutputDirectory
@OutputDirectory
public File getOutputDir ( ) {
public File getOutputDir ( ) {
return this . outputDir ;
return this . outputDir ;
@ -55,6 +59,15 @@ public class DocumentPluginGoals extends DefaultTask {
this . outputDir = outputDir ;
this . outputDir = outputDir ;
}
}
@Input
public Map < String , String > getGoalSections ( ) {
return this . goalSections ;
}
public void setGoalSections ( Map < String , String > goalSections ) {
this . goalSections = goalSections ;
}
@InputFile
@InputFile
public File getPluginXml ( ) {
public File getPluginXml ( ) {
return this . pluginXml ;
return this . pluginXml ;
@ -80,7 +93,7 @@ public class DocumentPluginGoals extends DefaultTask {
writer . println ( "| Goal | Description" ) ;
writer . println ( "| Goal | Description" ) ;
writer . println ( ) ;
writer . println ( ) ;
for ( Mojo mojo : plugin . getMojos ( ) ) {
for ( Mojo mojo : plugin . getMojos ( ) ) {
writer . printf ( "| << goals-%s,%s:%s>>%n", mojo . getGoal ( ) , plugin . getGoalPrefix ( ) , mojo . getGoal ( ) ) ;
writer . printf ( "| << %s,%s:%s>>%n", goalSectionId ( mojo ) , plugin . getGoalPrefix ( ) , mojo . getGoal ( ) ) ;
writer . printf ( "| %s%n" , mojo . getDescription ( ) ) ;
writer . printf ( "| %s%n" , mojo . getDescription ( ) ) ;
writer . println ( ) ;
writer . println ( ) ;
}
}
@ -90,7 +103,7 @@ public class DocumentPluginGoals extends DefaultTask {
private void documentMojo ( Plugin plugin , Mojo mojo ) throws IOException {
private void documentMojo ( Plugin plugin , Mojo mojo ) throws IOException {
try ( PrintWriter writer = new PrintWriter ( new FileWriter ( new File ( this . outputDir , mojo . getGoal ( ) + ".adoc" ) ) ) ) {
try ( PrintWriter writer = new PrintWriter ( new FileWriter ( new File ( this . outputDir , mojo . getGoal ( ) + ".adoc" ) ) ) ) {
String sectionId = "goals-" + mojo . getGoal ( ) ;
String sectionId = goalSectionId ( mojo ) ;
writer . println ( ) ;
writer . println ( ) ;
writer . println ( ) ;
writer . println ( ) ;
writer . printf ( "[[%s]]%n" , sectionId ) ;
writer . printf ( "[[%s]]%n" , sectionId ) ;
@ -105,12 +118,11 @@ public class DocumentPluginGoals extends DefaultTask {
List < Parameter > requiredParameters = parameters . stream ( )
List < Parameter > requiredParameters = parameters . stream ( )
. filter ( Parameter : : isRequired )
. filter ( Parameter : : isRequired )
. collect ( Collectors . toList ( ) ) ;
. collect ( Collectors . toList ( ) ) ;
String parametersSectionId = sectionId + "-parameters" ;
String detailsSectionId = sectionId + ".parameter-details" ;
String detailsSectionId = parametersSectionId + "-details" ;
if ( ! requiredParameters . isEmpty ( ) ) {
if ( ! requiredParameters . isEmpty ( ) ) {
writer . println ( ) ;
writer . println ( ) ;
writer . println ( ) ;
writer . println ( ) ;
writer . printf ( "[[%s -required]]%n", parametersS ectionId) ;
writer . printf ( "[[%s .required-parameters]]%n", s ectionId) ;
writer . println ( "== Required parameters" ) ;
writer . println ( "== Required parameters" ) ;
writeParametersTable ( writer , detailsSectionId , requiredParameters ) ;
writeParametersTable ( writer , detailsSectionId , requiredParameters ) ;
}
}
@ -120,7 +132,7 @@ public class DocumentPluginGoals extends DefaultTask {
if ( ! optionalParameters . isEmpty ( ) ) {
if ( ! optionalParameters . isEmpty ( ) ) {
writer . println ( ) ;
writer . println ( ) ;
writer . println ( ) ;
writer . println ( ) ;
writer . printf ( "[[%s -optional]]%n", parametersS ectionId) ;
writer . printf ( "[[%s .optional-parameters]]%n", s ectionId) ;
writer . println ( "== Optional parameters" ) ;
writer . println ( "== Optional parameters" ) ;
writeParametersTable ( writer , detailsSectionId , optionalParameters ) ;
writeParametersTable ( writer , detailsSectionId , optionalParameters ) ;
}
}
@ -132,6 +144,15 @@ public class DocumentPluginGoals extends DefaultTask {
}
}
}
}
private String goalSectionId ( Mojo mojo ) {
String goalSection = this . goalSections . get ( mojo . getGoal ( ) ) ;
if ( goalSection = = null ) {
throw new IllegalStateException ( "Goal '" + mojo . getGoal ( ) + "' has not be assigned to a section" ) ;
}
String sectionId = goalSection + "." + mojo . getGoal ( ) + "-goal" ;
return sectionId ;
}
private void writeParametersTable ( PrintWriter writer , String detailsSectionId , List < Parameter > parameters ) {
private void writeParametersTable ( PrintWriter writer , String detailsSectionId , List < Parameter > parameters ) {
writer . println ( "[cols=\"3,2,3\"]" ) ;
writer . println ( "[cols=\"3,2,3\"]" ) ;
writer . println ( "|===" ) ;
writer . println ( "|===" ) ;
@ -139,7 +160,7 @@ public class DocumentPluginGoals extends DefaultTask {
writer . println ( ) ;
writer . println ( ) ;
for ( Parameter parameter : parameters ) {
for ( Parameter parameter : parameters ) {
String name = parameter . getName ( ) ;
String name = parameter . getName ( ) ;
writer . printf ( "| <<%s - %s,%s>>%n", detailsSectionId , name, name ) ;
writer . printf ( "| <<%s . %s,%s>>%n", detailsSectionId , parameterId( name) , name ) ;
writer . printf ( "| `%s`%n" , typeNameToJavadocLink ( shortTypeName ( parameter . getType ( ) ) , parameter . getType ( ) ) ) ;
writer . printf ( "| `%s`%n" , typeNameToJavadocLink ( shortTypeName ( parameter . getType ( ) ) , parameter . getType ( ) ) ) ;
String defaultValue = parameter . getDefaultValue ( ) ;
String defaultValue = parameter . getDefaultValue ( ) ;
if ( defaultValue ! = null ) {
if ( defaultValue ! = null ) {
@ -158,7 +179,7 @@ public class DocumentPluginGoals extends DefaultTask {
String name = parameter . getName ( ) ;
String name = parameter . getName ( ) ;
writer . println ( ) ;
writer . println ( ) ;
writer . println ( ) ;
writer . println ( ) ;
writer . printf ( "[[%s - %s]]%n", sectionId , name) ;
writer . printf ( "[[%s . %s]]%n", sectionId , parameterId( name) ) ;
writer . printf ( "=== `%s`%n" , name ) ;
writer . printf ( "=== `%s`%n" , name ) ;
writer . println ( parameter . getDescription ( ) ) ;
writer . println ( parameter . getDescription ( ) ) ;
writer . println ( ) ;
writer . println ( ) ;
@ -174,6 +195,20 @@ public class DocumentPluginGoals extends DefaultTask {
}
}
}
}
private String parameterId ( String name ) {
StringBuilder id = new StringBuilder ( name . length ( ) + 4 ) ;
for ( char c : name . toCharArray ( ) ) {
if ( Character . isLowerCase ( c ) ) {
id . append ( c ) ;
}
else {
id . append ( "-" ) ;
id . append ( Character . toLowerCase ( c ) ) ;
}
}
return id . toString ( ) ;
}
private void writeDetail ( PrintWriter writer , String name , String value ) {
private void writeDetail ( PrintWriter writer , String name , String value ) {
writer . printf ( "| %s%n" , name ) ;
writer . printf ( "| %s%n" , name ) ;
writer . printf ( "| `%s`%n" , value ) ;
writer . printf ( "| `%s`%n" , value ) ;