package com.google.dart;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map;
import java.util.Set;

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.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;
import org.codehaus.plexus.util.cli.StreamConsumer;
import org.codehaus.plexus.util.cli.WriterStreamConsumer;

import com.google.common.base.Throwables;
import com.google.dart.util.Pub;

/**
 * Goal to invoke the dart web compiler.
 *
 * @author nigel magnay
 */
@Mojo(name = "dwc", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class DartWebMojo extends DartMojo {

    private final static String ARGUMENT_OUT = "--out";

    @Parameter(property = "output", defaultValue = "${project.build.directory}/generated-sources/dwc")
    private File outputDir;

    @Parameter(property = "htmlFile")
    private String htmlFile;

    @Parameter(property = "dwcScript", defaultValue = "packages/web_ui/dwc.dart")
    private String dwcScript;

    @Parameter(property = "packageName")
    private String packageName;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {

        final Set<File> dartPackageRoots = findDartPackageRoots();
        processPubDependencies(dartPackageRoots);

        File sourceDirectory;

        if( packageName != null ) {
            Map<String, Pub> packages = getDartPackagesByName();
            Pub pub = packages.get(packageName);

            if( pub == null ) {
                getLog().info("No package named " + packageName);
                for( String name : packages.keySet() ) {
                    getLog().info("  " + name);
                }
                throw new MojoFailureException("Missing Package");
            }

            sourceDirectory = pub.getPath();
        } else {
            sourceDirectory = getBasedir();
        }

        checkDart();
        String dartPath = getDartExecutable().getAbsolutePath();

        if (getLog().isDebugEnabled()) {
            getLog().debug("Using dart '" + dartPath + "'.");
        }

        final Commandline cl = new Commandline();
        cl.setExecutable(dartPath);

	    cl.createArg().setValue(buildPackagePath());

        File dwc = new File(sourceDirectory, dwcScript);
        if( !dwc.exists() )
            throw new MojoExecutionException("The dwc script does not exist here: " + dwc.getAbsolutePath());

        cl.createArg().setValue(dwc.getAbsolutePath());

        // Ensure the output location exists.

        try {
            FileUtils.deleteDirectory(outputDir);
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }

        outputDir.mkdirs();

        cl.createArg().setValue(ARGUMENT_OUT);
        cl.createArg().setValue(outputDir.getAbsolutePath());


        // Root HTML:
        File html = new File(sourceDirectory, htmlFile);
        if( !html.exists() )
            throw new MojoExecutionException("The HTML file does not exist here: " + html.getAbsolutePath());
        cl.createArg().setValue(html.getAbsolutePath());



        final StreamConsumer output = new WriterStreamConsumer(new OutputStreamWriter(System.out));
        final StreamConsumer error = new WriterStreamConsumer(new OutputStreamWriter(System.err));

        getLog().info("Execute dart: " + cl.toString());

        System.out.println();
        System.out.println();

        try {

            final int returnValue = CommandLineUtils.executeCommandLine(cl, output, error);

            if (getLog().isDebugEnabled()) {
                getLog().debug("dart return code: " + returnValue);
            }
            if (returnValue != 0) {
                throw new MojoExecutionException("Dart returned error code " + returnValue);
            }
        } catch (final CommandLineException e) {
            getLog().debug("dart error: ", e);
        }

        System.out.println();
        System.out.println();
    }

}

@@ -107,30 +107,8 @@
 er;%0A
-import java.util.Map;%0A
 impo
@@ -125,16 +125,16 @@
 il.Set;%0A
+
 %0Aimport 
@@ -759,41 +759,8 @@
 les;
-%0Aimport com.google.dart.util.Pub;
 %0A%0A/*
@@ -1320,82 +1320,8 @@
 t;%0A%0A
-    @Parameter(property = %22packageName%22)%0A    private String packageName;%0A%0A
     
@@ -1474,24 +1474,24 @@
 ageRoots();%0A
+
         proc
@@ -1533,610 +1533,8 @@
 );%0A%0A
-        File sourceDirectory;%0A%0A        if( packageName != null ) %7B%0A            Map%3CString, Pub%3E packages = getDartPackagesByName();%0A            Pub pub = packages.get(packageName);%0A%0A            if( pub == null ) %7B%0A                getLog().info(%22No package named %22 + packageName);%0A                for( String name : packages.keySet() ) %7B%0A                    getLog().info(%22  %22 + name);%0A                %7D%0A                throw new MojoFailureException(%22Missing Package%22);%0A            %7D%0A%0A            sourceDirectory = pub.getPath();%0A        %7D else %7B%0A            sourceDirectory = getBasedir();%0A        %7D%0A%0A
     

remove support for packages in web projects (do one "package" per project)