From 185ea2fc380a4583e8a117ded0b954270fa35347 Mon Sep 17 00:00:00 2001 From: jean-marie leroux Date: Mon, 12 Oct 2020 16:35:45 +0200 Subject: [PATCH] Add 'valid_locale-pattern' configuration key (#43) --- .gitignore | 1 + config/example-community-3x.yml | 54 +++++++++++++++++++ config/example.yml | 2 + docker-compose.yml | 2 + .../Configuration/CrowdinConfiguration.php | 1 + .../Nelson/PullTranslationsExecutor.php | 7 ++- src/Akeneo/Nelson/TranslationFilesCleaner.php | 54 +++++++++---------- 7 files changed, 91 insertions(+), 30 deletions(-) create mode 100644 config/example-community-3x.yml diff --git a/.gitignore b/.gitignore index 95620bf..9227f03 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ bin app/console app/logs/ !config/example.yml +!config/example-*.yml config/* diff --git a/config/example-community-3x.yml b/config/example-community-3x.yml new file mode 100644 index 0000000..3416fc2 --- /dev/null +++ b/config/example-community-3x.yml @@ -0,0 +1,54 @@ +github: + token: the_github_token + fork_owner: nelson-akeneo + owner: akeneo + repository: pim-community-dev + branches: ['3.2'] +crowdin: + project: akeneo + key: the_crowdin_key + min_translated_progress: 80 + download: + base_dir: /tmp/crowdin-download + valid_locale_pattern: /^[a-z]{2}_[A-Z]{2}$/ + locale_map: + pt-PT: pt_PT + pt-BR: pt_BR + ca-ES: ca_ES + da-DK: da_DK + de-DE: de_DE + en-US: en_US + en-NZ: en_NZ + en-GB: en_GB + es-ES: es_ES + fi-FI: fi_FI + fr-FR: fr_FR + hr-HR: hr_HR + nl-NL: nl_NL + it-IT: it_IT + ja-JP: ja_JP + ko-KR: ko_KR + ru-RU: ru_RU + zh-CN: zh_CN + sv-SE: sv_SE + es-VE: es_VE + id-ID: id_ID + pl-PL: pl_PL + ro-RO: ro_RO + tl-TL: tl_TL + tr-TR: tr_TR + uk_UK: uk_UK + folders: ['PimCommunity', 'AkeneoCommunity', 'AkeneoToolCommunity'] + upload: + base_dir: /tmp/crowdin-upload +nelson: + finder_options: + in: 'src/' + notPath: '/Oro/' + path: '/Resources\/translations/' + name: '*.en_US.yml' + target_rules: + '/Resources/translations': '' + 'src/Pim': 'PimCommunity' + 'src/Akeneo': 'AkeneoCommunity' + pattern_suffix: 'Community' diff --git a/config/example.yml b/config/example.yml index 229c948..6784b60 100644 --- a/config/example.yml +++ b/config/example.yml @@ -30,6 +30,8 @@ crowdin: locale_map: fr_FR: fr fr_BE: fr_BE + # You can specify a valid locale pattern to check after before renaming the files + valid_locale_pattern: /^[a-z]{2}_[A-Z]{2}$/ upload: base_dir: /tmp/nelson-upload diff --git a/docker-compose.yml b/docker-compose.yml index f24c62c..5fc3042 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,8 +6,10 @@ services: user: 'docker' volumes: - './:/srv/nelson' + - '${SSH_AUTH_SOCK}:/ssh-agent:ro' working_dir: '/srv/nelson' environment: PHP_IDE_CONFIG: 'serverName=nelson-cli' PHP_XDEBUG_ENABLED: "${PHP_XDEBUG_ENABLED:-0}" XDEBUG_CONFIG: 'remote_host=172.17.0.1' + SSH_AUTH_SOCK: '/ssh-agent' diff --git a/src/Akeneo/DependencyInjection/Configuration/CrowdinConfiguration.php b/src/Akeneo/DependencyInjection/Configuration/CrowdinConfiguration.php index b9ce8d4..d912a43 100644 --- a/src/Akeneo/DependencyInjection/Configuration/CrowdinConfiguration.php +++ b/src/Akeneo/DependencyInjection/Configuration/CrowdinConfiguration.php @@ -31,6 +31,7 @@ public function getConfigTreeBuilder() ->isRequired() ->children() ->scalarNode('base_dir')->isRequired()->end() + ->scalarNode('valid_locale_pattern')->end() ->arrayNode('locale_map') ->defaultValue([]) ->useAttributeAsKey('name') diff --git a/src/Akeneo/Nelson/PullTranslationsExecutor.php b/src/Akeneo/Nelson/PullTranslationsExecutor.php index dcce5a6..df455a5 100644 --- a/src/Akeneo/Nelson/PullTranslationsExecutor.php +++ b/src/Akeneo/Nelson/PullTranslationsExecutor.php @@ -123,7 +123,12 @@ protected function pullTranslations($githubBranch, $crowdinFolder, array $packag $projectDir = $this->cloner->cloneProject($updateDir, $githubBranch); $this->downloader->download($packages, $options['base_dir'], $crowdinFolder); $this->extractor->extract($packages, $options['base_dir'], $cleanerDir); - $this->translationsCleaner->cleanFiles($options['locale_map'], $cleanerDir, $projectDir); + $this->translationsCleaner->cleanFiles( + $options['locale_map'], + $cleanerDir, + $projectDir, + $options['valid_locale_pattern'] ?? '/^.+$/' + ); $this->translationsCleaner->moveFiles($cleanerDir, $projectDir); if ($this->diffChecker->haveDiff($projectDir)) { diff --git a/src/Akeneo/Nelson/TranslationFilesCleaner.php b/src/Akeneo/Nelson/TranslationFilesCleaner.php index 1498c55..1c1e8fa 100644 --- a/src/Akeneo/Nelson/TranslationFilesCleaner.php +++ b/src/Akeneo/Nelson/TranslationFilesCleaner.php @@ -32,39 +32,33 @@ class TranslationFilesCleaner /** @var array */ protected $finderOptions; - /** - * @param Executor $systemExecutor - * @param EventDispatcherInterface $eventDispatcher - * @param string $patternSuffix - * @param array $finderOptions - */ public function __construct( Executor $systemExecutor, EventDispatcherInterface $eventDispatcher, - $patternSuffix, - $finderOptions + string $patternSuffix, + array $finderOptions ) { - $this->systemExecutor = $systemExecutor; - $this->patternSuffix = $patternSuffix; + $this->systemExecutor = $systemExecutor; + $this->patternSuffix = $patternSuffix; $this->eventDispatcher = $eventDispatcher; - $this->finderOptions = $finderOptions; + $this->finderOptions = $finderOptions; } /** * Clean the files before the Pull Request creation - * - * @param array $localeMap - * @param string $cleanerDir - * @param string $projectDir */ - public function cleanFiles(array $localeMap, $cleanerDir, $projectDir) - { + public function cleanFiles( + array $localeMap, + string $cleanerDir, + string $projectDir, + string $validLocalePattern + ): void { $finder = new Finder(); $translatedFiles = $finder->in($cleanerDir)->files(); foreach ($translatedFiles as $file) { $this->cleanCrowdinYamlTranslation($file); - $this->renameTranslation($file, $localeMap); + $this->renameTranslation($file, $localeMap, $validLocalePattern); } } @@ -115,7 +109,7 @@ protected function getSimpleLocale($locale, array $localeMap) * @param SplFileInfo $file * @param array $localeMap */ - protected function renameTranslation($file, $localeMap) + protected function renameTranslation(SplFileInfo $file, array $localeMap, string $validLocalePattern) { $pathInfo = pathinfo($file); @@ -131,12 +125,14 @@ protected function renameTranslation($file, $localeMap) $pathInfo['extension'] ); - $this->eventDispatcher->dispatch(Events::NELSON_RENAME, new GenericEvent($this, [ - 'from' => $file, - 'to' => $target - ])); + if (preg_match($validLocalePattern, $simpleLocale)) { + $this->eventDispatcher->dispatch(Events::NELSON_RENAME, new GenericEvent($this, [ + 'from' => $file, + 'to' => $target, + ])); - rename($file, $target); + rename($file, $target); + } } } @@ -170,10 +166,10 @@ public function moveFiles($cleanerDir, $projectDir) if ($dirExists) { $projectFinder - ->in($fullProjectDir) - ->name($this->finderOptions['name']) - ->name($filename . '.*') - ->files(); + ->in($fullProjectDir) + ->name($this->finderOptions['name']) + ->name($filename . '.*') + ->files(); } if ($dirExists && ($projectFinder->count() > 0)) { $this->systemExecutor->execute(sprintf( @@ -183,7 +179,7 @@ public function moveFiles($cleanerDir, $projectDir) )); } else { $this->eventDispatcher->dispatch(Events::NELSON_DROP_USELESS, new GenericEvent($this, [ - 'file' => $file->getPathname() + 'file' => $file->getPathname(), ])); } }