package org.mirah.maven;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

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

import java.util.ArrayList;
import java.util.List;

import org.codehaus.plexus.util.StringUtils;

import org.mirah.MirahCommand;

/**
 * Goal which compiles Mirah source files
 *
 * @goal compile
 * @phase compile
 * @requiresDependencyResolution compile
 */
public class MirahCompilerMojo extends AbstractMojo {
    /**
     * Project classpath.
     *
     * @parameter default-value="${project.compileClasspathElements}"
     * @required
     * @readonly
     */
    private List<String> classpathElements;
    /**
     * Classes destination directory
     * @parameter expression="${project.build.outputDirectory}"
     */
    private File outputDirectory;
    /**
     * Classes source directory
     * @parameter expression="src/main/mirah"
     */
    private File sourceDirectory;
    /**
     * Whether produce bytecode or java source
     * @parameter bytecode, default true
     */
    private boolean bytecode = true;
    /**
     * Show log
     * @parameter verbose, default false
     */
    private boolean verbose;

    public void execute() throws MojoExecutionException {
        if ( !outputDirectory.exists() ) {
            outputDirectory.mkdirs();
        }

        List arguments = new ArrayList();
        if (!bytecode)
          arguments.add("--java");
        if (verbose)
          arguments.add("-V");

        arguments.add("-d");
        arguments.add(outputDirectory.getAbsolutePath());

        try {
            arguments.add("-c");
            arguments.add(StringUtils.join(classpathElements.iterator(), File.pathSeparator));

            arguments.add(sourceDirectory.getAbsolutePath());

            MirahCommand.compile(arguments);
        } catch (Exception e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
}

@@ -338,20 +338,9 @@
 %0A * 
-Goal which c
+C
 ompi
@@ -396,24 +396,39 @@
 ase compile%0A
+ * @threadSafe%0A
  * @requires
@@ -711,32 +711,267 @@
 ements;%0A    /**%0A
+     * The source directories containing the sources to be compiled.%0A     *%0A     * @parameter default-value=%22$%7Bproject.compileSourceRoots%7D%22%0A     * @required%0A     * @readonly%0A     */%0A    private List%3CString%3E compileSourceRoots;%0A    /**%0A
      * Classes d
@@ -1538,14 +1538,328 @@
     
+if (bytecode) %7B%0A          executeMirahCompiler(outputDirectory.getAbsolutePath());%0A       %7D else %7B%0A          String javaSourceRoot = compileSourceRoots.get(0);%0A          executeMirahCompiler(javaSourceRoot);%0A       %7D%0A    %7D%0A%0A    private void executeMirahCompiler(String output) throws MojoExecutionException %7B%0A      
  if (
- 
 !out
@@ -1879,17 +1879,16 @@
 exists()
- 
 ) %7B%0A    
@@ -2110,24 +2110,24 @@
 .add(%22-d%22);%0A
+
         argu
@@ -2146,35 +2146,8 @@
 tput
-Directory.getAbsolutePath()
 );%0A%0A

use compileSourceRoot to get the output directory when we generate java source