package hu.advancedweb.maven;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.util.Map;
import java.util.Properties;

import static java.lang.String.format;

@Mojo(name = "prepare-agent", defaultPhase = LifecyclePhase.INITIALIZE,
        requiresDependencyResolution = ResolutionScope.RUNTIME,
        threadSafe = true)
public class ScottAgentMojo extends AbstractMojo {

    private static final String SCOTT_ARTIFACT_NAME = "hu.advancedweb:scott";
    private static final String ARG_LINE = "argLine";

    @Parameter(property = "project", readonly = true)
    private MavenProject project;

    @Parameter(property = "plugin.artifactMap", required = true, readonly = true)
    Map<String, Artifact> pluginArtifactMap;

    public void execute() throws MojoExecutionException, MojoFailureException {
        final Properties projectProperties = this.project.getProperties();
        final File agentJarFile = pluginArtifactMap.get(SCOTT_ARTIFACT_NAME).getFile();
        String arguments = getArgument(agentJarFile);
        getLog().info(ARG_LINE + " set to " + arguments);
        projectProperties.setProperty(ARG_LINE, arguments);
    }

    private String getArgument(final File agentJarFile) {
        return format("-javaagent:%s", agentJarFile);
    }

}

@@ -514,16 +514,39 @@
 o.File;%0A
+import java.util.List;%0A
 import j
@@ -559,16 +559,16 @@
 il.Map;%0A
-
 import j
@@ -627,16 +627,73 @@
 .format;
+%0Aimport static org.codehaus.plexus.util.StringUtils.join;
 %0A%0A@Mojo(
@@ -1251,16 +1251,360 @@
 ctMap;%0A%0A
+    @Parameter(property = %22scott.track.method_annotation%22)%0A    List%3CString%3E trackMethodAnnotations;%0A%0A    @Parameter(property = %22scott.inject_junit4_rule.method_annotation%22)%0A    List%3CString%3E junit4RuleMethodAnnotations;%0A%0A    @Parameter(property = %22scott.inject_junit5_extension.method_annotation%22)%0A    List%3CString%3E junit5RuleMethodAnnotations;%0A%0A
     publ
@@ -2075,16 +2075,16 @@
 File) %7B%0A
-
         
@@ -2123,23 +2123,578 @@
 gentJarFile)
+%0A            + createScottArgument(%22scott.track.method_annotation%22, trackMethodAnnotations)%0A            + createScottArgument(%22scott.inject_junit4_rule.method_annotation%22, junit4RuleMethodAnnotations)%0A            + createScottArgument(%22scott.inject_junit5_extension.method_annotation%22, junit5RuleMethodAnnotations);%0A    %7D%0A%0A    private String createScottArgument(String name, List%3CString%3E arguments) %7B%0A        if (arguments == null %7C%7C arguments.isEmpty())%0A            return %22%22;%0A%0A        return format(%22 -D%25s=%5C%22%25s%5C%22%22, name, join(arguments.iterator(), %22,%22))
 ;%0A    %7D%0A%0A%7D%0A

Create arguments to scott