package sbtjooq.codegen.tool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.toList;


public class GenerationTool {

    private static final Logger logger = LoggerFactory.getLogger(GenerationTool.class);

    private final MethodHandle delegate;
    private final List<Path> configurations;

    public GenerationTool(MethodHandle delegate, List<Path> configurations) {
        this.delegate = delegate;
        this.configurations = configurations;
    }

    public void generate() throws Throwable {
        invokeDelegate();
    }

    private void invokeDelegate() throws Throwable {
        final String[] args = configurations.stream().map(Path::toString).toArray(String[]::new);
        //noinspection ConfusingArgumentToVarargsMethod
        delegate.invokeExact(args);
    }


    public static Class<?> detectGenerationToolClass() throws ClassNotFoundException {
        try {
            return Class.forName("org.jooq.codegen.GenerationTool");
        } catch (ClassNotFoundException e1) {
            try {
                return Class.forName("org.jooq.util.GenerationTool");
            } catch (ClassNotFoundException e2) {
                e2.addSuppressed(e1);
                throw e2;
            }
        }
    }

    public static MethodHandle getMainMethod(Class<?> mainClass) throws ReflectiveOperationException {
        final MethodType mainType = MethodType.methodType(void.class, String[].class);
        return MethodHandles.publicLookup().findStatic(mainClass, "main", mainType);
    }

    public static void showJooqLogo() {
        try {
            Class.forName("org.jooq.impl.DefaultRenderContext");
        } catch (ClassNotFoundException ignored) {
        }
    }

    public static void main(String[] args) throws Throwable {
        final Class<?> genToolClass = detectGenerationToolClass();
        logger.debug("Delegate to detected {}", genToolClass.getName());

        final MethodHandle delegate = getMainMethod(genToolClass);
        final List<Path> configurations = Arrays.stream(args).map(Paths::get).collect(toList());

        showJooqLogo();

        new GenerationTool(delegate, configurations).generate();
    }

}

@@ -199,162 +199,8 @@
 pe;%0A
-import java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0Aimport static java.util.stream.Collectors.toList;%0A
 %0A%0Apu
@@ -380,32 +380,20 @@
 nal 
-List%3CPath%3E configuration
+String%5B%5D arg
 s;%0A%0A
@@ -445,32 +445,20 @@
 te, 
-List%3CPath%3E configuration
+String%5B%5D arg
 s) %7B
@@ -509,38 +509,18 @@
 his.
-configurations = configuration
+args = arg
 s;%0A 
@@ -661,106 +661,8 @@
 e %7B%0A
-        final String%5B%5D args = configurations.stream().map(Path::toString).toArray(String%5B%5D::new);%0A
     
@@ -968,17 +968,16 @@
 eption e
-1
 ) %7B%0A    
@@ -1104,18 +1104,26 @@
 ception 
-e2
+suppressed
 ) %7B%0A    
@@ -1135,17 +1135,16 @@
        e
-2
 .addSupp
@@ -1154,10 +1154,18 @@
 sed(
-e1
+suppressed
 );%0A 
@@ -1190,9 +1190,8 @@
 ow e
-2
 ;%0A  
@@ -1829,239 +1829,130 @@
     
-logger.debug(%22Delegate to detected %7B%7D%22, genToolClass.getName());%0A%0A        final MethodHandle delegate = getMainMethod(genToolClass);%0A        final List%3CPath%3E configurations = Arrays.stream(args).map(Paths::get).collect(toList()
+final MethodHandle delegate = getMainMethod(genToolClass);%0A        logger.debug(%22Delegate to detected %7B%7D%22, genToolClass
 );%0A
-%0A
     
@@ -1971,17 +1971,16 @@
 Logo();%0A
-%0A
         
@@ -2012,21 +2012,11 @@
 te, 
-configuration
+arg
 s).g

Simplify GenerationTool