Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi onesky projects for translations #4

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
language: php

addons:
apt:
packages:
- php-pear
- libyaml-dev

php:
- "5.6"
- "7.0"

before_script:
- composer self-update
- composer install --no-scripts --no-interaction
- composer dump-autoload -o
- yes '' | pecl install yaml-2.0.0

after_script:
- php vendor/bin/coveralls -v
Expand Down
56 changes: 43 additions & 13 deletions Command/CheckTranslationProgressCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OpenClassrooms\Bundle\OneSkyBundle\Command;

use OpenClassrooms\Bundle\OneSkyBundle\Gateways\LanguageNotFoundException;
use OpenClassrooms\Bundle\OneSkyBundle\Model\Language;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;
Expand All @@ -22,13 +23,8 @@ protected function configure()
{
$this->setName($this->getCommandName())
->setDescription($this->getCommandDescription())
->addOption(
'locale',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Requested locales',
[]
);
->addOption('projectId', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Requested projectsIds', [])
->addOption('locale', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Source locale', []);
}

/**
Expand All @@ -44,19 +40,54 @@ protected function getCommandDescription()
return self::COMMAND_DESCRIPTION;
}

private function getLocales($project, array $locales)
{
if(empty($locales))
return $project["locales"];
else
return $locales;
}

private function getProjects(array $projects, array $projectsIds)
{
if(empty($projectsIds))
return $projects;
else
{
foreach ($projects as $projectId => &$project)
if(!in_array($projectId, $projectsIds))
unset($projects[$projectId]);
return $projects;
}
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$projects = $this->getProjects($this->getContainer()->getParameter('openclassrooms_onesky.projects'), $input->getOption('projectId'));
$output->writeln('<info>Check translations progress</info>');
$languages = $this->getContainer()
->get('openclassrooms.onesky.services.language_service')
->getLanguages($input->getOption('locale'));
$languages = [];
foreach ($projects as $projectId => &$project) {
try {
$projectLanguages = $this->getContainer()
->get('openclassrooms.onesky.services.language_service')
->getLanguages($project, $this->getLocales($project, $input->getOption('locale')));
$languages = array_merge($languages, $projectLanguages);
} catch(LanguageNotFoundException $e) {
if(!empty($input->getOption('locale')))
$output->writeln('<info>Language '.$input->getOption('locale').' not found for project '.$projectId.'</info>');
else
throw $e;
}
}
if(empty($languages))
throw new LanguageNotFoundException();
$table = new Table($output);
$table
->setHeaders(['Locale', 'Progression'])
->setHeaders(['Project', 'Locale', 'Progression'])
->setRows(
array_map(
function (Language $language) {
return [$language->getLocale(), $language->getTranslationProgress()];
return [$language->getProjectId(), $language->getLocale(), $language->getTranslationProgress()];
},
$languages
)
Expand All @@ -68,7 +99,6 @@ function (Language $language) {
return 1;
}
}

return 0;
}
}
20 changes: 6 additions & 14 deletions Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function handlePullDisplay(OutputInterface $output)
$dispatcher->addListener(
TranslationPrePullEvent::getEventName(),
function (TranslationPrePullEvent $event) use ($output) {
$output->writeln('<info>Pulling for project id '.$this->getProjectId()."</info>\n");
$output->writeln("<info>Pulling files </info>\n");
$this->progressBar = new ProgressBar($output, $event->getExportFilesCount());
$this->progressBar->setFormat(PROGRESS_BAR_FORMAT);
$this->getProgressBar()->start();
Expand All @@ -73,11 +73,11 @@ function (TranslationPostPullEvent $event) use ($output) {
$output->writeln('<info>'.count($event->getDownloadedFiles()).' files downloaded. </info>');
$table = new Table($output);
$table
->setHeaders(['File', 'Locale'])
->setHeaders(['Project', 'File', 'Locale'])
->setRows(
array_map(
function (ExportFile $file) {
return [$file->getSourceFilePathRelativeToProject(), $file->getRequestedLocale()];
return [$file->getProjectId(), $file->getSourceFilePathRelativeToProject(), $file->getRequestedLocale()];
},
$event->getDownloadedFiles()
)
Expand All @@ -87,14 +87,6 @@ function (ExportFile $file) {
);
}

/**
* @return string
*/
private function getProjectId()
{
return $this->getContainer()->getParameter('openclassrooms_onesky.project_id');
}

/**
* @return ProgressBar
*/
Expand All @@ -109,7 +101,7 @@ protected function handlePushDisplay(OutputInterface $output)
$dispatcher->addListener(
TranslationPrePushEvent::getEventName(),
function (TranslationPrePushEvent $event) use ($output) {
$output->writeln('<info>Pushing for project id '.$this->getProjectId()."</info>\n");
$output->writeln("<info>Pushing files </info>\n");
$this->progressBar = new ProgressBar($output, $event->getUploadFilesCount());
$this->progressBar->setFormat(PROGRESS_BAR_FORMAT);
$this->getProgressBar()->start();
Expand All @@ -132,11 +124,11 @@ function (TranslationPostPushEvent $event) use ($output) {
$output->writeln('<info>'.count($event->getUploadedFiles()).' files downloaded. </info>');
$table = new Table($output);
$table
->setHeaders(['File', 'Locale'])
->setHeaders(['Project', 'File', 'Locale'])
->setRows(
array_map(
function (UploadFile $file) {
return [$file->getSourceFilePathRelativeToProject(), $file->getSourceLocale()];
return [$file->getProjectId(), $file->getSourceFilePathRelativeToProject(), $file->getSourceLocale()];
},
$event->getUploadedFiles()
)
Expand Down
10 changes: 3 additions & 7 deletions Command/PullCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ protected function configure()
{
$this->setName($this->getCommandName())
->setDescription($this->getCommandDescription())
->addOption('projectId', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Requested projectsIds', [])
->addOption('filePath', 'dir', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'File paths', [])
->addOption(
'locale',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Requested requestedLocale',
[]
);
->addOption('locale', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Requested requestedLocale', []);
}

/**
Expand All @@ -46,6 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->handlePullDisplay($output);
$this->getContainer()->get('openclassrooms.onesky.services.translation_service')->pull(
$input->getOption('projectId'),
$input->getOption('filePath'),
$input->getOption('locale')
);
Expand Down
12 changes: 4 additions & 8 deletions Command/PushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ protected function configure()
{
$this->setName($this->getCommandName())
->setDescription($this->getCommandDescription())
->addOption('filePath', 'dir', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'File path', [])
->addOption(
'locale',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Source locale',
[]
);
->addOption('projectId', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Requested projectsIds', [])
->addOption('filePath', 'dir', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'File paths', [])
->addOption('locale', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Source locale', []);
}

/**
Expand All @@ -46,6 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->handlePushDisplay($output);
$this->getContainer()->get('openclassrooms.onesky.services.translation_service')->push(
$input->getOption('projectId'),
$input->getOption('filePath'),
$input->getOption('locale')
);
Expand Down
12 changes: 10 additions & 2 deletions Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OpenClassrooms\Bundle\OneSkyBundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand All @@ -17,7 +18,10 @@ class UpdateCommand extends Command
protected function configure()
{
$this->setName($this->getCommandName())
->setDescription($this->getCommandDescription());
->setDescription($this->getCommandDescription())
->addOption('projectId', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Requested projectsIds', [])
->addOption('filePath', 'dir', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'File paths', [])
->addOption('locale', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Source locale', []);
}

/**
Expand All @@ -38,6 +42,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("<info>Updating translations</info>\n");
$this->handlePullDisplay($output);
$this->handlePushDisplay($output);
$this->getContainer()->get('openclassrooms.onesky.services.translation_service')->update();
$this->getContainer()->get('openclassrooms.onesky.services.translation_service')->update(
$input->getOption('projectId'),
$input->getOption('filePath'),
$input->getOption('locale')
);
}
}
19 changes: 12 additions & 7 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ public function getConfigTreeBuilder()
$rootNode->children()
->scalarNode('api_key')->isRequired()->cannotBeEmpty()->end()
->scalarNode('api_secret')->isRequired()->cannotBeEmpty()->end()
->scalarNode('project_id')->isRequired()->cannotBeEmpty()->end()
->scalarNode('file_format')->cannotBeEmpty()->defaultValue('xliff')->end()
->scalarNode('source_locale')->cannotBeEmpty()->defaultValue('en')->end()
->arrayNode('locales')->isRequired()->cannotBeEmpty()->prototype('scalar')->end()->end()
->arrayNode('file_paths')->isRequired()->cannotBeEmpty()->prototype('scalar')->end()->end()
->scalarNode('keep_all_strings')->cannotBeEmpty()->defaultValue(true)->end()
->end();
->arrayNode('projects')
->prototype('array')
->children()
->scalarNode('file_format')->cannotBeEmpty()->defaultValue('xliff')->end()
->scalarNode('source_locale')->cannotBeEmpty()->defaultValue('en')->end()
->arrayNode('file_paths')->requiresAtLeastOneElement()->prototype('scalar')->end()->end()
->arrayNode('locales')->requiresAtLeastOneElement()->prototype('scalar')->end()->end()
->scalarNode('keep_all_strings')->cannotBeEmpty()->defaultValue(true)->end()
->end()
->end()
->end()
->end();

return $treeBuilder;
}
Expand Down
7 changes: 1 addition & 6 deletions DependencyInjection/OpenClassroomsOneSkyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ private function processParameters(ContainerBuilder $container, array $config)
{
$container->setParameter('openclassrooms_onesky.api_key', $config['api_key']);
$container->setParameter('openclassrooms_onesky.api_secret', $config['api_secret']);
$container->setParameter('openclassrooms_onesky.project_id', $config['project_id']);
$container->setParameter('openclassrooms_onesky.source_locale', $config['source_locale']);
$container->setParameter('openclassrooms_onesky.locales', $config['locales']);
$container->setParameter('openclassrooms_onesky.file_format', $config['file_format']);
$container->setParameter('openclassrooms_onesky.file_paths', $config['file_paths']);
$container->setParameter('openclassrooms_onesky.keep_all_strings', $config['keep_all_strings']);
$container->setParameter('openclassrooms_onesky.projects', $config['projects']);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions Gateways/Impl/FileGatewayImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace OpenClassrooms\Bundle\OneSkyBundle\Gateways\Impl;

use Guzzle\Http\Exception\ServerErrorResponseException;
use Onesky\Api\Client;
use OpenClassrooms\Bundle\OneSkyBundle\EventListener\TranslationDownloadTranslationEvent;
use OpenClassrooms\Bundle\OneSkyBundle\EventListener\TranslationUploadTranslationEvent;
Expand All @@ -12,6 +11,8 @@
use OpenClassrooms\Bundle\OneSkyBundle\Model\ExportFile;
use OpenClassrooms\Bundle\OneSkyBundle\Model\UploadFile;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Yaml\Yaml as SFYaml;
use Symfony\Component\HttpKernel\Exception\HttpException;

/**
* @author Romain Kuzniak <[email protected]>
Expand Down Expand Up @@ -58,7 +59,16 @@ private function downloadTranslation(ExportFile $file)
);
$downloadedContent = $this->client->translations(self::DOWNLOAD_METHOD, $file->format());
$this->checkTranslation($downloadedContent, $file);
file_put_contents($file->getTargetFilePath(), $downloadedContent);
if(!empty($downloadedContent)) {
if ($file->getFormat() != "yml")
file_put_contents($file->getTargetFilePath(), $downloadedContent);
else if (is_callable("yaml_parse"))
file_put_contents($file->getTargetFilePath(), SFYaml::dump(yaml_parse($downloadedContent)));
else
throw new HttpException(500, "PHP YAML extension is needed for YAML files ( https://pecl.php.net/package/yaml )");
}
else
file_put_contents($file->getTargetFilePath(), $downloadedContent);

return $file;
}
Expand All @@ -75,7 +85,7 @@ private function checkTranslation($downloadedContent, ExportFile $file)
throw new NonExistingTranslationException($file->getTargetFilePath());
}
if (500 === $json['meta']['status']) {
throw new ServerErrorResponseException($file->getTargetFilePath());
throw new HttpException(500, "Got 500 error receiving ".$file->getTargetFilePath());
}
throw new InvalidContentException($downloadedContent);
}
Expand Down Expand Up @@ -103,6 +113,7 @@ private function uploadTranslation(UploadFile $file)
TranslationUploadTranslationEvent::getEventName(),
new TranslationUploadTranslationEvent($file)
);

$this->client->files(self::UPLOAD_METHOD, $file->format());

return $file;
Expand Down
Loading