-
Notifications
You must be signed in to change notification settings - Fork 55
/
help-class-source.vm
489 lines (440 loc) · 16 KB
/
help-class-source.vm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
## Licensed to the Apache Software Foundation (ASF) under one
## or more contributor license agreements. See the NOTICE file
## distributed with this work for additional information
## regarding copyright ownership. The ASF licenses this file
## to you under the Apache License, Version 2.0 (the
## "License"); you may not use this file except in compliance
## with the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing,
## software distributed under the License is distributed on an
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
## KIND, either express or implied. See the License for the
## specific language governing permissions and limitations
## under the License.
#if ($helpPackageName.length()>0)
package ${helpPackageName};
#end
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
#if ( $useAnnotations )
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
#end
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Display help information on ${artifactId}.<br>
* Call <code>mvn ${goalPrefix}:help -Ddetail=true -Dgoal=<goal-name></code> to display parameter details.
* @author maven-plugin-tools
#if ( !$useAnnotations )
* @goal help
* @requiresProject false
* @threadSafe
#end
*/
#if ( $useAnnotations )
@Mojo( name = "help", requiresProject = false, threadSafe = true )
#end
public class HelpMojo
extends AbstractMojo
{
/**
* If <code>true</code>, display all settable properties for each goal.
*
#if ( !$useAnnotations )
* @parameter property="detail" default-value="false"
#end
*/
#if ( $useAnnotations )
@Parameter( property = "detail", defaultValue = "false" )
#end
private boolean detail;
/**
* The name of the goal for which to show help. If unspecified, all goals will be displayed.
*
#if ( !$useAnnotations )
* @parameter property="goal"
#end
*/
#if ( $useAnnotations )
@Parameter( property = "goal" )
#end
private java.lang.String goal;
/**
* The maximum length of a display line, should be positive.
*
#if ( !$useAnnotations )
* @parameter property="lineLength" default-value="80"
#end
*/
#if ( $useAnnotations )
@Parameter( property = "lineLength", defaultValue = "80" )
#end
private int lineLength;
/**
* The number of spaces per indentation level, should be positive.
*
#if ( !$useAnnotations )
* @parameter property="indentSize" default-value="2"
#end
*/
#if ( $useAnnotations )
@Parameter( property = "indentSize", defaultValue = "2" )
#end
private int indentSize;
// /META-INF/maven/<groupId>/<artifactId>/plugin-help.xml
private static final String PLUGIN_HELP_PATH =
"/META-INF/maven/${pluginHelpPath}";
private static final int DEFAULT_LINE_LENGTH = 80;
private Document build()
throws MojoExecutionException
{
getLog().debug( "load plugin-help.xml: " + PLUGIN_HELP_PATH );
try ( InputStream is = getClass().getResourceAsStream( PLUGIN_HELP_PATH ) )
{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
return dBuilder.parse( is );
}
catch ( IOException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
catch ( ParserConfigurationException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
catch ( SAXException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
/**
* {@inheritDoc}
*/
@Override
public void execute()
throws MojoExecutionException
{
if ( lineLength <= 0 )
{
getLog().warn( "The parameter 'lineLength' should be positive, using '80' as default." );
lineLength = DEFAULT_LINE_LENGTH;
}
if ( indentSize <= 0 )
{
getLog().warn( "The parameter 'indentSize' should be positive, using '2' as default." );
indentSize = 2;
}
Document doc = build();
StringBuilder sb = new StringBuilder();
Node plugin = getSingleChild( doc, "plugin" );
String name = getValue( plugin, "name" );
String version = getValue( plugin, "version" );
String id = getValue( plugin, "groupId" ) + ":" + getValue( plugin, "artifactId" ) + ":" + version;
if ( isNotEmpty( name ) && !name.contains( id ) )
{
append( sb, name + " " + version, 0 );
}
else
{
if ( isNotEmpty( name ) )
{
append( sb, name, 0 );
}
else
{
append( sb, id, 0 );
}
}
append( sb, getValue( plugin, "description" ), 1 );
append( sb, "", 0 );
//<goalPrefix>plugin</goalPrefix>
String goalPrefix = getValue( plugin, "goalPrefix" );
Node mojos1 = getSingleChild( plugin, "mojos" );
List<Node> mojos = findNamedChild( mojos1, "mojo" );
if ( goal == null || goal.length() <= 0 )
{
append( sb, "This plugin has " + mojos.size() + ( mojos.size() > 1 ? " goals:" : " goal:" ), 0 );
append( sb, "", 0 );
}
for ( Node mojo : mojos )
{
writeGoal( sb, goalPrefix, (Element) mojo );
}
if ( getLog().isInfoEnabled() )
{
getLog().info( sb.toString() );
}
}
private static boolean isNotEmpty( String string )
{
return string != null && string.length() > 0;
}
private static String getValue( Node node, String elementName )
throws MojoExecutionException
{
return getSingleChild( node, elementName ).getTextContent();
}
private static Node getSingleChild( Node node, String elementName )
throws MojoExecutionException
{
List<Node> namedChild = findNamedChild( node, elementName );
if ( namedChild.isEmpty() )
{
throw new MojoExecutionException( "Could not find " + elementName + " in plugin-help.xml" );
}
if ( namedChild.size() > 1 )
{
throw new MojoExecutionException( "Multiple " + elementName + " in plugin-help.xml" );
}
return namedChild.get( 0 );
}
private static List<Node> findNamedChild( Node node, String elementName )
{
List<Node> result = new ArrayList<Node>();
NodeList childNodes = node.getChildNodes();
for ( int i = 0; i < childNodes.getLength(); i++ )
{
Node item = childNodes.item( i );
if ( elementName.equals( item.getNodeName() ) )
{
result.add( item );
}
}
return result;
}
private static Node findSingleChild( Node node, String elementName )
throws MojoExecutionException
{
List<Node> elementsByTagName = findNamedChild( node, elementName );
if ( elementsByTagName.isEmpty() )
{
return null;
}
if ( elementsByTagName.size() > 1 )
{
throw new MojoExecutionException( "Multiple " + elementName + "in plugin-help.xml" );
}
return elementsByTagName.get( 0 );
}
private void writeGoal( StringBuilder sb, String goalPrefix, Element mojo )
throws MojoExecutionException
{
String mojoGoal = getValue( mojo, "goal" );
Node configurationElement = findSingleChild( mojo, "configuration" );
Node description = findSingleChild( mojo, "description" );
if ( goal == null || goal.length() <= 0 || mojoGoal.equals( goal ) )
{
append( sb, goalPrefix + ":" + mojoGoal, 0 );
Node deprecated = findSingleChild( mojo, "deprecated" );
if ( ( deprecated != null ) && isNotEmpty( deprecated.getTextContent() ) )
{
append( sb, "Deprecated. " + deprecated.getTextContent(), 1 );
if ( detail && description != null )
{
append( sb, "", 0 );
append( sb, description.getTextContent(), 1 );
}
}
else if ( description != null )
{
append( sb, description.getTextContent(), 1 );
}
append( sb, "", 0 );
if ( detail )
{
Node parametersNode = getSingleChild( mojo, "parameters" );
List<Node> parameters = findNamedChild( parametersNode, "parameter" );
append( sb, "Available parameters:", 1 );
append( sb, "", 0 );
for ( Node parameter : parameters )
{
writeParameter( sb, parameter, configurationElement );
}
}
}
}
private void writeParameter( StringBuilder sb, Node parameter, Node configurationElement )
throws MojoExecutionException
{
String parameterName = getValue( parameter, "name" );
String parameterDescription = getValue( parameter, "description" );
Element fieldConfigurationElement = null;
if ( configurationElement != null )
{
fieldConfigurationElement = (Element) findSingleChild( configurationElement, parameterName );
}
String parameterDefaultValue = "";
if ( fieldConfigurationElement != null && fieldConfigurationElement.hasAttribute( "default-value" ) )
{
parameterDefaultValue = " (Default: " + fieldConfigurationElement.getAttribute( "default-value" ) + ")";
}
append( sb, parameterName + parameterDefaultValue, 2 );
Node deprecated = findSingleChild( parameter, "deprecated" );
if ( ( deprecated != null ) && isNotEmpty( deprecated.getTextContent() ) )
{
append( sb, "Deprecated. " + deprecated.getTextContent(), 3 );
append( sb, "", 0 );
}
append( sb, parameterDescription, 3 );
if ( "true".equals( getValue( parameter, "required" ) ) )
{
append( sb, "Required: Yes", 3 );
}
if ( ( fieldConfigurationElement != null ) && isNotEmpty( fieldConfigurationElement.getTextContent() ) )
{
String property = getPropertyFromExpression( fieldConfigurationElement.getTextContent() );
append( sb, "User property: " + property, 3 );
}
append( sb, "", 0 );
}
/**
* <p>Repeat a String <code>n</code> times to form a new string.</p>
*
* @param str String to repeat
* @param repeat number of times to repeat str
* @return String with repeated String
* @throws NegativeArraySizeException if <code>repeat < 0</code>
* @throws NullPointerException if str is <code>null</code>
*/
private static String repeat( String str, int repeat )
{
StringBuilder buffer = new StringBuilder( repeat * str.length() );
for ( int i = 0; i < repeat; i++ )
{
buffer.append( str );
}
return buffer.toString();
}
/**
* Append a description to the buffer by respecting the indentSize and lineLength parameters.
* <b>Note</b>: The last character is always a new line.
*
* @param sb The buffer to append the description, not <code>null</code>.
* @param description The description, not <code>null</code>.
* @param indent The base indentation level of each line, must not be negative.
*/
private void append( StringBuilder sb, String description, int indent )
{
for ( String line : toLines( description, indent, indentSize, lineLength ) )
{
sb.append( line ).append( '\n' );
}
}
/**
* Splits the specified text into lines of convenient display length.
*
* @param text The text to split into lines, must not be <code>null</code>.
* @param indent The base indentation level of each line, must not be negative.
* @param indentSize The size of each indentation, must not be negative.
* @param lineLength The length of the line, must not be negative.
* @return The sequence of display lines, never <code>null</code>.
* @throws NegativeArraySizeException if <code>indent < 0</code>
*/
private static List<String> toLines( String text, int indent, int indentSize, int lineLength )
{
List<String> lines = new ArrayList<String>();
String ind = repeat( "\t", indent );
String[] plainLines = text.split( "(\r\n)|(\r)|(\n)" );
for ( String plainLine : plainLines )
{
toLines( lines, ind + plainLine, indentSize, lineLength );
}
return lines;
}
/**
* Adds the specified line to the output sequence, performing line wrapping if necessary.
*
* @param lines The sequence of display lines, must not be <code>null</code>.
* @param line The line to add, must not be <code>null</code>.
* @param indentSize The size of each indentation, must not be negative.
* @param lineLength The length of the line, must not be negative.
*/
private static void toLines( List<String> lines, String line, int indentSize, int lineLength )
{
int lineIndent = getIndentLevel( line );
StringBuilder buf = new StringBuilder( 256 );
String[] tokens = line.split( " +" );
for ( String token : tokens )
{
if ( buf.length() > 0 )
{
if ( buf.length() + token.length() >= lineLength )
{
lines.add( buf.toString() );
buf.setLength( 0 );
buf.append( repeat( " ", lineIndent * indentSize ) );
}
else
{
buf.append( ' ' );
}
}
for ( int j = 0; j < token.length(); j++ )
{
char c = token.charAt( j );
if ( c == '\t' )
{
buf.append( repeat( " ", indentSize - buf.length() % indentSize ) );
}
else if ( c == '\u00A0' )
{
buf.append( ' ' );
}
else
{
buf.append( c );
}
}
}
lines.add( buf.toString() );
}
/**
* Gets the indentation level of the specified line.
*
* @param line The line whose indentation level should be retrieved, must not be <code>null</code>.
* @return The indentation level of the line.
*/
private static int getIndentLevel( String line )
{
int level = 0;
for ( int i = 0; i < line.length() && line.charAt( i ) == '\t'; i++ )
{
level++;
}
for ( int i = level + 1; i <= level + 4 && i < line.length(); i++ )
{
if ( line.charAt( i ) == '\t' )
{
level++;
break;
}
}
return level;
}
private static String getPropertyFromExpression( String expression )
{
if ( expression != null && expression.startsWith( "${" ) && expression.endsWith( "}" )
&& !expression.substring( 2 ).contains( "${" ) )
{
// expression="${xxx}" -> property="xxx"
return expression.substring( 2, expression.length() - 1 );
}
// no property can be extracted
return null;
}
}