package org.interestinglab.waterdrop.docutils;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.interestinglab.waterdrop.configparser.*;

/**
 * Created by gaoyingju on 16/09/2017.
 */
public class PluginDocCommand {

    public static void main(String[] args) throws Exception {

        CharStream charStream = CharStreams.fromFileName(args[0]);
        PluginDocLexer lexer = new PluginDocLexer(charStream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PluginDocParser parser = new PluginDocParser(tokens);

        PluginDocParser.WaterdropPluginContext context = parser.waterdropPlugin();
        PluginDocVisitor<String> visitor = new PluginDocMarkdownRender();
        String text = visitor.visit(context);

        System.out.println("Rendered Plugin Doc: \n" + text);
    }
}

@@ -161,32 +161,72 @@
 monTokenStream;%0A
+import org.apache.commons.io.FileUtils;%0A
 import org.inter
@@ -262,16 +262,126 @@
 ser.*;%0A%0A
+import java.io.File;%0Aimport java.nio.charset.Charset;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0A%0A
 /**%0A * C
@@ -511,24 +511,1122 @@
 xception %7B%0A%0A
+        if (args.length %3C 1 %7C%7C args.length %3E 2) %7B%0A            System.out.println(%22please specify %3Cplugin_doc_path%3E %5Boverwrite%5D%22);%0A            System.exit(-1);%0A        %7D%0A%0A        boolean overwrite = false;%0A        if (args.length == 2) %7B%0A            overwrite = Boolean.valueOf(args%5B1%5D);%0A        %7D%0A%0A        File file = new File(args%5B0%5D);%0A        if (!file.exists() %7C%7C !file.isFile()) %7B%0A            System.out.println(%22file does not exists or is not file%22);%0A            System.exit(-1);%0A        %7D%0A%0A        if (!file.getName().endsWith(%22.docs%22)) %7B%0A            System.out.println(%22plugin doc file shoud be named as *.docs%22);%0A            System.exit(-1);%0A        %7D%0A%0A        final Path parent = file.toPath().getParent();%0A        final String filenamePrefix = file.getName().substring(0, file.getName().length() - %22.docs%22.length());%0A        final File target = new File(Paths.get(parent.toString(), filenamePrefix + %22.md%22).toString());%0A%0A        if (!overwrite && target.exists()) %7B%0A            System.out.println(%22target rendered plugin doc file already exists%22);%0A            System.exit(-1);%0A        %7D%0A%0A
         Char
@@ -2087,59 +2087,116 @@
     
-System.out.println(%22Rendered Plugin Doc: %5Cn%22 + text
+FileUtils.writeStringToFile(target, text, Charset.defaultCharset());%0A%0A        System.out.println(%22Success !%22
 );%0A 

write rendered plugin doc to file