Skip to content

Commit

Permalink
Add links to flux-commands.md (#488)
Browse files Browse the repository at this point in the history
The tsv to enrich the flux-commands.md is taken from the repo
metafacture/metafacture-documentation.
  • Loading branch information
dr0i committed Nov 20, 2023
1 parent d8fe91a commit b378ba4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,32 @@
import org.metafacture.framework.annotations.Out;
import org.metafacture.framework.annotations.ReturnsAvailableArguments;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
* Prints Flux help for a given {@link ObjectFactory}
* Prints Flux help for a given {@link ObjectFactory}.
* If the file at {@value #PATH_TO_EXAMPLES} exists it's taken to insert links to examples and to the source code.
*
* @author Markus Michael Geipel
*/
public final class HelpPrinter {
private static final String PATH_TO_EXAMPLES = "../metafacture-documentation/linksAndExamples.tsv";
private static HashMap<String, List<String>> examplesMap = new HashMap<>();

private HelpPrinter() {
// no instances
Expand All @@ -56,7 +64,7 @@ private HelpPrinter() {
*
* @see #print
*/
public static void main(final String[] args) {
public static void main(final String[] args) throws IOException {
FluxProgramm.printHelp(System.out);
}

Expand All @@ -68,7 +76,7 @@ public static void main(final String[] args) {
* @param out the PrintStream to print to
*/
public static void print(final ObjectFactory<?> factory,
final PrintStream out) {
final PrintStream out) throws IOException {
out.println("Welcome to Metafacture");
out.println("======================");
out.println();
Expand All @@ -80,6 +88,7 @@ public static void print(final ObjectFactory<?> factory,
final List<String> keyWords = new ArrayList<String>();
keyWords.addAll(factory.keySet());
Collections.sort(keyWords);
loadExamples(PATH_TO_EXAMPLES);
for (final String name : keyWords) {
describe(name, factory, out);
}
Expand All @@ -94,12 +103,17 @@ private static String getVersionInfo() {
}
}

private static <T> void describe(final String name, final ObjectFactory<T> factory, final PrintStream out) { // checkstyle-disable-line ExecutableStatementCount
private static <T> void describe(final String name, final ObjectFactory<T> factory, final PrintStream out) throws IOException { // checkstyle-disable-line ExecutableStatementCount
final ConfigurableClass<? extends T> configurableClass = factory.get(name);
final Class<? extends T> moduleClass = configurableClass.getPlainClass();
final Description desc = moduleClass.getAnnotation(Description.class);

out.println(name);
if (examplesMap.get(name) != null && examplesMap.get(name).size() == 3) {
out.println("[" + name + "](" + examplesMap.get(name).get(2) + ")");
}
else {
out.println(name);
}
name.chars().forEach(c -> out.print("-"));
out.println();

Expand Down Expand Up @@ -152,7 +166,12 @@ private static <T> void describe(final String name, final ObjectFactory<T> facto
outString = outClass.value().getSimpleName();
}
out.println("- signature:\t" + inString + " -> " + outString);
out.println("- java class:\t" + moduleClass.getCanonicalName());
if (examplesMap.get(name) != null) {
out.println("- java class:\t[" + moduleClass.getCanonicalName() + "](" + examplesMap.get(name).get(1) + ")");
}
else {
out.println("- java class:\t" + moduleClass.getCanonicalName());
}
out.println();
}

Expand All @@ -171,4 +190,18 @@ private static Collection<String> getAvailableArguments(final Class<?> moduleCla
return Collections.emptyList();
}

private static HashMap<String, List<String>> loadExamples(final String fname) throws IOException {
final File f = new File(fname);
if (Files.exists(f.toPath())) {
final BufferedReader bufferedReader = new BufferedReader(new FileReader(f));
final StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
final List<String> tsv = Arrays.asList(line.split("\t"));
examplesMap.put(tsv.get(0), tsv);
}
}
return examplesMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void start() {
*
* @param out the PrintStream to orint to
*/
public static void printHelp(final PrintStream out) {
public static void printHelp(final PrintStream out) throws IOException {
HelpPrinter.print(COMMAND_FACTORY, out);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package org.metafacture.flux;

import org.junit.Test;
import org.metafacture.flux.parser.FluxProgramm;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import org.junit.Test;
import org.metafacture.flux.parser.FluxProgramm;

/**
* Tests {@link FluxProgramm}
*
Expand All @@ -33,7 +33,7 @@
public final class FluxProgrammTest {

@Test
public void testCommandRegistration() {
public void testCommandRegistration() throws IOException {
// all commands must properly load to print the help
FluxProgramm.printHelp(discardOutput());

Expand Down

0 comments on commit b378ba4

Please sign in to comment.