<commit_before><?php

namespace Oro\Bundle\TranslationBundle\Command;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Translation\Catalogue\MergeOperation;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class OroTranslationPackCommand extends ContainerAwareCommand
{
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('oro:translation:pack')
            ->setDefinition(
                array(
                    new InputArgument('project', InputArgument::REQUIRED, 'The project [e.g Oro, OroCRM etc]'),
                    new InputArgument(
                        'default-locale',
                        InputArgument::OPTIONAL,
                        'The locale for creating language pack [en by default]',
                        'en'
                    ),
                    new InputOption(
                        'output-format',
                        null,
                        InputOption::VALUE_OPTIONAL,
                        'Override the default output format',
                        'yml'
                    ),
                    new InputOption(
                        'dump',
                        null,
                        InputOption::VALUE_NONE,
                        'Create language pack for  uploading to translation service'
                    ),
                )
            )
            ->setDescription('Performs operations with translation pack')
            ->setHelp(
                <<<EOF
The <info>%command.name%</info> command extract translation files for each bundle in
specified vendor namespace(project) and creates language pack that's placed at
%kernel.root_dir%/Resources/language-pack

    <info>php %command.full_name% --dump OroCRM</info>
EOF
            );
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // check presence of action
        if ($input->getOption('dump') !== true) {
            $output->writeln('<info>You must choose action: e.g --dump</info>');

            return 1;
        }

        if ($input->getOption('dump') === true) {
            $projectNamespace = $input->getArgument('project');
            $defaultLocale    = $input->getArgument('default-locale');

            $bundles   = $this->getContainer()->get('kernel')->getBundles();
            $writer    = $this->getContainer()->get('translation.writer');
            $extractor = $this->getContainer()->get('translation.extractor');
            $loader    = $this->getContainer()->get('translation.loader');
            $fs        = new Filesystem();

            foreach ($bundles as $bundle) {
                $namespaceParts = explode('\\', $bundle->getNamespace());
                if ($namespaceParts && reset($namespaceParts) === $projectNamespace) {
                    $bundleTransPath        = $bundle->getPath() . '/Resources/translations';
                    $bundleViewsPath        = $bundle->getPath() . '/Resources/views/';
                    $bundleLanguagePackPath = $this->getContainer()->getParameter('kernel.root_dir')
                        . '/Resources/language-pack/' . $projectNamespace . '/' . $bundle->getName()
                        . '/translations';

                    if (!is_dir($bundleLanguagePackPath)) {
                        $fs->mkdir($bundleLanguagePackPath);
                    }
                    $currentCatalogue   = new MessageCatalogue($defaultLocale);
                    $extractedCatalogue = new MessageCatalogue($defaultLocale);
                    if (is_dir($bundleViewsPath)) {
                        $extractor->extract($bundleViewsPath, $extractedCatalogue);
                    }
                    if (is_dir($bundleTransPath)) {
                        $loader->loadMessages($bundleTransPath, $currentCatalogue);
                    }
                    $operation = new MergeOperation($currentCatalogue, $extractedCatalogue);
                    foreach ($operation->getDomains() as $domain) {
                        $output->writeln(
                            sprintf(
                                PHP_EOL . 'Writing files for <info>%s: %s</info>',
                                $bundle->getName(),
                                $domain
                            )
                        );
                        $writer->writeTranslations(
                            $operation->getResult(),
                            $input->getOption('output-format'),
                            array('path' => $bundleLanguagePackPath)
                        );
                    }
                }
            }
        }

        return 0;
    }
}<commit_msg>BAP-1807: Create project translation extractor    - refactoring<commit_after><?php

namespace Oro\Bundle\TranslationBundle\Command;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\Translation\Catalogue\MergeOperation;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class OroTranslationPackCommand extends ContainerAwareCommand
{
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('oro:translation:pack')
            ->setDefinition(
                array(
                    new InputArgument('project', InputArgument::REQUIRED, 'The project [e.g Oro, OroCRM etc]'),
                    new InputArgument(
                        'default-locale',
                        InputArgument::OPTIONAL,
                        'The locale for creating language pack [en by default]',
                        'en'
                    ),
                    new InputOption(
                        'output-format',
                        null,
                        InputOption::VALUE_OPTIONAL,
                        'Override the default output format',
                        'yml'
                    ),
                    new InputOption(
                        'dump',
                        null,
                        InputOption::VALUE_NONE,
                        'Create language pack for  uploading to translation service'
                    ),
                )
            )
            ->setDescription('Performs operations with translation pack')
            ->setHelp(
                <<<EOF
The <info>%command.name%</info> command extract translation files for each bundle in
specified vendor namespace(project) and creates language pack that's placed at
%kernel.root_dir%/Resources/language-pack

    <info>php %command.full_name% --dump OroCRM</info>
EOF
            );
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // check presence of action
        if ($input->getOption('dump') !== true) {
            $output->writeln('<info>You must choose action: e.g --dump</info>');

            return 1;
        }

        if ($input->getOption('dump') === true) {
            $projectNamespace = $input->getArgument('project');
            $defaultLocale    = $input->getArgument('default-locale');

            $container = $this->getContainer();
            $bundles   = $container->get('kernel')->getBundles();
            $writer    = $container->get('translation.writer');

            foreach ($bundles as $bundle) {
                $namespaceParts = explode('\\', $bundle->getNamespace());
                if ($namespaceParts && reset($namespaceParts) === $projectNamespace) {
                    $bundleLanguagePackPath = $container->getParameter('kernel.root_dir')
                        . '/Resources/language-pack/' . $projectNamespace . '/' . $bundle->getName()
                        . '/translations';

                    if (!is_dir($bundleLanguagePackPath)) {
                        $this->createDirectory($bundleLanguagePackPath);
                    }

                    $operation = $this->getMergedTranslations($defaultLocale, $bundle);
                    $output->writeln(
                        sprintf(
                            'Writing files for <info>%s</info>',
                            $bundle->getName()
                        )
                    );
                    $writer->writeTranslations(
                        $operation->getResult(),
                        $input->getOption('output-format'),
                        array('path' => $bundleLanguagePackPath)
                    );
                }
            }
        }

        return 0;
    }

    /**
     * Create directory using Filesystem object
     *
     * @param string $dirPath
     */
    protected function createDirectory($dirPath)
    {
        $fs = new Filesystem();
        $fs->mkdir($dirPath);
    }

    /**
     * Merge current and extracted translations
     *
     * @param string          $defaultLocale
     * @param BundleInterface $bundle
     *
     * @return MergeOperation
     */
    protected function getMergedTranslations($defaultLocale, BundleInterface $bundle)
    {
        $bundleTransPath = $bundle->getPath() . '/Resources/translations';
        $bundleViewsPath = $bundle->getPath() . '/Resources/views/';

        $container = $this->getContainer();
        $extractor = $container->get('translation.extractor');
        $loader    = $container->get('translation.loader');

        $currentCatalogue   = new MessageCatalogue($defaultLocale);
        $extractedCatalogue = new MessageCatalogue($defaultLocale);
        if (is_dir($bundleViewsPath)) {
            $extractor->extract($bundleViewsPath, $extractedCatalogue);
        }
        if (is_dir($bundleTransPath)) {
            $loader->loadMessages($bundleTransPath, $currentCatalogue);
        }
        $operation = new MergeOperation($currentCatalogue, $extractedCatalogue);

        return $operation;
    }
}
{'fraction_non_alphanumeric': 0.11184085284599277, 'fraction_numerical': 0.0007614696363982486, 'mean_word_length': 1.6104347826086955, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 85, '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': '2371490', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2405, 'n_words': 471}