Skip to content

Commit 52e4141

Browse files
committed
Merge #509 from remote-tracking branch 'origin/488-addLinksToPgToFluxCommands'
2 parents d8fe91a + 25a2779 commit 52e4141

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

metafacture-flux/src/main/java/org/metafacture/flux/HelpPrinter.java

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import org.metafacture.framework.annotations.Out;
2727
import org.metafacture.framework.annotations.ReturnsAvailableArguments;
2828

29+
import java.io.BufferedReader;
30+
import java.io.File;
31+
import java.io.FileReader;
2932
import java.io.IOException;
3033
import java.io.PrintStream;
3134
import java.lang.reflect.InvocationTargetException;
@@ -34,17 +37,22 @@
3437
import java.util.Arrays;
3538
import java.util.Collection;
3639
import java.util.Collections;
40+
import java.util.HashMap;
3741
import java.util.List;
3842
import java.util.Map;
3943
import java.util.Map.Entry;
4044

4145
/**
42-
* Prints Flux help for a given {@link ObjectFactory}
46+
* Prints Flux help for a given {@link ObjectFactory}.
47+
* If the file at {@value #PATH_TO_EXAMPLES} exists it's taken to insert links to examples and to the source code.
4348
*
4449
* @author Markus Michael Geipel
4550
*/
4651
public final class HelpPrinter {
4752

53+
private static final String PATH_TO_EXAMPLES = "../metafacture-documentation/linksAndExamples.tsv";
54+
private static final Map<String, String[]> EXAMPLES_MAP = new HashMap<>();
55+
4856
private HelpPrinter() {
4957
// no instances
5058
}
@@ -55,8 +63,9 @@ private HelpPrinter() {
5563
* @param args unused
5664
*
5765
* @see #print
66+
* @throws IOException when an I/O error occurs
5867
*/
59-
public static void main(final String[] args) {
68+
public static void main(final String[] args) throws IOException {
6069
FluxProgramm.printHelp(System.out);
6170
}
6271

@@ -66,9 +75,10 @@ public static void main(final String[] args) {
6675
*
6776
* @param factory the ObjectFactory
6877
* @param out the PrintStream to print to
78+
* @throws IOException when an I/O error occurs
6979
*/
7080
public static void print(final ObjectFactory<?> factory,
71-
final PrintStream out) {
81+
final PrintStream out) throws IOException {
7282
out.println("Welcome to Metafacture");
7383
out.println("======================");
7484
out.println();
@@ -77,9 +87,9 @@ public static void print(final ObjectFactory<?> factory,
7787
out.println("\nUsage:\tflux FLOW_FILE [VARNAME=VALUE ...]\n");
7888
out.println("Available flux commands:\n");
7989

80-
final List<String> keyWords = new ArrayList<String>();
81-
keyWords.addAll(factory.keySet());
90+
final List<String> keyWords = new ArrayList<>(factory.keySet());
8291
Collections.sort(keyWords);
92+
loadExamples();
8393
for (final String name : keyWords) {
8494
describe(name, factory, out);
8595
}
@@ -111,8 +121,37 @@ private static <T> void describe(final String name, final ObjectFactory<T> facto
111121
out.println("- arguments:\t" + arguments);
112122
}
113123

114-
final Map<String, Method> attributes = configurableClass.getSetters();
124+
printAttributes(out, configurableClass, configurableClass.getSetters());
125+
printSignature(out, moduleClass);
126+
127+
final String[] examplesEntry = EXAMPLES_MAP.get(name);
128+
if (examplesEntry != null && examplesEntry.length > 2) {
129+
out.println("- [example in Playground]" + "(" + examplesEntry[2] + ")");
130+
}
131+
if (examplesEntry != null && examplesEntry.length > 1) {
132+
out.println("- java class:\t[" + moduleClass.getCanonicalName() + "](" + examplesEntry[1] + ")");
133+
}
134+
else {
135+
out.println("- java class:\t" + moduleClass.getCanonicalName());
136+
}
137+
out.println();
138+
}
139+
140+
private static <T> void printSignature(final PrintStream out, final Class<? extends T> moduleClass) {
141+
String inString = "<unknown>";
142+
String outString = "";
143+
final In inClass = moduleClass.getAnnotation(In.class);
144+
if (inClass != null) {
145+
inString = inClass.value().getSimpleName();
146+
}
147+
final Out outClass = moduleClass.getAnnotation(Out.class);
148+
if (outClass != null) {
149+
outString = outClass.value().getSimpleName();
150+
}
151+
out.println("- signature:\t" + inString + " -> " + outString);
152+
}
115153

154+
private static <T> void printAttributes(final PrintStream out, final ConfigurableClass<? extends T> configurableClass, final Map<String, Method> attributes) {
116155
if (!attributes.isEmpty()) {
117156
out.print("- options:\t");
118157
final StringBuilder builder = new StringBuilder();
@@ -140,20 +179,6 @@ private static <T> void describe(final String name, final ObjectFactory<T> facto
140179
}
141180
out.println(builder.substring(0, builder.length() - 2));
142181
}
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();
157182
}
158183

159184
@SuppressWarnings("unchecked")
@@ -171,4 +196,20 @@ private static Collection<String> getAvailableArguments(final Class<?> moduleCla
171196
return Collections.emptyList();
172197
}
173198

199+
private static void loadExamples() throws IOException {
200+
final File f = new File(PATH_TO_EXAMPLES);
201+
if (f.exists()) {
202+
final BufferedReader bufferedReader = new BufferedReader(new FileReader(f));
203+
String line;
204+
while ((line = bufferedReader.readLine()) != null) {
205+
final String[] tsv = line.split("\t");
206+
EXAMPLES_MAP.put(tsv[0], tsv);
207+
208+
if (tsv.length < 2) {
209+
System.err.println("Invalid command info: " + line);
210+
}
211+
}
212+
}
213+
}
214+
174215
}

metafacture-flux/src/main/java/org/metafacture/flux/parser/FluxProgramm.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,10 @@ public void start() {
178178
/**
179179
* Prints the help to the given PrintStream.
180180
*
181-
* @param out the PrintStream to orint to
181+
* @param out the PrintStream to print to
182+
* @throws IOException when an I/O error occurs
182183
*/
183-
public static void printHelp(final PrintStream out) {
184+
public static void printHelp(final PrintStream out) throws IOException {
184185
HelpPrinter.print(COMMAND_FACTORY, out);
185186
}
186187

metafacture-flux/src/test/java/org/metafacture/flux/FluxProgrammTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
package org.metafacture.flux;
1818

19+
import org.junit.Test;
20+
import org.metafacture.flux.parser.FluxProgramm;
21+
1922
import java.io.IOException;
2023
import java.io.OutputStream;
2124
import java.io.PrintStream;
2225

23-
import org.junit.Test;
24-
import org.metafacture.flux.parser.FluxProgramm;
25-
2626
/**
2727
* Tests {@link FluxProgramm}
2828
*
@@ -33,7 +33,7 @@
3333
public final class FluxProgrammTest {
3434

3535
@Test
36-
public void testCommandRegistration() {
36+
public void testCommandRegistration() throws IOException {
3737
// all commands must properly load to print the help
3838
FluxProgramm.printHelp(discardOutput());
3939

0 commit comments

Comments
 (0)