package suryagaddipati.codeclimate;

import hudson.*;
import hudson.matrix.*;
import hudson.model.*;
import hudson.plugins.analysis.core.*;
import hudson.plugins.analysis.util.*;
import hudson.tasks.*;
import org.kohsuke.stapler.*;

import java.io.*;

public class CodeClimatePublisher extends HealthAwarePublisher{
    @DataBoundConstructor
    public CodeClimatePublisher(){
        super("CODECLIMATE");
    }

    @Override
    protected boolean perform(final Run<?, ?> build, final FilePath workspace, final Launcher launcher, final PluginLogger logger)
            throws IOException, InterruptedException {
//        String codeClimateCommand = "docker run " +
//                "  --interactive  --rm  " +
//                "  --env CODE_PATH=\"$PWD\" " +
//                "  --volume \"$PWD\":/code " +
//                "  --volume /var/run/docker.sock:/var/run/docker.sock " +
//                "  --volume /tmp/cc:/tmp/cc " +
//                "  codeclimate/codeclimate analyze -f json> code-climate-result.json";
//        Shell execution = new Shell("#!/bin/bash  -ex \n" + codeClimateCommand);
        try {
//            execution.perform(((AbstractBuild) build), launcher, launcher.getListener());
            FilesParser parser = new FilesParser("CODE_CLIMATE", "code-climate-result.json",
                    new CodeClimateResultParser(getDefaultEncoding()),
                    false, false);
            ParserResult project = ((AbstractBuild) build).getWorkspace().act(parser);
            CodeClimateResult result = new CodeClimateResult(build, getDefaultEncoding(), project,
                    usePreviousBuildAsReference(), useOnlyStableBuildsAsReference());
            build.addAction(new CodeClimateResultAction(build, this, result));

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    @Override
    public MatrixAggregator createAggregator(MatrixBuild matrixBuild, Launcher launcher, BuildListener buildListener) {
        return null;
    }
}


@@ -183,20 +183,31 @@
 ort 
-hudson.tasks
+org.apache.commons.lang
 .*;%0A
@@ -320,16 +320,127 @@
 lisher%7B%0A
+    private static final String DEFAULT_FILE_NAME = %22code-climate-result.json%22;%0A    private String resultFile;%0A
     @Dat
@@ -1401,34 +1401,70 @@
 E%22, 
-%22code-climate-result.json%22
+StringUtils.defaultIfEmpty(getResultFile(), DEFAULT_FILE_NAME)
 ,%0A  
@@ -2250,24 +2250,24 @@
 Listener) %7B%0A
-
         retu
@@ -2280,12 +2280,198 @@
 l;%0A    %7D
+%0A%0A    public String getResultFile() %7B%0A        return resultFile;%0A    %7D%0A    @DataBoundSetter%0A    public void setResultFile(String resultFile) %7B%0A        this.resultFile = resultFile;%0A    %7D
 %0A%7D%0A%0A

Allow specification of output file