26
26
import org .metafacture .framework .annotations .Out ;
27
27
import org .metafacture .framework .annotations .ReturnsAvailableArguments ;
28
28
29
+ import java .io .BufferedReader ;
30
+ import java .io .File ;
31
+ import java .io .FileReader ;
29
32
import java .io .IOException ;
30
33
import java .io .PrintStream ;
31
34
import java .lang .reflect .InvocationTargetException ;
32
35
import java .lang .reflect .Method ;
36
+ import java .nio .file .Files ;
33
37
import java .util .ArrayList ;
34
38
import java .util .Arrays ;
35
39
import java .util .Collection ;
36
40
import java .util .Collections ;
41
+ import java .util .HashMap ;
37
42
import java .util .List ;
38
43
import java .util .Map ;
39
44
import java .util .Map .Entry ;
40
45
41
46
/**
42
- * Prints Flux help for a given {@link ObjectFactory}
47
+ * Prints Flux help for a given {@link ObjectFactory}.
48
+ * If the file at {@value #PATH_TO_EXAMPLES} exists it's taken to insert links to examples and to the source code.
43
49
*
44
50
* @author Markus Michael Geipel
45
51
*/
46
52
public final class HelpPrinter {
53
+ private static final String PATH_TO_EXAMPLES = "../metafacture-documentation/linksAndExamples.tsv" ;
54
+ private static final Map <String , String []> EXAMPLES_MAP = new HashMap <>();
55
+ private static final int COLUMN_TO_PG_EXAMPLE = 3 ;
56
+ private static final int REQUIRED_COLUMNS_OF_EXAMPLE = 2 ;
47
57
48
58
private HelpPrinter () {
49
59
// no instances
@@ -55,8 +65,9 @@ private HelpPrinter() {
55
65
* @param args unused
56
66
*
57
67
* @see #print
68
+ * @throws IOException when an I/O error occurs
58
69
*/
59
- public static void main (final String [] args ) {
70
+ public static void main (final String [] args ) throws IOException {
60
71
FluxProgramm .printHelp (System .out );
61
72
}
62
73
@@ -66,9 +77,10 @@ public static void main(final String[] args) {
66
77
*
67
78
* @param factory the ObjectFactory
68
79
* @param out the PrintStream to print to
80
+ * @throws IOException when an I/O error occurs
69
81
*/
70
82
public static void print (final ObjectFactory <?> factory ,
71
- final PrintStream out ) {
83
+ final PrintStream out ) throws IOException {
72
84
out .println ("Welcome to Metafacture" );
73
85
out .println ("======================" );
74
86
out .println ();
@@ -77,9 +89,9 @@ public static void print(final ObjectFactory<?> factory,
77
89
out .println ("\n Usage:\t flux FLOW_FILE [VARNAME=VALUE ...]\n " );
78
90
out .println ("Available flux commands:\n " );
79
91
80
- final List <String > keyWords = new ArrayList <String >();
81
- keyWords .addAll (factory .keySet ());
92
+ final List <String > keyWords = new ArrayList <>(factory .keySet ());
82
93
Collections .sort (keyWords );
94
+ loadExamples ();
83
95
for (final String name : keyWords ) {
84
96
describe (name , factory , out );
85
97
}
@@ -111,8 +123,42 @@ private static <T> void describe(final String name, final ObjectFactory<T> facto
111
123
out .println ("- arguments:\t " + arguments );
112
124
}
113
125
114
- final Map <String , Method > attributes = configurableClass .getSetters ();
126
+ printAttributes (out , configurableClass , configurableClass .getSetters ());
127
+ printSignature (out , moduleClass );
115
128
129
+ final String [] examplesEntry = EXAMPLES_MAP .get (name );
130
+ if (!EXAMPLES_MAP .isEmpty () && (examplesEntry == null || examplesEntry .length < REQUIRED_COLUMNS_OF_EXAMPLE )) {
131
+ throw new MetafactureException (
132
+ "Failed to load build infos: tsv with links hasn't at least " + REQUIRED_COLUMNS_OF_EXAMPLE +
133
+ " for the entry '" + name + "'" );
134
+ }
135
+ if (examplesEntry != null && examplesEntry .length == COLUMN_TO_PG_EXAMPLE ) {
136
+ out .println ("- [example in Playground]" + "(" + examplesEntry [COLUMN_TO_PG_EXAMPLE - 1 ] + ")" );
137
+ }
138
+ if (examplesEntry != null ) {
139
+ out .println ("- java class:\t [" + moduleClass .getCanonicalName () + "](" + examplesEntry [1 ] + ")" );
140
+ }
141
+ else {
142
+ out .println ("- java class:\t " + moduleClass .getCanonicalName ());
143
+ }
144
+ out .println ();
145
+ }
146
+
147
+ private static <T > void printSignature (final PrintStream out , final Class <? extends T > moduleClass ) {
148
+ String inString = "<unknown>" ;
149
+ String outString = "" ;
150
+ final In inClass = moduleClass .getAnnotation (In .class );
151
+ if (inClass != null ) {
152
+ inString = inClass .value ().getSimpleName ();
153
+ }
154
+ final Out outClass = moduleClass .getAnnotation (Out .class );
155
+ if (outClass != null ) {
156
+ outString = outClass .value ().getSimpleName ();
157
+ }
158
+ out .println ("- signature:\t " + inString + " -> " + outString );
159
+ }
160
+
161
+ private static <T > void printAttributes (final PrintStream out , final ConfigurableClass <? extends T > configurableClass , final Map <String , Method > attributes ) {
116
162
if (!attributes .isEmpty ()) {
117
163
out .print ("- options:\t " );
118
164
final StringBuilder builder = new StringBuilder ();
@@ -140,20 +186,6 @@ private static <T> void describe(final String name, final ObjectFactory<T> facto
140
186
}
141
187
out .println (builder .substring (0 , builder .length () - 2 ));
142
188
}
143
-
144
- String inString = "<unknown>" ;
145
- String outString = "" ;
146
- final In inClass = moduleClass .getAnnotation (In .class );
147
- if (inClass != null ) {
148
- inString = inClass .value ().getSimpleName ();
149
- }
150
- final Out outClass = moduleClass .getAnnotation (Out .class );
151
- if (outClass != null ) {
152
- outString = outClass .value ().getSimpleName ();
153
- }
154
- out .println ("- signature:\t " + inString + " -> " + outString );
155
- out .println ("- java class:\t " + moduleClass .getCanonicalName ());
156
- out .println ();
157
189
}
158
190
159
191
@ SuppressWarnings ("unchecked" )
@@ -171,4 +203,16 @@ private static Collection<String> getAvailableArguments(final Class<?> moduleCla
171
203
return Collections .emptyList ();
172
204
}
173
205
206
+ private static void loadExamples () throws IOException {
207
+ final File f = new File (PATH_TO_EXAMPLES );
208
+ if (Files .exists (f .toPath ())) {
209
+ final BufferedReader bufferedReader = new BufferedReader (new FileReader (f ));
210
+ String line ;
211
+ while ((line = bufferedReader .readLine ()) != null ) {
212
+ final String [] tsv = line .split ("\t " );
213
+ EXAMPLES_MAP .put (tsv [0 ], tsv );
214
+ }
215
+ }
216
+ }
217
+
174
218
}
0 commit comments