<commit_before><?php

namespace Azi\Generators;

use Azi\TimberInstaller;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

/**
 * Class Theme
 *
 * @package Azi\Generators
 */
class Theme
{
    protected $themeFilesDirectory;

    /**
     * @var
     */
    protected $name;

    /**
     * @var string
     */
    protected $path;

    /**
     * @var InputInterface $input
     */
    protected $input;

    /**
     * @var OutputInterface $output
     */
    protected $output;

    /**
     * Theme constructor.
     *
     * @param $name
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    public function __construct($name, InputInterface $input, OutputInterface $output)
    {
        $this->name = $name;
        $this->path = getcwd() . '/' . $name . '/wp-content/themes/' . $name;
        $this->fileSystem = new Filesystem();
        $this->input = $input;
        $this->output = $output;
    }

    public function getThemeFilesDirectory()
    {
        return dirname(dirname(__DIR__)) . '/theme/';
    }

    /**
     * Generate theme
     */
    public function generate()
    {
        $this->createDirectory()
            ->installTimber()
            ->scaffoldWPTheme()
            ->replaceThemeName()
            ->setupGulp()
        ->setTimeout(180);

        return $this;
    }

    /**
     * @return Process
     */
    public function setupGulp()
    {
        $process = new Process("cd $this->path && echo {} > package.json && npm install gulp gulp-clean-css gulp-concat gulp-imagemin gulp-rename gulp-sass gulp-uglify  --save-dev");
        if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
            $process->setTty(true);
        }

        $process->run(function ( $type, $line ) {
            $this->output->writeln($line);
        });

        return $this;
    }

    /**
     * Sets timeout for Symphony process
     * @param $timeout
     */
    public function setTimeout($timeout)
    {
        $this->timeout = $timeout;
    }

    /**
     * Create theme directory
     */
    protected function createDirectory()
    {
        if (is_dir($this->path) && is_file($this->path)) {
            throw new RuntimeException('There is a theme with the same name');
        }

        mkdir($this->path);

        return $this;
    }

    /**
     * Install timber
     */
    protected function installTimber()
    {
        (new TimberInstaller($this->path, $this->output))->install();

        return $this;
    }

    /**<commit_msg>Add Timeout to setupGulp method<commit_after><?php

namespace Azi\Generators;

use Azi\TimberInstaller;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

/**
 * Class Theme
 *
 * @package Azi\Generators
 */
class Theme
{
    protected $themeFilesDirectory;

    /**
     * @var
     */
    protected $name;

    /**
     * @var string
     */
    protected $path;

    /**
     * @var InputInterface $input
     */
    protected $input;

    /**
     * @var OutputInterface $output
     */
    protected $output;

    /**
     * Theme constructor.
     *
     * @param $name
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    public function __construct($name, InputInterface $input, OutputInterface $output)
    {
        $this->name = $name;
        $this->path = getcwd() . '/' . $name . '/wp-content/themes/' . $name;
        $this->fileSystem = new Filesystem();
        $this->input = $input;
        $this->output = $output;
    }

    public function getThemeFilesDirectory()
    {
        return dirname(dirname(__DIR__)) . '/theme/';
    }

    /**
     * Generate theme
     */
    public function generate()
    {
        $this->createDirectory()
            ->installTimber()
            ->scaffoldWPTheme()
            ->replaceThemeName()
            ->setupGulp();

        return $this;
    }

    /**
     * @return Process
     */
    public function setupGulp()
    {
        $process = new Process("cd $this->path && echo {} > package.json && npm install gulp gulp-clean-css gulp-concat gulp-imagemin gulp-rename gulp-sass gulp-uglify  --save-dev");


        $process->setTimeout(2 * 3600);

        if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
            $process->setTty(true);
        }

        $process->run(function ( $type, $line ) {
            $this->output->writeln($line);

        });

        return $this;
    }


    /**
     * Create theme directory
     */
    protected function createDirectory()
    {
        if (is_dir($this->path) && is_file($this->path)) {
            throw new RuntimeException('There is a theme with the same name');
        }

        mkdir($this->path);

        return $this;
    }

    /**
     * Install timber
     */
    protected function installTimber()
    {
        (new TimberInstaller($this->path, $this->output))->install();

        return $this;
    }

    /**
{'fraction_non_alphanumeric': 0.14352196574832465, 'fraction_numerical': 0.0014892032762472078, 'mean_word_length': 2.045918367346939, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, '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': '739039', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1566, 'n_words': 325}