Skip to content

Commit

Permalink
Improve ExtractTask when CakePHP installed via Composer
Browse files Browse the repository at this point in the history
  • Loading branch information
garas committed Feb 3, 2016
1 parent fc57dee commit f389231
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
19 changes: 11 additions & 8 deletions lib/Cake/Console/Command/Task/ExtractTask.php
Expand Up @@ -151,7 +151,7 @@ protected function _getPaths() {
*/
public function execute() {
if (!empty($this->params['exclude'])) {
$this->_exclude = explode(',', $this->params['exclude']);
$this->_exclude = explode(',', str_replace('/', DS, $this->params['exclude']));
}
if (isset($this->params['files']) && !is_array($this->params['files'])) {
$this->_files = explode(',', $this->params['files']);
Expand Down Expand Up @@ -567,6 +567,11 @@ protected function _processValidationRules($field, $rules, $file, $domain, $cate
protected function _buildFiles() {
$paths = $this->_paths;
$paths[] = realpath(APP) . DS;

usort($paths, function ($a, $b) {
return strlen($b) - strlen($a);
});

foreach ($this->_translations as $category => $domains) {
foreach ($domains as $domain => $translations) {
foreach ($translations as $msgid => $contexts) {
Expand Down Expand Up @@ -799,19 +804,17 @@ protected function _searchFiles() {
}
$pattern = '/' . implode('|', $exclude) . '/';
}
foreach ($this->_paths as $path) {
$Folder = new Folder($path);
foreach ($this->_paths as $i => $path) {
$this->_paths[$i] = realpath($path) . DS;
$Folder = new Folder($this->_paths[$i]);
$files = $Folder->findRecursive('.*\.(php|ctp|thtml|inc|tpl)', true);
if (!empty($pattern)) {
foreach ($files as $i => $file) {
if (preg_match($pattern, $file)) {
unset($files[$i]);
}
}
$files = preg_grep($pattern, $files, PREG_GREP_INVERT);
$files = array_values($files);
}
$this->_files = array_merge($this->_files, $files);
}
$this->_files = array_unique($this->_files);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions lib/Cake/Test/Case/Console/Command/Task/ExtractTaskTest.php
Expand Up @@ -135,23 +135,23 @@ public function testExecute() {
$this->assertRegExp($pattern, $result);

// extract.ctp
$pattern = '/\#: (\\\\|\/)extract\.ctp:15;6\n';
$pattern = '/\#: extract\.ctp:15;6\n';
$pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
$this->assertRegExp($pattern, $result);

$pattern = '/msgid "You have %d new message."\nmsgstr ""/';
$this->assertNotRegExp($pattern, $result, 'No duplicate msgid');

$pattern = '/\#: (\\\\|\/)extract\.ctp:7\n';
$pattern = '/\#: extract\.ctp:7\n';
$pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
$this->assertRegExp($pattern, $result);

$pattern = '/\#: (\\\\|\/)extract\.ctp:14\n';
$pattern .= '\#: (\\\\|\/)home\.ctp:68\n';
$pattern = '/\#: extract\.ctp:14\n';
$pattern .= '\#: home\.ctp:68\n';
$pattern .= 'msgid "Editing this Page"\nmsgstr ""/';
$this->assertRegExp($pattern, $result);

$pattern = '/\#: (\\\\|\/)extract\.ctp:22\nmsgid "';
$pattern = '/\#: extract\.ctp:22\nmsgid "';
$pattern .= 'Hot features!';
$pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
$pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
Expand All @@ -162,19 +162,19 @@ public function testExecute() {
$this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
$this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');

$pattern = '/\#: (\\\\|\/)extract\.ctp:36\nmsgid "letter"/';
$pattern = '/\#: extract\.ctp:36\nmsgid "letter"/';
$this->assertRegExp($pattern, $result, 'Strings with context should not overwrite strings without context');

$pattern = '/\#: (\\\\|\/)extract\.ctp:37;39\nmsgctxt "A"\nmsgid "letter"/';
$pattern = '/\#: extract\.ctp:37;39\nmsgctxt "A"\nmsgid "letter"/';
$this->assertRegExp($pattern, $result, 'Should contain string with context "A"');

$pattern = '/\#: (\\\\|\/)extract\.ctp:38\nmsgctxt "B"\nmsgid "letter"/';
$pattern = '/\#: extract\.ctp:38\nmsgctxt "B"\nmsgid "letter"/';
$this->assertRegExp($pattern, $result, 'Should contain string with context "B"');

$pattern = '/\#: (\\\\|\/)extract\.ctp:40\nmsgid "%d letter"\nmsgid_plural "%d letters"/';
$pattern = '/\#: extract\.ctp:40\nmsgid "%d letter"\nmsgid_plural "%d letters"/';
$this->assertRegExp($pattern, $result, 'Plural strings with context should not overwrite strings without context');

$pattern = '/\#: (\\\\|\/)extract\.ctp:41\nmsgctxt "A"\nmsgid "%d letter"\nmsgid_plural "%d letters"/';
$pattern = '/\#: extract\.ctp:41\nmsgctxt "A"\nmsgid "%d letter"\nmsgid_plural "%d letters"/';
$this->assertRegExp($pattern, $result, 'Should contain plural string with context "A"');

// extract.ctp - reading the domain.pot
Expand Down

0 comments on commit f389231

Please sign in to comment.