package rhogenwizard.sdk.task;

import java.io.IOException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.IStreamListener;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.IStreamMonitor;

import rhogenwizard.Activator;
import rhogenwizard.ConsoleHelper;
import rhogenwizard.ILogDevice;
import rhogenwizard.SysCommandExecutor;

public class RubyDebugTask extends RubyTask implements IDebugTask
{
    private final ILaunch               m_launch;
    private final String                m_appName;
    private final ConsoleHelper.Console m_console;
    private IProcess                    m_debugProcess;

    public RubyDebugTask(ILaunch launch, String appName, String workDir,
        SysCommandExecutor.Decorator decorator, String... args)
    {
        super(workDir, decorator, args);

        m_launch  = launch;
        m_appName = appName;
        m_console = ConsoleHelper.getBuildConsole();

        m_debugProcess = null;
    }

    @Override
    public boolean isOk()
    {
        return m_debugProcess != null;
    }

    @Override
    public IProcess getDebugProcess()
    {
        return m_debugProcess;
    }

    @Override
    public void exec()
    {
        m_console.show();
        m_console.getStream().print(showCommand());
    	
        SysCommandExecutor executor = new SysCommandExecutor();
        executor.setOutputLogDevice(getLogDevice(m_console.getOutputStream()));
        executor.setErrorLogDevice(getLogDevice(m_console.getErrorStream()));

        executor.setWorkingDirectory(m_workDir);

        Process process;
        try
        {
            process = executor.startCommand(m_decorator, m_cmdLine, null);
        }
        catch (IOException e)
        {
            Activator.logError(e);
            return;
        }

        m_debugProcess = DebugPlugin.newProcess(m_launch, process, m_appName);
                
        if (m_debugProcess != null)
        {
            attachConsole(m_debugProcess, m_console);
        }

        new Job("Bring console back.")
        {
            @Override
            protected IStatus run(IProgressMonitor monitor)
            {
                m_console.show();
                return Status.OK_STATUS;
            }
        }.schedule(1000);
    }

    public static void attachConsole(IProcess process, ConsoleHelper.Console console)
    {
        process.getStreamsProxy().getErrorStreamMonitor()
            .addListener(getStreamListener(console.getErrorStream()));
        process.getStreamsProxy().getOutputStreamMonitor()
            .addListener(getStreamListener(console.getOutputStream()));
    }

    private static IStreamListener getStreamListener(final ConsoleHelper.Stream stream)
    {
        return new IStreamListener()
        {
            @Override
            public void streamAppended(String text, IStreamMonitor monitor)
            {
        		stream.println(text);              
            }
        };
    }

    private static ILogDevice getLogDevice(final ConsoleHelper.Stream stream)
    {
        return new ILogDevice()
        {
            @Override
            public void log(String str)
            {
                stream.println(str.replaceAll("\\p{Cntrl}", " "));
            }
        };
    }
}

@@ -52,16 +52,38 @@
 ception;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -575,16 +575,46 @@
 Device;%0A
+import rhogenwizard.OSHelper;%0A
 import r
@@ -2550,16 +2550,324 @@
 (1000);%0A
+        %0A        try %0A        %7B%0A        %09while(true)%0A        %09%7B%0A        %09%09Set%3CInteger%3E ids = OSHelper.getProcessesIds(%22ruby.exe%22);%0A        %09%09%0A        %09%09if(ids.size() == 0)%0A        %09%09%7B%0A        %09%09%09break;%0A        %09%09%7D%0A        %09%7D%0A%09%09%7D %0A        catch (InterruptedException e) %0A        %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A
     %7D%0A%0A 

fix cancel debug task