<commit_before><?php

namespace Couscous\Step\Template;

use Couscous\Model\Repository;
use Couscous\Model\Template;
use Couscous\Step\StepInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
 * Initializes the website template.
 *
 * @author <NAME> <<EMAIL>>
 */
class InitTemplate implements StepInterface
{
    const TEMPLATE_DIRECTORY = 'website';
    const PUBLIC_DIRECTORY = 'public';

    /**
     * @var Filesystem
     */
    private $filesystem;

    public function __construct(Filesystem $filesystem)
    {
        $this->filesystem = $filesystem;
    }

    public function __invoke(Repository $repository, OutputInterface $output)
    {
        $templateUrl = $repository->templateUrl;

        if ($templateUrl !== null) {
            $this->templateFromGitUrl($repository, $templateUrl, $output);
        } else {
            $this->templateFromDirectory($repository);
        }
    }

    private function templateFromDirectory(Repository $repository)
    {
        $templateDirectory = $repository->sourceDirectory . '/' . self::TEMPLATE_DIRECTORY;
        $publicDirectory = $templateDirectory . '/' . self::PUBLIC_DIRECTORY;

        if (! $this->filesystem->exists($templateDirectory)) {
            throw new \RuntimeException("The template directory doesn't exist: $templateDirectory");
        }

        $repository->template = new Template($templateDirectory, $publicDirectory);
    }

    private function templateFromGitUrl(Repository $repository, $gitUrl, OutputInterface $output)
    {
        $output->writeln("Fetching template from <info>$gitUrl</info>");

        $templateDirectory = $this->createTempDirectory('couscous_template_');

        $command = "git clone $gitUrl $templateDirectory 2>&1";
        exec($command, $gitOutput, $returnValue);
        if ($returnValue !== 0) {
            throw new \RuntimeException(implode(PHP_EOL, $gitOutput));
        }

        $publicDirectory = $templateDirectory . '/' . self::PUBLIC_DIRECTORY;
        $repository->template = new Template($templateDirectory, $publicDirectory);
    }

    private function createTempDirectory($prefix)
    {
        $tempFile = tempnam(sys_get_temp_dir(), $prefix);
        // Turn the temp file into a temp directory
        $this->filesystem->remove($tempFile);
        $this->filesystem->mkdir($tempFile);

        return $tempFile;
    }
}
<commit_msg>Bugfix in templates<commit_after><?php

namespace Couscous\Step\Template;

use Couscous\Model\Repository;
use Couscous\Model\Template;
use Couscous\Step\StepInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
 * Initializes the website template.
 *
 * @author <NAME> <<EMAIL>>
 */
class InitTemplate implements StepInterface
{
    const TEMPLATE_DIRECTORY = 'website';
    const PUBLIC_DIRECTORY = 'public';

    /**
     * @var Filesystem
     */
    private $filesystem;

    public function __construct(Filesystem $filesystem)
    {
        $this->filesystem = $filesystem;
    }

    public function __invoke(Repository $repository, OutputInterface $output)
    {
        $templateUrl = $repository->config->templateUrl;

        if ($templateUrl !== null) {
            $this->templateFromGitUrl($repository, $templateUrl, $output);
        } else {
            $this->templateFromDirectory($repository);
        }
    }

    private function templateFromDirectory(Repository $repository)
    {
        $templateDirectory = $repository->sourceDirectory . '/' . self::TEMPLATE_DIRECTORY;
        $publicDirectory = $templateDirectory . '/' . self::PUBLIC_DIRECTORY;

        if (! $this->filesystem->exists($templateDirectory)) {
            throw new \RuntimeException("The template directory doesn't exist: $templateDirectory");
        }

        $repository->template = new Template($templateDirectory, $publicDirectory);
    }

    private function templateFromGitUrl(Repository $repository, $gitUrl, OutputInterface $output)
    {
        $output->writeln("Fetching template from <info>$gitUrl</info>");

        $templateDirectory = $this->createTempDirectory('couscous_template_');

        $command = "git clone $gitUrl $templateDirectory 2>&1";
        exec($command, $gitOutput, $returnValue);
        if ($returnValue !== 0) {
            throw new \RuntimeException(implode(PHP_EOL, $gitOutput));
        }

        $publicDirectory = $templateDirectory . '/' . self::PUBLIC_DIRECTORY;
        $repository->template = new Template($templateDirectory, $publicDirectory);
    }

    private function createTempDirectory($prefix)
    {
        $tempFile = tempnam(sys_get_temp_dir(), $prefix);
        // Turn the temp file into a temp directory
        $this->filesystem->remove($tempFile);
        $this->filesystem->mkdir($tempFile);

        return $tempFile;
    }
}

{'fraction_non_alphanumeric': 0.12203112203112203, 'fraction_numerical': 0.0012285012285012285, 'mean_word_length': 3.240451388888889, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 46, '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': '2212875', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1287, 'n_words': 270}