<commit_before>package org.rubyforge.debugcommons;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.rubyforge.debugcommons.RubyDebuggerProxy.DebuggerType;
import org.rubyforge.debugcommons.Util;
import org.rubyforge.debugcommons.model.RubyDebugTarget;

import static org.rubyforge.debugcommons.RubyDebuggerProxy.CLASSIC_DEBUGGER;
import static org.rubyforge.debugcommons.RubyDebuggerProxy.RUBY_DEBUG;

public final class RubyDebuggerFactory {
    
    private static final String CLASSIC_DEBUG_NAME = "classic-debug.rb";
    private static final String CLASSIC_VERBOSE_DEBUG_NAME = "classic-debug-verbose.rb";
    
    private static final int DEFAULT_PORT = 1098;
    
    /**
     * Starts classic-debugger session for the given script. Debugger waits on
     * the first script's line.
     *
     * @param descriptor {@link Descriptor} to be used
     * @param pathToClassicDebugDir directory containing classic-debug.rb and
     *        classic-debug-verbose.rb.
     * @param interpreter interpreter to be used
     * @return {@link RubyDebugTarget} instance
     * @throws java.io.IOException
     * @throws org.rubyforge.debugcommons.RubyDebuggerException
     */
    public static RubyDebuggerProxy startClassicDebugger(
            final Descriptor descriptor,
            final String pathToClassicDebugDir,
            final String interpreter)
            throws IOException, RubyDebuggerException {
        descriptor.setType(CLASSIC_DEBUGGER);
        List<String> args = new ArrayList<String>();
        args.add(interpreter);
        args.add("-I");
        args.add(pathToClassicDebugDir);
        if (!descriptor.defaultPortUsed()) {
            if (descriptor.getPort() != -1) {
                try {
                    String path = createRemoteDebugPortFile(descriptor.getPort());
                    args.add("-r");
                    args.add(path);
                } catch (IOException e) {
                    Util.severe("Could not create 'RemoteDebugPortFile'. Using default port.", e);
                }
            }
        }
        if (descriptor.isSynchronizedOutput()) {
            String path = createIOSynchronizer();
            args.add("-r");
            args.add(path);
        }
        args.add("-r");
        args.add(descriptor.isVerbose() ? CLASSIC_VERBOSE_DEBUG_NAME : CLASSIC_DEBUG_NAME);
        args.addAll(descriptor.getAddtionalOptions());
        args.add(descriptor.getScriptPath());
        if (descriptor.getScriptArguments() != null) {
            args.addAll(Arrays.asList(descriptor.getScriptArguments()));
        }
        return startDebugger(descriptor, args);
    }
    
    /**
     * Delegates to {@link #startRubyDebug(Descriptor, String, String)} with
     * <code>null</code> for <code>interpreter</code> parameter.
     */
    public static RubyDebuggerProxy startRubyDebug(final Descriptor descriptor,
            final String rdebugExecutable) throws IOException, RubyDebuggerException {
        return startRubyDebug(descriptor, rdebugExecutable, null);
    }
    
    /**
     * Starts Kent Sibilev's ruby-debug session for the given script. Debugger
     * waits on the first script's line.
     *
     * @param descriptor {@link Descriptor} to be used
     * @param rdebugExecutable path to rdebug-ide
     * @param interpreter interpreter to be used for running
     *        <code>rdebugExecutable</code>. If <code>null</code>
     *        <code>rdebugExecutable</code> will be run directly using default
     *        system interpreter.
     * @return {@link RubyDebugTarget} instance
     * @throws java.io.IOException<commit_msg>Prepend custom options rather than append<commit_after>package org.rubyforge.debugcommons;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.rubyforge.debugcommons.RubyDebuggerProxy.DebuggerType;
import org.rubyforge.debugcommons.Util;
import org.rubyforge.debugcommons.model.RubyDebugTarget;

import static org.rubyforge.debugcommons.RubyDebuggerProxy.CLASSIC_DEBUGGER;
import static org.rubyforge.debugcommons.RubyDebuggerProxy.RUBY_DEBUG;

public final class RubyDebuggerFactory {
    
    private static final String CLASSIC_DEBUG_NAME = "classic-debug.rb";
    private static final String CLASSIC_VERBOSE_DEBUG_NAME = "classic-debug-verbose.rb";
    
    private static final int DEFAULT_PORT = 1098;
    
    /**
     * Starts classic-debugger session for the given script. Debugger waits on
     * the first script's line.
     *
     * @param descriptor {@link Descriptor} to be used
     * @param pathToClassicDebugDir directory containing classic-debug.rb and
     *        classic-debug-verbose.rb.
     * @param interpreter interpreter to be used
     * @return {@link RubyDebugTarget} instance
     * @throws java.io.IOException
     * @throws org.rubyforge.debugcommons.RubyDebuggerException
     */
    public static RubyDebuggerProxy startClassicDebugger(
            final Descriptor descriptor,
            final String pathToClassicDebugDir,
            final String interpreter)
            throws IOException, RubyDebuggerException {
        descriptor.setType(CLASSIC_DEBUGGER);
        List<String> args = new ArrayList<String>();
        args.add(interpreter);
        args.addAll(descriptor.getAddtionalOptions());
        args.add("-I");
        args.add(pathToClassicDebugDir);
        if (!descriptor.defaultPortUsed()) {
            if (descriptor.getPort() != -1) {
                try {
                    String path = createRemoteDebugPortFile(descriptor.getPort());
                    args.add("-r");
                    args.add(path);
                } catch (IOException e) {
                    Util.severe("Could not create 'RemoteDebugPortFile'. Using default port.", e);
                }
            }
        }
        if (descriptor.isSynchronizedOutput()) {
            String path = createIOSynchronizer();
            args.add("-r");
            args.add(path);
        }
        args.add("-r");
        args.add(descriptor.isVerbose() ? CLASSIC_VERBOSE_DEBUG_NAME : CLASSIC_DEBUG_NAME);
        args.add(descriptor.getScriptPath());
        if (descriptor.getScriptArguments() != null) {
            args.addAll(Arrays.asList(descriptor.getScriptArguments()));
        }
        return startDebugger(descriptor, args);
    }
    
    /**
     * Delegates to {@link #startRubyDebug(Descriptor, String, String)} with
     * <code>null</code> for <code>interpreter</code> parameter.
     */
    public static RubyDebuggerProxy startRubyDebug(final Descriptor descriptor,
            final String rdebugExecutable) throws IOException, RubyDebuggerException {
        return startRubyDebug(descriptor, rdebugExecutable, null);
    }
    
    /**
     * Starts <NAME>'s ruby-debug session for the given script. Debugger
     * waits on the first script's line.
     *
     * @param descriptor {@link Descriptor} to be used
     * @param rdebugExecutable path to rdebug-ide
     * @param interpreter interpreter to be used for running
     *        <code>rdebugExecutable</code>. If <code>null</code>
     *        <code>rdebugExecutable</code> will be run directly using default
     *        system interpreter.
     * @return {@link RubyDebugTarget} instance
     * @throws java.io.IOException
{'fraction_non_alphanumeric': 0.09532515016975712, 'fraction_numerical': 0.0013058239749281796, 'mean_word_length': 2.7379209370424595, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4820090', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2049, 'n_words': 489}