<commit_before><?php

namespace Twr;

use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Process;

/**
 * Run a shell command
 */

class CommandRunner
{
    protected $process;
    protected $logger;

    /**
     * Set the logger
     *
     * @param LoggerInterface $logger
     */

    public function setLogger($logger)
    {
        $this->logger = $logger;
    }

    /**
     * Prepare the next command to run
     *
     * @param string $command
     * @param string $cwd Optional
     * @param array $envs Optional
     *
     * @return CommandRunner self
     */

    public function setCommand($command, $cwd = null, array $envs = null)
    {
        $this->process = new Process($command, $cwd, $envs);

        return $this;
    }

    /**
     * Execute the passed command
     *
     * @throws RuntimeException If the command don't return a successful exit code
     *
     * @param callable $callback Executed on each command output
     *
     * @return int Exit code
     */

    public function run(callable $callback)
    {
        if (!$this->process) {
            throw new \RuntimeException('No command specified');
        }

        $this->logger->info(sprintf(
            'Running command "%s"...',
            $this->process->getCommandLine()
        ));

        $this->process->run(function ($type, $buffer) use ($callback) {
            if ($type === Process::ERR) {
                $this->logger->error($buffer);
            } else {
                $this->logger->info($buffer);
            }

            $callback($type, $buffer);
        });

        $code = $this->process->getExitCode();

        if (!$this->process->isSuccessful()) {
            $msg = sprintf(
                'Command line "%s" failed',
                $this->process->getCommandLine()
            );

            $this->logger->emergency($msg, ['exitCode' => $code]);
            $this->process = null;
            throw new \RuntimeException($msg, $code);
        }

        $this->process = null;

        return $code;
    }
}<commit_msg>added the exit code in the error message when a command fails<commit_after><?php

namespace Twr;

use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Process;

/**
 * Run a shell command
 */

class CommandRunner
{
    protected $process;
    protected $logger;

    /**
     * Set the logger
     *
     * @param LoggerInterface $logger
     */

    public function setLogger($logger)
    {
        $this->logger = $logger;
    }

    /**
     * Prepare the next command to run
     *
     * @param string $command
     * @param string $cwd Optional
     * @param array $envs Optional
     *
     * @return CommandRunner self
     */

    public function setCommand($command, $cwd = null, array $envs = null)
    {
        $this->process = new Process($command, $cwd, $envs);

        return $this;
    }

    /**
     * Execute the passed command
     *
     * @throws RuntimeException If the command don't return a successful exit code
     *
     * @param callable $callback Executed on each command output
     *
     * @return int Exit code
     */

    public function run(callable $callback)
    {
        if (!$this->process) {
            throw new \RuntimeException('No command specified');
        }

        $this->logger->info(sprintf(
            'Running command "%s"...',
            $this->process->getCommandLine()
        ));

        $this->process->run(function ($type, $buffer) use ($callback) {
            if ($type === Process::ERR) {
                $this->logger->error($buffer);
            } else {
                $this->logger->info($buffer);
            }

            $callback($type, $buffer);
        });

        $code = $this->process->getExitCode();

        if (!$this->process->isSuccessful()) {
            $msg = sprintf(
                'Command line "%s" failed with the following exit code: %s',
                $this->process->getCommandLine(),
                $code
            );

            $this->logger->emergency($msg, ['exitCode' => $code]);
            $this->process = null;
            throw new \RuntimeException($msg, $code);
        }

        $this->process = null;

        return $code;
    }
}
{'fraction_non_alphanumeric': 0.13494318181818182, 'fraction_numerical': 0.0, 'mean_word_length': 1.8035832780358327, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, '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': '6783099', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1154, 'n_words': 304}