<commit_before><?php

namespace Terramar\Packages\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Yaml\Exception\RuntimeException;
use Terramar\Packages\Adapter\FileAdapter;
use Terramar\Packages\Adapter\GitLabAdapter;
use Terramar\Packages\Adapter\SshAdapter;
use Terramar\Packages\Entity\Package;

/**
 * Updates the projects satis.json
 */
class UpdateCommand extends Command implements ContainerAwareInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;
    
    private static $template = array(
        'name' => 'Terramar Labs',
        'homepage' => 'http://packages.terramarlabs.com',
        'repositories' => array(),
        'require-all' => true,
        'output-dir' => null,
    );

    protected function configure()
    {
        $this
            ->setName('update')
            ->setDescription('Updates the project\'s satis.json file')
            ->setDefinition(array(
                new InputArgument('scan-dir', InputArgument::OPTIONAL, 'Directory to look for git repositories')
            ));
    }

    /**
     * @param InputInterface  $input  The input instance
     * @param OutputInterface $output The output instance
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $config = $this->getApplication()->getConfiguration();
        $data = array(
            'output-dir'    => realpath($config['output_dir']),
            'repositories'  => array(),
        );

        $packages = $this->container->get('doctrine.orm.entity_manager')->getRepository('Terramar\Packages\Entity\Package')->findBy(array('enabled' => true));

        $repositories = array_map(function(Package $package) {
                return $package->getSshUrl();
            }, $packages);

        foreach ($repositories as $repository) {
            $output->writeln(sprintf('Found repository: <comment>%s</comment>', $repository));
            $data['repositories'][] = array(
                'type' => 'vcs',
                'url' => $repository
            );
        }

        if (count($data['repositories']) > 0) {
            $fp = fopen('satis.json', 'w+');
            if (!$fp) {
                throw new \RuntimeException('Unable to open "satis.json" for writing.');
            }

            fwrite($fp, json_encode($data, JSON_PRETTY_PRINT));

            $output->writeln(array(
                '<info>satis.json updated successfully.</info>',
            ));
        }

        $output->writeln(array(
            sprintf('<info>Found </info>%s<info> repositories.</info>', count($data['repositories'])),
        ));
    }

    /**
     * Sets the Container.<commit_msg>UpdateCommand now always updats satis.json<commit_after><?php

namespace Terramar\Packages\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Yaml\Exception\RuntimeException;
use Terramar\Packages\Adapter\FileAdapter;
use Terramar\Packages\Adapter\GitLabAdapter;
use Terramar\Packages\Adapter\SshAdapter;
use Terramar\Packages\Entity\Package;

/**
 * Updates the projects satis.json
 */
class UpdateCommand extends Command implements ContainerAwareInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;
    
    private static $template = array(
        'name' => 'Terramar Labs',
        'homepage' => 'http://packages.terramarlabs.com',
        'repositories' => array(),
        'require-all' => true,
        'output-dir' => null,
    );

    protected function configure()
    {
        $this
            ->setName('update')
            ->setDescription('Updates the project\'s satis.json file')
            ->setDefinition(array(
                new InputArgument('scan-dir', InputArgument::OPTIONAL, 'Directory to look for git repositories')
            ));
    }

    /**
     * @param InputInterface  $input  The input instance
     * @param OutputInterface $output The output instance
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $config = $this->getApplication()->getConfiguration();
        $data = array(
            'output-dir'    => realpath($config['output_dir']),
            'repositories'  => array(),
        );

        $packages = $this->container->get('doctrine.orm.entity_manager')->getRepository('Terramar\Packages\Entity\Package')->findBy(array('enabled' => true));

        $repositories = array_map(function(Package $package) {
                return $package->getSshUrl();
            }, $packages);

        foreach ($repositories as $repository) {
            $output->writeln(sprintf('Found repository: <comment>%s</comment>', $repository));
            $data['repositories'][] = array(
                'type' => 'vcs',
                'url' => $repository
            );
        }

        $fp = fopen('satis.json', 'w+');
        if (!$fp) {
            throw new \RuntimeException('Unable to open "satis.json" for writing.');
        }
    
        fwrite($fp, json_encode($data, JSON_PRETTY_PRINT));
    
        $output->writeln(array(
            '<info>satis.json updated successfully.</info>',
        ));

        $output->writeln(array(
            sprintf('<info>Found </info>%s<info> repositories.</info>', count($data['repositories'])),
        ));
    }

    /**
     * Sets the Container.
{'fraction_non_alphanumeric': 0.1379834487417666, 'fraction_numerical': 0.00016889039013680122, 'mean_word_length': 2.6555555555555554, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 66, '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': '970859', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1598, 'n_words': 308}