package com.indeed.proctor.consumer.gen.ant;

import com.google.common.base.Strings;
import com.indeed.proctor.consumer.gen.CodeGenException;
import org.apache.log4j.Logger;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Ant task for generating Proctor test groups files.
 *
 * @author andrewk
 */
public abstract class TestGroupsGeneratorTask extends Task {
    protected static final Logger LOGGER = Logger.getLogger(TestGroupsGeneratorTask.class);
    protected String input;
    protected String target;
    protected String packageName;
    protected String groupsClass;
    protected String specificationOutput;

    public String getInput() {
        return input;
    }

    public void setInput(final String input) {
        this.input = input;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(final String target) {
        this.target = target;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(final String packageName) {
        this.packageName = packageName;
    }

    public String getGroupsClass() {
        return groupsClass;
    }

    public void setGroupsClass(final String groupsClass) {
        this.groupsClass = groupsClass;
    }

    public String getSpecificationOutput() {
        return specificationOutput;
    }

    public void setSpecificationOutput(final String specificationOutput) {
        this.specificationOutput = specificationOutput;
    }

    /**
     * Use {@link TestGroupsGeneratorTask#totalSpecificationGenerator(List)} instead
     * @deprecated
     */
    @Deprecated
    protected void totalSpecificationGenerator(final File dir) throws CodeGenException {
        totalSpecificationGenerator(Arrays.asList(dir.listFiles()));
    }
    /*
     * Generates total specifications from any partial specifications found
     */
    protected void totalSpecificationGenerator(final List<File> files) throws CodeGenException {
        if (files == null || files.size() == 0) {
            throw new CodeGenException("No specifications file input");
        }
        final List<File> providedContextFiles =  new ArrayList<>();
        for (final File file : files) {
            if ("providedcontext.json".equals(file.getName())) {
                providedContextFiles.add(file);
            }
        }
        if (providedContextFiles.size() == 1) {
            //make directory if it doesn't exist
            (new File(specificationOutput.substring(0, specificationOutput.lastIndexOf(File.separator)))).mkdirs();
            final File specificationOutputFile = new File(specificationOutput);
            generateTotalSpecification(files, specificationOutputFile);
        } else {
            throw new CodeGenException("Incorrect amount of providedcontext.json in specified input folder");
        }
    }

    /**
     * Use {@link TestGroupsGeneratorTask#generateTotalSpecification(List, File)} instead
     * @deprecated
     */
    @Deprecated
    protected abstract void generateTotalSpecification(final File dir, final File specificationOutputFile) throws CodeGenException;

    protected abstract void generateTotalSpecification(final List<File> files, final File specificationOutputFile) throws CodeGenException;

    public void execute() throws BuildException {
        final String[] inputs = input.split(",");

        if (inputs.length == 0) {
            LOGGER.error("input shouldn't be empty");
            return;
        } else {
            final List<File> files = new ArrayList<>();

            for (final String input : inputs) {
                final File inputFile = new File(input.trim());
                if (inputFile == null) {
                    LOGGER.error("input not substituted with configured value");
                    return;
                }
                if (inputFile.isDirectory()) {
                    files.addAll(Arrays.asList(inputFile.listFiles()));
                } else {
                    files.add(inputFile);
                }
            }

            if (files.size() == 1) {
                try {
                    generateFile();
                } catch (final CodeGenException ex) {
                    throw new BuildException("Unable to generate code " + ex.toString(), ex);
                }
            } else {
                if (!Strings.isNullOrEmpty(getSpecificationOutput())) {
                    try {
                        totalSpecificationGenerator(files);
                    } catch (final CodeGenException e) {
                        throw new BuildException("Could not create total specification", e);
                    }
                } else {
                    throw new BuildException("Undefined output folder for generated specification");
                }
            }
        }
    }

    protected abstract void generateFile() throws CodeGenException;
}

@@ -4187,24 +4187,95 @@
          %7D%0A%0A
+            if (Strings.isNullOrEmpty(getSpecificationOutput())) %7B%0A    
             
@@ -4307,38 +4307,46 @@
                 
+    
 try %7B%0A
+    
                 
@@ -4361,24 +4361,28 @@
 rateFile();%0A
+    
             
@@ -4435,32 +4435,36 @@
                 
+    
 throw new BuildE
@@ -4529,34 +4529,41 @@
                 
-%7D%0A
+    %7D%0A   
             %7D el
@@ -4550,32 +4550,33 @@
 %0A               
+ 
 %7D else %7B%0A       
@@ -4588,71 +4588,132 @@
     
-if (!Strings.isNullOrEmpty(getSpecificationOutput())) %7B%0A       
+    throw new BuildException(%22Undefined output folder for generated specification%22);%0A                %7D%0A            %7D else %7B%0A
     
@@ -4717,34 +4717,33 @@
             
+   
 try %7B%0A
-    
             
@@ -4794,36 +4794,32 @@
                 
-    
 %7D catch (final C
@@ -4851,36 +4851,32 @@
                 
- 
-   
 throw new BuildE
@@ -4932,156 +4932,8 @@
 e);%0A
-                    %7D%0A                %7D else %7B%0A                    throw new BuildException(%22Undefined output folder for generated specification%22);%0A
     

Fix support for dir-style specs with no tests