package com.redhat.ceylon.eclipse.core.launch;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;

import com.redhat.ceylon.compiler.js.Runner;

import static org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME;

public class JsLaunchDelegate extends LaunchConfigurationDelegate {

    private MessageConsole findConsole() {
        ConsolePlugin plugin = ConsolePlugin.getDefault();
        IConsoleManager conman = plugin.getConsoleManager();
        MessageConsole cons = null;
        for (IConsole ccons : conman.getConsoles()) {
            if ("com.redhat.ceylon".equals(ccons.getName())) {
                cons = (MessageConsole)ccons;
                break;
            }
        }
        if (cons == null) {
            cons = new MessageConsole("com.redhat.ceylon", null);
            conman.addConsoles(new IConsole[]{cons});
        }
        cons.clearConsole();
        return cons;
    }

    @Override
    public void launch(ILaunchConfiguration configuration, String mode,
            ILaunch launch, IProgressMonitor monitor) throws CoreException {

        //Check that JS is enabled for the project
        String qname = configuration.getAttribute(ATTR_MAIN_TYPE_NAME, "::run");
        String methname = qname.substring(qname.indexOf("::")+2);
        String modname = configuration.getAttribute(ICeylonLaunchConfigurationConstants.ATTR_CEYLON_MODULE, "default");
        System.out.println("Launching " + methname + " from " + modname);
        ArrayList<String> repos = new ArrayList<String>(4);
        //Add workspace repo
        //Add project repo?
        //Add user repo
        repos.add("modules");
        PrintStream pout = new PrintStream(findConsole().newOutputStream());
        try {
            Runner.run(repos, modname, methname, pout);
        } catch (FileNotFoundException ex) {
            //Install node.js
            System.err.println(ex.getMessage());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            pout.close();
        }
    }

}

@@ -164,16 +164,60 @@
 yList;%0A%0A
+import org.eclipse.core.resources.IProject;%0A
 import o
@@ -684,16 +684,77 @@
 .Runner;
+%0Aimport com.redhat.ceylon.eclipse.core.builder.CeylonBuilder;
 %0A%0Aimport
@@ -2030,71 +2030,73 @@
     
-System.out.println(%22Launching %22 + methname + %22 from %22 + modname
+IProject proj = configuration.getMappedResources()%5B0%5D.getProject(
 );%0A 
@@ -2154,9 +2154,8 @@
 ng%3E(
-4
 );%0A 
@@ -2171,22 +2171,99 @@
 Add 
-workspace repo
+system repo%0A        //not working yet - repos.add(CeylonBuilder.getCeylonSystemRepo(proj));
 %0A   
@@ -2289,9 +2289,9 @@
 repo
-?
+s
 %0A   
@@ -2299,51 +2299,152 @@
     
-//Add user repo%0A        repos.add(%22modules%22
+repos.addAll(CeylonBuilder.getCeylonRepositories(proj));%0A        repos.add(CeylonBuilder.getCeylonModulesOutputDirectory(proj).getAbsolutePath()
 );%0A 

Add repos to the Runner so that it works