<commit_before><?php

namespace Parsnick\Steak\Console;

use GitWrapper\GitWorkingCopy;
use GitWrapper\GitWrapper;
use Illuminate\Config\Repository;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Yaml\Dumper;

class InitCommand extends Command
{
    /**
     * @var GitWrapper
     */
    protected $git;

    /**
     * @var Filesystem
     */
    protected $files;

    /**
     * @var Dumper
     */
    protected $yaml;

    /**
     * Create a new DeployCommand instance.
     *
     * @param GitWrapper $git
     * @param Filesystem $files
     * @param Dumper $yaml
     */
    public function __construct(GitWrapper $git, Filesystem $files, Dumper $yaml)
    {
        $this->git = $git;
        $this->files = $files;
        $this->yaml = $yaml;

        parent::__construct();
    }

    /**
     * Configure the command options.
     *
     * @return void
     */
    protected function configure()
    {
        $this
            ->setName('init')
            ->setDescription('Sets up the project for steak')
            ->addOption('generate', 'g', InputOption::VALUE_OPTIONAL, 'Path to the config file to save', 'steak.yml')
        ;
    }

    /**
     * Execute the command.
     *
     * @param  InputInterface  $input
     * @param  OutputInterface  $output
     * @return void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->setIo($input, $output);

        /** @var Repository $config */
        $config = $this->container['config'];

        $generated = [];

        $output->writeln("<info>Configuring <b>steak</b>...</info>");

        $this->title('Directories');
        $generated['source'] = trim($this->ask("Where should the site source files be stored?", "{$config['source']}/"), '/');
        $generated['output'] = trim($this->ask("Where should the generated site files be created?", "{$config['output']}/"), '/');

        $this->title('Deployment via Git');
        $generated['deploy.git'] = $this->ask(
            "Specify a target repository for the <path>steak deploy</path> command to use:",
            $config->get('deploy.git', $this->getPushUrl('.'))
        );
        $generated['deploy.branch'] = $this->ask(
            "Specify the branch to use:",
            $config->get('deploy.branch', 'gh-pages')
        );

        $this->title('Gulp integration');
        $generated['gulp.file'] = $this->ask("Which gulp file should we use to publish non-PHP assets?", $config->get('gulp.file', 'gulpfile.js'));

        $this->title('Local development server');
        $generated['server.directory'] = $this->ask(
            "Serve the site in a subdirectory when using <path>steak serve</path>? \nUse your project name to emulate the github-pages behaviour.",
            $config->get('server.directory') ?: $this->guessSubdirectory($generated['deploy.git'])
        );

        $yaml = $this->createYaml($generated);

        $output->writeln("\n<comment>Ready to write config file...</comment>");
        $output->writeln($yaml);

        $saveTo = $input->getOption('generate');

        if ($this->confirmGeneration($saveTo)) {

            if ($this->files->put($saveTo, $yaml)) {
                $output->writeln("<info>Success! <path>{$saveTo}</path> written.</info>");
            } else {
                $output->writeln('<error>Failed to save config file.</error>');
            }

        } else {
            $output->writeln('<error>Aborted!</error>');
        }
    }

    /**
     * Ask a question.
     *
     * @param string $question
     * @param null|string $default
     * @return string
     */
    protected function ask($question, $default = null)
    {
        if ($default) {
            $question .= " [$default]";
        }

        $question = new Question($question . "\n> ", $default);

        return trim($this->getHelper('question')->ask($this->input, $this->output, $question));
    }

    /**
     * Print a title.
     *
     * @param $string
     */
    protected function title($string)
    {
        $this->output->writeln('');
        $this->output->writeln("<b>$string</b>");
    }

    /**
     * Check if a directory is the root of a git repository.
     *
     * @param string $dir
     * @return bool
     */
    protected function isGitRepo($dir)
    {
        return $this->git->workingCopy($dir)->isCloned();
    }

    /**
     * Get the origin's push URL.
     *
     * @param string $dir
     * @return null|string
     */
    protected function getPushUrl($dir)
    {
        $remotes = $this->git->workingCopy($dir)->remote('show', 'origin',  '-n')->getOutput();

        if (preg_match('!Push\s*URL:\s*([^\s#]+)!', $remotes, $matches)) {
            return $matches[1];
        }

        return null;
    }

    /**
     * Guess the subdirectory to serve from to match github pages.
     *
     * @param string $deployUrl
     * @return string
     */<commit_msg>Flattened some logic in InitCommand<commit_after><?php

