package com.redhat.ceylon.ant;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import com.redhat.ceylon.common.ant.Rep;
import com.redhat.ceylon.common.config.Repositories;
import com.redhat.ceylon.compiler.js.Runner;

/** Ant task to run Ceylon code compiled to JS from ant.
 * 
 * @author Enrique Zamudio
 */
public class Ceylonjs extends Task {

    private String nodePath;
    private String module;
    private String func = "run";
    private List<Rep> repos = new LinkedList<Rep>();
    private Rep sysrepo;

    /**
     * Adds a module repository
     * @param rep the new module repository
     */
    public void addRep(Rep rep) {
        repos.add(rep);
    }
    protected List<Rep> getRepositories() {
        return repos;
    }

    protected Rep getSystemRepository() {
        return sysrepo;
    }

    /**
     * Sets the system repository
     * @param rep the new system repository
     */
    public void setSysRep(Rep rep) {
        sysrepo = rep;
    }

    public void setModule(String value) {
        module = value;
    }
    public void setRun(String value) {
        func = value;
    }
    public void setNodePath(String value) {
        nodePath = value;
    }

    @Override
    public void execute() throws BuildException {
        if (nodePath == null) {
            try {
                nodePath = Runner.findNode();
            } catch (FileNotFoundException ex) {
                throw new BuildException("Cannot find Node.js executable", ex);
            }
        }
        if (module == null) {
            throw new BuildException("ceylonjs requires module attribute to be set");
        }
        if (func == null) {
            throw new BuildException("ceylonjs requires method attribute to be set");
        }
        List<String> reps = new ArrayList<String>(repos.size());
        for (Rep r : repos) {
            reps.add(r.toString());
        }
        if (reps.isEmpty()) {
            //Use defaults
            reps.add("modules");
            File r = Repositories.get().getSystemRepoDir();
            if (r != null) {
                reps.add(r.getAbsolutePath());
            }
            r = Repositories.get().getUserRepoDir();
            if (r != null) {
                reps.add(r.getAbsolutePath());
            }
        }
        try {
            Runner.run(reps, module, func, System.out, true);
        } catch (IOException ex) {
            throw new BuildException(ex);
        } catch (InterruptedException ex) {
            throw new BuildException(ex);
        }
    }
}

@@ -50,74 +50,8 @@
 le;%0A
-import java.io.FileNotFoundException;%0Aimport java.io.IOException;%0A
 impo
@@ -70,24 +70,24 @@
 .ArrayList;%0A
+
 import java.
@@ -337,22 +337,90 @@
 iler.js.
-Runner
+CeylonRunJsException;%0Aimport com.redhat.ceylon.compiler.js.CeylonRunJsTool
 ;%0A%0A/** A
@@ -1532,22 +1532,31 @@
 ePath = 
-Runner
+CeylonRunJsTool
 .findNod
@@ -1585,20 +1585,19 @@
 ch (
-FileNotFound
+CeylonRunJs
 Exce
@@ -2526,132 +2526,199 @@
     
-Runner.run(reps, module, func, System.out, true);%0A        %7D catch (IOException ex) %7B%0A            throw new BuildException(ex
+CeylonRunJsTool runner = new CeylonRunJsTool();%0A            runner.setRepositories(reps);%0A            runner.setModuleVersion(module);%0A            runner.setRun(func);%0A            runner.run(
 );%0A 
@@ -2737,19 +2737,8 @@
 ch (
-Interrupted
 Exce

Use the runner tool instead of the old Runner