/*
 * Copyright 2014 the original author or authors.
 *
 * 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.
 */

package org.gradle.sonar.runner.tasks;

import com.beust.jcommander.internal.Maps;
import com.google.common.base.Joiner;
import org.gradle.api.DefaultTask;
import org.gradle.api.Incubating;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
import org.gradle.process.JavaForkOptions;
import org.gradle.process.internal.DefaultJavaForkOptions;
import org.gradle.process.internal.JavaExecHandleBuilder;
import org.gradle.sonar.runner.SonarRunnerExtension;

import javax.inject.Inject;
import java.util.Map;

/**
 * Analyses one or more projects with the <a href="http://docs.codehaus.org/display/SONAR/Analyzing+with+Sonar+Runner">Sonar Runner</a>.
 * <p>
 * Can be used with or without the {@code "sonar-runner"} plugin.
 * If used together with the plugin, {@code sonarProperties} will be populated with defaults based on Gradle's object model and user-defined
 * values configured via {@link SonarRunnerExtension} and {@link org.gradle.sonar.runner.SonarRunnerRootExtension}.
 * If used without the plugin, all properties have to be configured manually.
 * <p>
 * For more information on how to configure the Sonar Runner, and on which properties are available, see the
 * <a href="http://docs.codehaus.org/display/SONAR/Analyzing+with+SonarQube+Runner">Sonar Runner documentation</a>.
 */
@Incubating
public class SonarRunner extends DefaultTask {

    private static final Logger LOGGER = Logging.getLogger(SonarRunner.class);
    private static final String MAIN_CLASS_NAME = "org.sonar.runner.Main";

    private JavaForkOptions forkOptions;
    private Map<String, Object> sonarProperties;

    @TaskAction
    public void run() {
        prepareExec().build().start().waitForFinish().assertNormalExitValue();
    }

    JavaExecHandleBuilder prepareExec() {
        Map<String, Object> properties = getSonarProperties();

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Executing Sonar Runner with properties:\n[{}]", Joiner.on(", ").withKeyValueSeparator(": ").join(properties));
        }

        JavaExecHandleBuilder javaExec = new JavaExecHandleBuilder(getFileResolver());
        getForkOptions().copyTo(javaExec);

        FileCollection sonarRunnerConfiguration = getProject().getConfigurations().getAt(SonarRunnerExtension.SONAR_RUNNER_CONFIGURATION_NAME);

        return javaExec
                .setClasspath(sonarRunnerConfiguration)
                .setMain(MAIN_CLASS_NAME)
                .systemProperties(properties);
    }

    /**
     * Options for the analysis process. Configured via {@link org.gradle.sonar.runner.SonarRunnerRootExtension#forkOptions}.
     */
    public JavaForkOptions getForkOptions() {
        if (forkOptions == null) {
            forkOptions = new DefaultJavaForkOptions(getFileResolver());
        }

        return forkOptions;
    }

    /**
     * The String key/value pairs to be passed to the Sonar Runner.
     *
     * {@code null} values are not permitted.
     */
    @Input
    public Map<String, Object> getSonarProperties() {
        if (sonarProperties == null) {
            sonarProperties = Maps.newLinkedHashMap();
        }

        return sonarProperties;
    }

    @Inject
    protected FileResolver getFileResolver() {
        throw new UnsupportedOperationException();
    }

}

@@ -1257,16 +1257,46 @@
 tension;
+%0Aimport org.gradle.util.GUtil;
 %0A%0Aimport
@@ -1333,16 +1333,66 @@
 ava.
-util.Map
+io.File;%0Aimport java.util.Map;%0Aimport java.util.Properties
 ;%0A%0A/
@@ -3186,23 +3186,356 @@
     
-return javaExec
+Properties propertiesObject = new Properties();%0A        propertiesObject.putAll(properties);%0A        File propertyFile = new File(getTemporaryDir(), %22sonar-project.properties%22);%0A        GUtil.saveProperties(propertiesObject, propertyFile);%0A%0A        return javaExec%0A                .systemProperty(%22project.settings%22, propertyFile.getAbsolutePath())
 %0A   
@@ -3632,54 +3632,8 @@
 AME)
-%0A                .systemProperties(properties)
 ;%0A  

Use a properties file to configure the Sonar Runner to avoid long command lines.