namespace Parsnick\Steak\Console;

use GitWrapper\GitWorkingCopy;
use GitWrapper\GitWrapper;
use Illuminate\Config\Repository;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Yaml\Dumper;

class InitCommand extends Command
{
    /**
     * @var GitWrapper
     */
    protected $git;

    /**
     * @var Filesystem
     */
    protected $files;

    /**
     * @var Dumper
     */
    protected $yaml;

    /**
     * Create a new DeployCommand instance.
     *
     * @param GitWrapper $git
     * @param Filesystem $files
     * @param Dumper $yaml
     */
    public function __construct(GitWrapper $git, Filesystem $files, Dumper $yaml)
    {
        $this->git = $git;
        $this->files = $files;
        $this->yaml = $yaml;

        parent::__construct();
    }

    /**
     * Configure the command options.
     *
     * @return void
     */
    protected function configure()
    {
        $this
            ->setName('init')
            ->setDescription('Sets up the project for steak')
            ->addOption('generate', 'g', InputOption::VALUE_OPTIONAL, 'Path to the config file to save', 'steak.yml')
        ;
    }

    /**
     * Execute the command.
     *
     * @param  InputInterface  $input
     * @param  OutputInterface  $output
     * @return void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->setIo($input, $output);

        /** @var Repository $config */
        $config = $this->container['config'];

        $generated = [];

        $output->writeln("<info>Configuring <b>steak</b>...</info>");

        $this->title('Directories');
        $generated['source'] = trim($this->ask("Where should the site source files be stored?", "{$config['source']}/"), '/');
        $generated['output'] = trim($this->ask("Where should the generated site files be created?", "{$config['output']}/"), '/');

        $this->title('Deployment via Git');
        $generated['deploy.git'] = $this->ask(
            "Specify a target repository for the <path>steak deploy</path> command to use:",
            $config->get('deploy.git', $this->getPushUrl('.'))
        );
        $generated['deploy.branch'] = $this->ask(
            "Specify the branch to use:",
            $config->get('deploy.branch', 'gh-pages')
        );

        $this->title('Gulp integration');
        $generated['gulp.file'] = $this->ask("Which gulp file should we use to publish non-PHP assets?", $config->get('gulp.file', 'gulpfile.js'));

        $this->title('Local development server');
        $generated['server.directory'] = $this->ask(
            "Serve the site in a subdirectory when using <path>steak serve</path>? \nUse your project name to emulate the github-pages behaviour.",
            $config->get('server.directory') ?: $this->guessSubdirectory($generated['deploy.git'])
        );

        $yaml = $this->createYaml($generated);

        $output->writeln("\n<comment>Ready to write config file...</comment>");
        $output->writeln($yaml);

        $saveTo = $input->getOption('generate');

        if ( ! $this->confirmGeneration($saveTo)) {
            return $output->writeln('<error>Aborted!</error>');
        }

        if ( ! $this->files->put($saveTo, $yaml)) {
            return $output->writeln('<error>Failed to save config file.</error>');
        }

        $output->writeln("<info>Success! <path>{$saveTo}</path> written.</info>");
    }

    /**
     * Ask a question.
     *
     * @param string $question
     * @param null|string $default
     * @return string
     */
    protected function ask($question, $default = null)
    {
        if ($default) {
            $question .= " [$default]";
        }

        $question = new Question($question . "\n> ", $default);

        return trim($this->getHelper('question')->ask($this->input, $this->output, $question));
    }

    /**
     * Print a title.
     *
     * @param $string
     */
    protected function title($string)
    {
        $this->output->writeln('');
        $this->output->writeln("<b>$string</b>");
    }

    /**
     * Get the origin's push URL.
     *
     * @param string $dir
     * @return null|string
     */
    protected function getPushUrl($dir)
    {
        $remotes = $this->git->workingCopy($dir)->remote('show', 'origin',  '-n')->getOutput();

        if (preg_match('!Push\s*URL:\s*([^\s#]+)!', $remotes, $matches)) {
            return $matches[1];
        }

        return null;
    }

    /**
     * Guess the subdirectory to serve from to match github pages.
     *
     * @param string $deployUrl
     * @return string
     */
{'fraction_non_alphanumeric': 0.16255064634381633, 'fraction_numerical': 0.00019293845263360988, 'mean_word_length': 2.4034799737360473, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 142, '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': '5188076', 'n_tokens_mistral': 3063, 'n_tokens_neox': 2983, 'n_words': 667}