package com.redhat.ceylon.maven;

import com.redhat.ceylon.maven.compiler.ExtendedCompilerOptions;
import com.redhat.ceylon.maven.compiler.JavaCompilerImpl;
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 com.redhat.ceylon.compiler.java.runtime.tools.*;
import com.redhat.ceylon.compiler.java.runtime.tools.Compiler;
import org.apache.maven.shared.model.fileset.util.FileSetManager;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
 */
@Mojo(name = "compile", defaultPhase = LifecyclePhase.COMPILE)
public class CeylonCompileMojo extends AbstractMojo {

  @Parameter(defaultValue = "false")
  private boolean verbose;

  @Parameter(defaultValue = "${project.build.directory}/modules")
  private File targetModulesDirectory;

  @Parameter()
  private List sources;

  @Parameter
  private String[] userRepos;

  @Parameter
  private String target;

  public void execute() throws MojoExecutionException, MojoFailureException {
    FileSetManager fileSetManager = new FileSetManager();
    for (Source source : (List<Source>)sources) {
      File sourcePath = new File(source.getFileset().getDirectory());
      Compiler compiler = new JavaCompilerImpl();
      ExtendedCompilerOptions options = new ExtendedCompilerOptions();
      options.setSourcePath(Collections.singletonList(sourcePath));
      options.setOutputRepository(targetModulesDirectory.getAbsolutePath());
      if (target != null) {
        options.setTarget(target);
      }
      options.setVerbose(verbose);
      if (userRepos != null) {
        for (String userRepo : userRepos) {
          options.addUserRepository(new File(userRepo).getAbsolutePath());
        }
      }
      ArrayList<File> sources = new ArrayList<File>();
      for (String included : fileSetManager.getIncludedFiles(source.getFileset())) {
        sources.add(new File(sourcePath, included));
      }
      options.setFiles(sources);
      boolean ok = compiler.compile(options, new CompilationListener() {

        public void error(File file, long line, long column, String message) {
          String msg;
          if (file != null) {
            msg = "Compilation error at (" + line + "," + column + ") in " +
                file.getAbsolutePath() + ":" + message;
          } else {
            msg = "Compilation error:" + message;
          }
          System.out.println(msg);
        }

        public void warning(File file, long line, long column, String message) {
          String msg;
          if (file != null) {
            msg = "Compilation warning at (" + line + "," + column + ") in " +
                file.getAbsolutePath() + ":" + message;
          } else {
            msg = "Compilation warning:" + message;
          }
          System.out.println(msg);
        }

        public void moduleCompiled(String module, String version) {
          System.out.println("Compiled module " + module + "/" + version);
        }
      });

      if (!ok) {
        throw new MojoExecutionException("Compilation failed");
      }
    }
  }
}

@@ -704,16 +704,41 @@
 ayList;%0A
+import java.util.Arrays;%0A
 import j
@@ -776,19 +776,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A/**%0A
@@ -2109,24 +2109,227 @@
   %7D%0A      %7D%0A
+      Set%3CFile%3E excluded = new HashSet%3CFile%3E();%0A      for (String excludedFile : fileSetManager.getExcludedFiles(source.getFileset())) %7B%0A        excluded.add(new File(sourcePath, excludedFile));%0A      %7D%0A
       ArrayL
@@ -2397,16 +2397,20 @@
 included
+File
  : fileS
@@ -2472,20 +2472,24 @@
     
-sources.add(
+File included = 
 new 
@@ -2513,19 +2513,109 @@
 included
-));
+File);%0A        if (!excluded.contains(included)) %7B%0A          sources.add(included);%0A        %7D
 %0A      %7D

Make exclusion work for compiler