package com.worldline.maven.plugin.externalTasksRunner;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.IOException;

/*
 * Copyright 2013-2014 Frédéric Langlade <frederic.langlade@atos.net>
 *                     Adrien Plagnol <adrien.plagnol@atos.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
@Mojo( name = "run", defaultPhase = LifecyclePhase.INITIALIZE )
public class SubTaskRunnerMojo extends AbstractMojo {
    @Parameter( defaultValue = "${os.name}", readonly = true)
    String osName;

    @Parameter( required = false )
    String taskRunnerPath;

    @Parameter( required = true)
    String taskRunnerName;

    @Parameter( required = true )
    String task;

    @Parameter( defaultValue = " ", required = true )
    String additionalParameters;


    /** Base working directory.
     *
     * @parameter default-value="${project.basedir}"
     * @required
     * @readonly
     */
	@Parameter(property = "external-tasks-maven.basedir", defaultValue = "${project-basedir}")
	protected File basedir;


    private org.apache.maven.project.MavenProject mavenProject;

    public void execute() throws MojoExecutionException {
        String params = "";
        if ( System.getProperty("env") != null ) {
            params += "--env=" + System.getProperty("env");
        }
        params += " " + defaultParamsFor(taskRunnerName) + " " + additionalParameters;

        if (taskRunnerPath == null) {
            taskRunnerPath = taskRunnerName;
        }

        executeCommand( taskRunnerPath + " " + task + " " + params, task );
    }

    private String defaultParamsFor(String taskRunnerName) {
        if (taskRunnerName == "grunt") {
            return "--no-color";
        }

        return "";
    }

    void executeCommand( String command, String taskName ) throws MojoExecutionException {
        try {
            if (isWindows()) {
                command = "cmd /c " + command;
            }

            getLog().debug( "Running command : "  + command );
            CommandLine cmdLine = CommandLine.parse( command );
            DefaultExecutor executor = new DefaultExecutor();
            executor.setWorkingDirectory(basedir);
            executor.execute( cmdLine );
        } catch (ExecuteException e) {
            getLog().error( "Task not found or failure : " + taskName );
            throw new MojoExecutionException( "Task not found or failure : " + taskName );
        } catch (IOException e) {
            throw new MojoExecutionException( "Unknown error when running grunt", e );
        }


    }


    private boolean isWindows() {
        return osName.startsWith("Windows");
    }
}

@@ -1669,16 +1669,210 @@
 eters;%0A%0A
+    @Parameter( defaultValue = %22false%22, required = true)%0A    Boolean isTestPhase;%0A%0A    @Parameter(property = %22maven.test.failure.ignore%22, defaultValue = %22false%22)%0A    Boolean ignoreTestsResults;%0A
 %0A    /**
@@ -2120,131 +2120,491 @@
 r;%0A%0A
-%0A    private org.apache.maven.project.MavenProject mavenProject;%0A%0A    public void execute() throws MojoExecutionException %7B
+    @Parameter(property = %22skipTests%22, defaultValue = %22false%22)%0A    protected Boolean isSkippingTests;%0A%0A    @Parameter(property = %22maven.test.skip%22, defaultValue = %22false%22)%0A    protected Boolean isSkippingTestsAlt;%0A%0A%0A    private org.apache.maven.project.MavenProject mavenProject;%0A%0A    public void execute() throws MojoExecutionException %7B%0A        if (isTestPhase && (isSkippingTests %7C%7C isSkippingTestsAlt)) %7B%0A            getLog().info(%22skipping tests%22);%0A            return;%0A        %7D
 %0A   
@@ -3769,32 +3769,167 @@
 %22 + taskName );%0A
+%0A            if (isTestPhase && ignoreTestsResults) %7B%0A                getLog().warn(%22ignoring tests result%22);%0A            %7D else %7B%0A    
             thro
@@ -3999,24 +3999,39 @@
 taskName );%0A
+            %7D%0A%0A
         %7D ca

support for tests ignore and skip