Skip to content

Commit

Permalink
Add 'valid_locale-pattern' configuration key (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmleroux authored Oct 12, 2020
1 parent ed5210b commit 185ea2f
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ bin
app/console
app/logs/
!config/example.yml
!config/example-*.yml
config/*
54 changes: 54 additions & 0 deletions config/example-community-3x.yml
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 2 additions & 0 deletions config/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 6 additions & 1 deletion src/Akeneo/Nelson/PullTranslationsExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
54 changes: 25 additions & 29 deletions src/Akeneo/Nelson/TranslationFilesCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -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(
Expand All @@ -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(),
]));
}
}
Expand Down

0 comments on commit 185ea2f

Please sign in to comment.