Skip to content

Commit 3498dc8

Browse files
committed
Add links to flux-commands.md (#488)
The tsv to enrich the flux-commands.md is taken from the repo metafacture/metafacture-documentation.
1 parent d8fe91a commit 3498dc8

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed

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

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,34 @@
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;
3235
import java.lang.reflect.Method;
36+
import java.nio.file.Files;
3337
import java.util.ArrayList;
3438
import java.util.Arrays;
3539
import java.util.Collection;
3640
import java.util.Collections;
41+
import java.util.HashMap;
3742
import java.util.List;
3843
import java.util.Map;
3944
import java.util.Map.Entry;
4045

4146
/**
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.
4349
*
4450
* @author Markus Michael Geipel
4551
*/
4652
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;
4757

4858
private HelpPrinter() {
4959
// no instances
@@ -55,8 +65,9 @@ private HelpPrinter() {
5565
* @param args unused
5666
*
5767
* @see #print
68+
* @throws IOException when an I/O error occurs
5869
*/
59-
public static void main(final String[] args) {
70+
public static void main(final String[] args) throws IOException {
6071
FluxProgramm.printHelp(System.out);
6172
}
6273

@@ -66,9 +77,10 @@ public static void main(final String[] args) {
6677
*
6778
* @param factory the ObjectFactory
6879
* @param out the PrintStream to print to
80+
* @throws IOException when an I/O error occurs
6981
*/
7082
public static void print(final ObjectFactory<?> factory,
71-
final PrintStream out) {
83+
final PrintStream out) throws IOException {
7284
out.println("Welcome to Metafacture");
7385
out.println("======================");
7486
out.println();
@@ -77,9 +89,9 @@ public static void print(final ObjectFactory<?> factory,
7789
out.println("\nUsage:\tflux FLOW_FILE [VARNAME=VALUE ...]\n");
7890
out.println("Available flux commands:\n");
7991

80-
final List<String> keyWords = new ArrayList<String>();
81-
keyWords.addAll(factory.keySet());
92+
final List<String> keyWords = new ArrayList<>(factory.keySet());
8293
Collections.sort(keyWords);
94+
loadExamples();
8395
for (final String name : keyWords) {
8496
describe(name, factory, out);
8597
}
@@ -111,8 +123,42 @@ private static <T> void describe(final String name, final ObjectFactory<T> facto
111123
out.println("- arguments:\t" + arguments);
112124
}
113125

114-
final Map<String, Method> attributes = configurableClass.getSetters();
126+
printAttributes(out, configurableClass, configurableClass.getSetters());
127+
printSignature(out, moduleClass);
115128

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) {
116162
if (!attributes.isEmpty()) {
117163
out.print("- options:\t");
118164
final StringBuilder builder = new StringBuilder();
@@ -140,20 +186,6 @@ private static <T> void describe(final String name, final ObjectFactory<T> facto
140186
}
141187
out.println(builder.substring(0, builder.length() - 2));
142188
}
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();
157189
}
158190

159191
@SuppressWarnings("unchecked")
@@ -171,4 +203,16 @@ private static Collection<String> getAvailableArguments(final Class<?> moduleCla
171203
return Collections.emptyList();
172204
}
173205

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+
174218
}

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)