/*
 * Copyright (C) 2015 Stefan Niederhauser (nidin@gmx.ch)
 *
 * 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 guru.nidi.codeassert;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
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.project.MavenProject;

import static org.twdata.maven.mojoexecutor.MojoExecutor.*;

/**
 * Prepare the maven surefire plugin to run the tests with a Jacoco Agent.
 */
@Mojo(name = "prepare", defaultPhase = LifecyclePhase.INITIALIZE)
public class PrepareMojo extends AbstractMojo {

    @Parameter(defaultValue = "${project}", readonly = true)
    private MavenProject mavenProject;

    @Parameter(defaultValue = "${session}", readonly = true)
    private MavenSession mavenSession;

    @Component
    private BuildPluginManager pluginManager;

    public void execute() throws MojoExecutionException, MojoFailureException {
        executeMojo(
                plugin(
                        groupId("org.jacoco"),
                        artifactId("jacoco-maven-plugin"),
                        version("0.7.6.201602180812")
                ),
                goal("prepare-agent"),
                configuration(),
                executionEnvironment(mavenProject, mavenSession, pluginManager)
        );
    }
}
@@ -1165,24 +1165,43 @@
 enProject;%0A%0A
+import java.io.*;%0A%0A
 import stati
@@ -1248,16 +1248,16 @@
 utor.*;%0A
-
 %0A/**%0A * 
@@ -1791,16 +1791,153 @@
 ption %7B%0A
+        executeJacocoPlugin();%0A        writeArgLineFile();%0A    %7D%0A%0A    private void executeJacocoPlugin() throws MojoExecutionException %7B%0A
         
@@ -2304,16 +2304,16 @@
 anager)%0A
-
         
@@ -2320,10 +2320,560 @@
 );%0A    %7D
+%0A%0A    private void writeArgLineFile() %7B%0A        final String argLine = mavenProject.getProperties().getProperty(%22argLine%22);%0A        if (argLine != null) %7B%0A            final File file = new File(%22target/coverageOptions.txt%22);%0A            try (final Writer out = new OutputStreamWriter(new FileOutputStream(file), %22utf-8%22)) %7B%0A                out.write(argLine);%0A                getLog().info(%22Wrote argLine to %22 + file);%0A            %7D catch (IOException e) %7B%0A                getLog().warn(%22Could not write to %22 + file, e);%0A            %7D%0A        %7D%0A    %7D
 %0A%7D

write argLine into a file